os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/vacuum.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2001 September 15
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this file is testing the VACUUM statement.
sl@0
    13
#
sl@0
    14
# $Id: vacuum.test,v 1.41 2008/04/15 02:36:34 drh Exp $
sl@0
    15
sl@0
    16
set testdir [file dirname $argv0]
sl@0
    17
source $testdir/tester.tcl
sl@0
    18
sl@0
    19
# If the VACUUM statement is disabled in the current build, skip all
sl@0
    20
# the tests in this file.
sl@0
    21
#
sl@0
    22
ifcapable {!vacuum} {
sl@0
    23
  omit_test vacuum.test {Compiled with SQLITE_OMIT_VACUUM}
sl@0
    24
  finish_test
sl@0
    25
  return
sl@0
    26
}
sl@0
    27
if $AUTOVACUUM {
sl@0
    28
  omit_test vacuum.test {Auto-vacuum is enabled}
sl@0
    29
  finish_test
sl@0
    30
  return
sl@0
    31
}
sl@0
    32
sl@0
    33
set fcnt 1
sl@0
    34
do_test vacuum-1.1 {
sl@0
    35
  execsql {
sl@0
    36
    BEGIN;
sl@0
    37
    CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
sl@0
    38
    INSERT INTO t1 VALUES(NULL,randstr(10,100),randstr(5,50));
sl@0
    39
    INSERT INTO t1 VALUES(123456,randstr(10,100),randstr(5,50));
sl@0
    40
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
sl@0
    41
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
sl@0
    42
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
sl@0
    43
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
sl@0
    44
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
sl@0
    45
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
sl@0
    46
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
sl@0
    47
    CREATE INDEX i1 ON t1(b,c);
sl@0
    48
    CREATE UNIQUE INDEX i2 ON t1(c,a);
sl@0
    49
    CREATE TABLE t2 AS SELECT * FROM t1;
sl@0
    50
    COMMIT;
sl@0
    51
    DROP TABLE t2;
sl@0
    52
  }
sl@0
    53
  set ::size1 [file size test.db]
sl@0
    54
  set ::cksum [cksum]
sl@0
    55
  expr {$::cksum!=""}
sl@0
    56
} {1}
sl@0
    57
do_test vacuum-1.2 {
sl@0
    58
  execsql {
sl@0
    59
    VACUUM;
sl@0
    60
  }
sl@0
    61
  cksum
sl@0
    62
} $cksum
sl@0
    63
ifcapable vacuum {
sl@0
    64
  do_test vacuum-1.3 {
sl@0
    65
    expr {[file size test.db]<$::size1}
sl@0
    66
  } {1}
sl@0
    67
}
sl@0
    68
do_test vacuum-1.4 {
sl@0
    69
  set sql_script {
sl@0
    70
    BEGIN;
sl@0
    71
    CREATE TABLE t2 AS SELECT * FROM t1;
sl@0
    72
    CREATE TABLE t3 AS SELECT * FROM t1;
sl@0
    73
    CREATE VIEW v1 AS SELECT b, c FROM t3;
sl@0
    74
    CREATE TRIGGER r1 AFTER DELETE ON t2 BEGIN SELECT 1; END;
sl@0
    75
    COMMIT;
sl@0
    76
    DROP TABLE t2;
sl@0
    77
  }
sl@0
    78
  # If the library was compiled to omit view support, comment out the
sl@0
    79
  # create view in the script $sql_script before executing it. Similarly,
sl@0
    80
  # if triggers are not supported, comment out the trigger definition.
sl@0
    81
  ifcapable !view {
sl@0
    82
    regsub {CREATE VIEW} $sql_script {-- CREATE VIEW} sql_script
sl@0
    83
  }
sl@0
    84
  ifcapable !trigger {
sl@0
    85
    regsub {CREATE TRIGGER} $sql_script {-- CREATE TRIGGER} sql_script
sl@0
    86
  }
sl@0
    87
  execsql $sql_script
sl@0
    88
  set ::size1 [file size test.db]
sl@0
    89
  set ::cksum [cksum]
sl@0
    90
  expr {$::cksum!=""}
sl@0
    91
} {1}
sl@0
    92
do_test vacuum-1.5 {
sl@0
    93
  execsql {
sl@0
    94
    VACUUM;
sl@0
    95
  }
sl@0
    96
  cksum
sl@0
    97
} $cksum
sl@0
    98
sl@0
    99
ifcapable vacuum {
sl@0
   100
  do_test vacuum-1.6 {
sl@0
   101
    expr {[file size test.db]<$::size1}
sl@0
   102
  } {1}
sl@0
   103
}
sl@0
   104
ifcapable vacuum {
sl@0
   105
  do_test vacuum-2.1 {
sl@0
   106
    catchsql {
sl@0
   107
      BEGIN;
sl@0
   108
      VACUUM;
sl@0
   109
      COMMIT;
sl@0
   110
    }
sl@0
   111
  } {1 {cannot VACUUM from within a transaction}}
sl@0
   112
  catch {db eval COMMIT}
sl@0
   113
}
sl@0
   114
do_test vacuum-2.2 {
sl@0
   115
  sqlite3 db2 test.db
sl@0
   116
  execsql {
sl@0
   117
    BEGIN;
sl@0
   118
    CREATE TABLE t4 AS SELECT * FROM t1;
sl@0
   119
    CREATE TABLE t5 AS SELECT * FROM t1;
sl@0
   120
    COMMIT;
sl@0
   121
    DROP TABLE t4;
sl@0
   122
    DROP TABLE t5;
sl@0
   123
  } db2
sl@0
   124
  set ::cksum [cksum db2]
sl@0
   125
  catchsql {
sl@0
   126
    VACUUM
sl@0
   127
  }
sl@0
   128
} {0 {}}
sl@0
   129
do_test vacuum-2.3 {
sl@0
   130
  cksum
sl@0
   131
} $cksum
sl@0
   132
do_test vacuum-2.4 {
sl@0
   133
  catch {db2 eval {SELECT count(*) FROM sqlite_master}}
sl@0
   134
  cksum db2
sl@0
   135
} $cksum
sl@0
   136
sl@0
   137
# Make sure the schema cookie is incremented by vacuum.
sl@0
   138
#
sl@0
   139
do_test vacuum-2.5 {
sl@0
   140
  execsql {
sl@0
   141
    BEGIN;
sl@0
   142
    CREATE TABLE t6 AS SELECT * FROM t1;
sl@0
   143
    CREATE TABLE t7 AS SELECT * FROM t1;
sl@0
   144
    COMMIT;
sl@0
   145
  }
sl@0
   146
  sqlite3 db3 test.db
sl@0
   147
  execsql {
sl@0
   148
    -- The "SELECT * FROM sqlite_master" statement ensures that this test
sl@0
   149
    -- works when shared-cache is enabled. If shared-cache is enabled, then
sl@0
   150
    -- db3 shares a cache with db2 (but not db - it was opened as 
sl@0
   151
    -- "./test.db").
sl@0
   152
    SELECT * FROM sqlite_master;
sl@0
   153
    SELECT * FROM t7 LIMIT 1
sl@0
   154
  } db3
sl@0
   155
  execsql {
sl@0
   156
    VACUUM;
sl@0
   157
  }
sl@0
   158
  execsql {
sl@0
   159
    INSERT INTO t7 VALUES(1234567890,'hello','world');
sl@0
   160
  } db3
sl@0
   161
  execsql {
sl@0
   162
    SELECT * FROM t7 WHERE a=1234567890
sl@0
   163
  }
sl@0
   164
} {1234567890 hello world}
sl@0
   165
integrity_check vacuum-2.6
sl@0
   166
do_test vacuum-2.7 {
sl@0
   167
  execsql {
sl@0
   168
    SELECT * FROM t7 WHERE a=1234567890
sl@0
   169
  } db3
sl@0
   170
} {1234567890 hello world}
sl@0
   171
do_test vacuum-2.8 {
sl@0
   172
  execsql {
sl@0
   173
    INSERT INTO t7 SELECT * FROM t6;
sl@0
   174
    SELECT count(*) FROM t7;
sl@0
   175
  }
sl@0
   176
} 513
sl@0
   177
integrity_check vacuum-2.9
sl@0
   178
do_test vacuum-2.10 {
sl@0
   179
  execsql {
sl@0
   180
    DELETE FROM t7;
sl@0
   181
    SELECT count(*) FROM t7;
sl@0
   182
  } db3
sl@0
   183
} 0
sl@0
   184
integrity_check vacuum-2.11
sl@0
   185
db3 close
sl@0
   186
 
sl@0
   187
sl@0
   188
# Ticket #427.  Make sure VACUUM works when the EMPTY_RESULT_CALLBACKS
sl@0
   189
# pragma is turned on.
sl@0
   190
#
sl@0
   191
do_test vacuum-3.1 {
sl@0
   192
  db close
sl@0
   193
  db2 close
sl@0
   194
  file delete test.db
sl@0
   195
  sqlite3 db test.db
sl@0
   196
  execsql {
sl@0
   197
    PRAGMA empty_result_callbacks=on;
sl@0
   198
    VACUUM;
sl@0
   199
  }
sl@0
   200
} {}
sl@0
   201
sl@0
   202
# Ticket #464.  Make sure VACUUM works with the sqlite3_prepare() API.
sl@0
   203
#
sl@0
   204
do_test vacuum-4.1 {
sl@0
   205
  db close
sl@0
   206
  sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   207
  set VM [sqlite3_prepare $DB {VACUUM} -1 TAIL]
sl@0
   208
  sqlite3_step $VM
sl@0
   209
} {SQLITE_DONE}
sl@0
   210
do_test vacuum-4.2 {
sl@0
   211
  sqlite3_finalize $VM
sl@0
   212
} SQLITE_OK
sl@0
   213
sl@0
   214
# Ticket #515.  VACUUM after deleting and recreating the table that
sl@0
   215
# a view refers to. Omit this test if the library is not view-enabled.
sl@0
   216
#
sl@0
   217
ifcapable view {
sl@0
   218
do_test vacuum-5.1 {
sl@0
   219
  db close
sl@0
   220
  file delete -force test.db
sl@0
   221
  sqlite3 db test.db
sl@0
   222
  catchsql {
sl@0
   223
    CREATE TABLE Test (TestID int primary key);
sl@0
   224
    INSERT INTO Test VALUES (NULL);
sl@0
   225
    CREATE VIEW viewTest AS SELECT * FROM Test;
sl@0
   226
sl@0
   227
    BEGIN;
sl@0
   228
    CREATE TABLE tempTest (TestID int primary key, Test2 int NULL);
sl@0
   229
    INSERT INTO tempTest SELECT TestID, 1 FROM Test;
sl@0
   230
    DROP TABLE Test;
sl@0
   231
    CREATE TABLE Test(TestID int primary key, Test2 int NULL);
sl@0
   232
    INSERT INTO Test SELECT * FROM tempTest;
sl@0
   233
    DROP TABLE tempTest;
sl@0
   234
    COMMIT;
sl@0
   235
    VACUUM;
sl@0
   236
  }
sl@0
   237
} {0 {}}
sl@0
   238
do_test vacuum-5.2 {
sl@0
   239
  catchsql {
sl@0
   240
    VACUUM;
sl@0
   241
  }
sl@0
   242
} {0 {}}
sl@0
   243
} ;# ifcapable view
sl@0
   244
sl@0
   245
# Ensure vacuum works with complicated tables names.
sl@0
   246
do_test vacuum-6.1 {
sl@0
   247
  execsql {
sl@0
   248
    CREATE TABLE "abc abc"(a, b, c);
sl@0
   249
    INSERT INTO "abc abc" VALUES(1, 2, 3);
sl@0
   250
    VACUUM;
sl@0
   251
  }
sl@0
   252
} {}
sl@0
   253
do_test vacuum-6.2 {
sl@0
   254
  execsql {
sl@0
   255
    select * from "abc abc";
sl@0
   256
  }
sl@0
   257
} {1 2 3}
sl@0
   258
sl@0
   259
# Also ensure that blobs survive a vacuum.
sl@0
   260
ifcapable {bloblit} {
sl@0
   261
  do_test vacuum-6.3 {
sl@0
   262
    execsql {
sl@0
   263
      DELETE FROM "abc abc";
sl@0
   264
      INSERT INTO "abc abc" VALUES(X'00112233', NULL, NULL);
sl@0
   265
      VACUUM;
sl@0
   266
    }
sl@0
   267
  } {}
sl@0
   268
  do_test vacuum-6.4 {
sl@0
   269
    execsql {
sl@0
   270
      select count(*) from "abc abc" WHERE a = X'00112233';
sl@0
   271
    }
sl@0
   272
  } {1}
sl@0
   273
}
sl@0
   274
sl@0
   275
# Check what happens when an in-memory database is vacuumed. The
sl@0
   276
# [file delete] command covers us in case the library was compiled
sl@0
   277
# without in-memory database support.
sl@0
   278
#
sl@0
   279
file delete -force :memory:
sl@0
   280
do_test vacuum-7.0 {
sl@0
   281
  sqlite3 db2 :memory:
sl@0
   282
  execsql {
sl@0
   283
    CREATE TABLE t1(t);
sl@0
   284
    VACUUM;
sl@0
   285
  } db2
sl@0
   286
} {}
sl@0
   287
db2 close
sl@0
   288
sl@0
   289
# Ticket #873.  VACUUM a database that has ' in its name.
sl@0
   290
#
sl@0
   291
do_test vacuum-8.1 {
sl@0
   292
  file delete -force a'z.db
sl@0
   293
  file delete -force a'z.db-journal
sl@0
   294
  sqlite3 db2 a'z.db
sl@0
   295
  execsql {
sl@0
   296
    CREATE TABLE t1(t);
sl@0
   297
    VACUUM;
sl@0
   298
  } db2
sl@0
   299
} {}
sl@0
   300
db2 close
sl@0
   301
sl@0
   302
# Ticket #1095:  Vacuum a table that uses AUTOINCREMENT
sl@0
   303
#
sl@0
   304
ifcapable {autoinc} {
sl@0
   305
  do_test vacuum-9.1 {
sl@0
   306
    execsql {
sl@0
   307
      DROP TABLE 'abc abc';
sl@0
   308
      CREATE TABLE autoinc(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
sl@0
   309
      INSERT INTO autoinc(b) VALUES('hi');
sl@0
   310
      INSERT INTO autoinc(b) VALUES('there');
sl@0
   311
      DELETE FROM autoinc;
sl@0
   312
    }
sl@0
   313
    set ::cksum [cksum]
sl@0
   314
    expr {$::cksum!=""}
sl@0
   315
  } {1}
sl@0
   316
  do_test vacuum-9.2 {
sl@0
   317
    execsql {
sl@0
   318
      VACUUM;
sl@0
   319
    }
sl@0
   320
    cksum
sl@0
   321
  } $::cksum
sl@0
   322
  do_test vacuum-9.3 {
sl@0
   323
    execsql {
sl@0
   324
      INSERT INTO autoinc(b) VALUES('one');
sl@0
   325
      INSERT INTO autoinc(b) VALUES('two');
sl@0
   326
    }
sl@0
   327
    set ::cksum [cksum]
sl@0
   328
    expr {$::cksum!=""}
sl@0
   329
  } {1}
sl@0
   330
  do_test vacuum-9.4 {
sl@0
   331
    execsql {
sl@0
   332
      VACUUM;
sl@0
   333
    }
sl@0
   334
    cksum
sl@0
   335
  } $::cksum
sl@0
   336
}
sl@0
   337
sl@0
   338
file delete -force {a'z.db}
sl@0
   339
sl@0
   340
finish_test