summaryrefslogtreecommitdiffstats
path: root/test/vacuum.test
blob: 57429c29ea5f867d6e63c6272d3c05e15bd4cf14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# 2001 September 15
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is testing the VACUUM statement.
#
# $Id: vacuum.test,v 1.43 2009/01/31 14:54:07 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# If the VACUUM statement is disabled in the current build, skip all
# the tests in this file.
#
ifcapable {!vacuum} {
  omit_test vacuum.test {Compiled with SQLITE_OMIT_VACUUM}
  finish_test
  return
}
if $AUTOVACUUM {
  omit_test vacuum.test {Auto-vacuum is enabled}
  finish_test
  return
}

set fcnt 1
do_test vacuum-1.1 {
  execsql {
    BEGIN;
    CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
    INSERT INTO t1 VALUES(NULL,randstr(10,100),randstr(5,50));
    INSERT INTO t1 VALUES(123456,randstr(10,100),randstr(5,50));
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
    INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
    CREATE INDEX i1 ON t1(b,c);
    CREATE UNIQUE INDEX i2 ON t1(c,a);
    CREATE TABLE t2 AS SELECT * FROM t1;
    COMMIT;
    DROP TABLE t2;
  }
  set ::size1 [file size test.db]
  set ::cksum [cksum]
  expr {$::cksum!=""}
} {1}

# Create bogus application-defined functions for functions used 
# internally by VACUUM, to ensure that VACUUM falls back
# to the built-in functions.
#
proc failing_app_func {args} {error "bad function"}
do_test vacuum-1.1b {
  db func substr failing_app_func
  db func like failing_app_func
  db func quote failing_app_func
  catchsql {SELECT substr(name,1,3) FROM sqlite_master}
} {1 {bad function}}

do_test vacuum-1.2 {
  execsql {
    VACUUM;
  }
  cksum
} $cksum
ifcapable vacuum {
  do_test vacuum-1.3 {
    expr {[file size test.db]<$::size1}
  } {1}
}
do_test vacuum-1.4 {
  set sql_script {
    BEGIN;
    CREATE TABLE t2 AS SELECT * FROM t1;
    CREATE TABLE t3 AS SELECT * FROM t1;
    CREATE VIEW v1 AS SELECT b, c FROM t3;
    CREATE TRIGGER r1 AFTER DELETE ON t2 BEGIN SELECT 1; END;
    COMMIT;
    DROP TABLE t2;
  }
  # If the library was compiled to omit view support, comment out the
  # create view in the script $sql_script before executing it. Similarly,
  # if triggers are not supported, comment out the trigger definition.
  ifcapable !view {
    regsub {CREATE VIEW} $sql_script {-- CREATE VIEW} sql_script
  }
  ifcapable !trigger {
    regsub {CREATE TRIGGER} $sql_script {-- CREATE TRIGGER} sql_script
  }
  execsql $sql_script
  set ::size1 [file size test.db]
  set ::cksum [cksum]
  expr {$::cksum!=""}
} {1}
do_test vacuum-1.5 {
  execsql {
    VACUUM;
  }
  cksum
} $cksum

ifcapable vacuum {
  do_test vacuum-1.6 {
    expr {[file size test.db]<$::size1}
  } {1}
}
ifcapable vacuum {
  do_test vacuum-2.1.1 {
    catchsql {
      BEGIN;
      VACUUM;
    }
  } {1 {cannot VACUUM from within a transaction}}
  do_test vacuum-2.1.2 {
    sqlite3_get_autocommit db
  } {0}
  do_test vacuum-2.1.3 {
    db eval {COMMIT}
  } {}
}
do_test vacuum-2.2 {
  sqlite3 db2 test.db
  execsql {
    BEGIN;
    CREATE TABLE t4 AS SELECT * FROM t1;
    CREATE TABLE t5 AS SELECT * FROM t1;
    COMMIT;
    DROP TABLE t4;
    DROP TABLE t5;
  } db2
  set ::cksum [cksum db2]
  catchsql {
    VACUUM
  }
} {0 {}}
do_test vacuum-2.3 {
  cksum
} $cksum
do_test vacuum-2.4 {
  catch {db2 eval {SELECT count(*) FROM sqlite_master}}
  cksum db2
} $cksum

# Make sure the schema cookie is incremented by vacuum.
#
do_test vacuum-2.5 {
  execsql {
    BEGIN;
    CREATE TABLE t6 AS SELECT * FROM t1;
    CREATE TABLE t7 AS SELECT * FROM t1;
    COMMIT;
  }
  sqlite3 db3 test.db
  execsql {
    -- The "SELECT * FROM sqlite_master" statement ensures that this test
    -- works when shared-cache is enabled. If shared-cache is enabled, then
    -- db3 shares a cache with db2 (but not db - it was opened as 
    -- "./test.db").
    SELECT * FROM sqlite_master;
    SELECT * FROM t7 LIMIT 1
  } db3
  execsql {
    VACUUM;
  }
  execsql {
    INSERT INTO t7 VALUES(1234567890,'hello','world');
  } db3
  execsql {
    SELECT * FROM t7 WHERE a=1234567890
  }
} {1234567890 hello world}
integrity_check vacuum-2.6
do_test vacuum-2.7 {
  execsql {
    SELECT * FROM t7 WHERE a=1234567890
  } db3
} {1234567890 hello world}
do_test vacuum-2.8 {
  execsql {
    INSERT INTO t7 SELECT * FROM t6;
    SELECT count(*) FROM t7;
  }
} 513
integrity_check vacuum-2.9
do_test vacuum-2.10 {
  execsql {
    DELETE FROM t7;
    SELECT count(*) FROM t7;
  } db3
} 0
integrity_check vacuum-2.11
db3 close
 

# Ticket #427.  Make sure VACUUM works when the EMPTY_RESULT_CALLBACKS
# pragma is turned on.
#
do_test vacuum-3.1 {
  db close
  db2 close
  delete_file test.db
  sqlite3 db test.db
  execsql {
    PRAGMA empty_result_callbacks=on;
    VACUUM;
  }
} {}

# Ticket #464.  Make sure VACUUM works with the sqlite3_prepare() API.
#
do_test vacuum-4.1 {
  db close
  sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
  set VM [sqlite3_prepare $DB {VACUUM} -1 TAIL]
  sqlite3_step $VM
} {SQLITE_DONE}
do_test vacuum-4.2 {
  sqlite3_finalize $VM
} SQLITE_OK

# Ticket #515.  VACUUM after deleting and recreating the table that
# a view refers to. Omit this test if the library is not view-enabled.
#
ifcapable view {
do_test vacuum-5.1 {
  db close
  forcedelete test.db
  sqlite3 db test.db
  catchsql {
    CREATE TABLE Test (TestID int primary key);
    INSERT INTO Test VALUES (NULL);
    CREATE VIEW viewTest AS SELECT * FROM Test;

    BEGIN;
    CREATE TABLE tempTest (TestID int primary key, Test2 int NULL);
    INSERT INTO tempTest SELECT TestID, 1 FROM Test;
    DROP TABLE Test;
    CREATE TABLE Test(TestID int primary key, Test2 int NULL);
    INSERT INTO Test SELECT * FROM tempTest;
    DROP TABLE tempTest;
    COMMIT;
    VACUUM;
  }
} {0 {}}
do_test vacuum-5.2 {
  catchsql {
    VACUUM;
  }
} {0 {}}
} ;# ifcapable view

# Ensure vacuum works with complicated tables names.
do_test vacuum-6.1 {
  execsql {
    CREATE TABLE "abc abc"(a, b, c);
    INSERT INTO "abc abc" VALUES(1, 2, 3);
    VACUUM;
  }
} {}
do_test vacuum-6.2 {
  execsql {
    select * from "abc abc";
  }
} {1 2 3}

# Also ensure that blobs survive a vacuum.
ifcapable {bloblit} {
  do_test vacuum-6.3 {
    execsql {
      DELETE FROM "abc abc";
      INSERT INTO "abc abc" VALUES(X'00112233', NULL, NULL);
      VACUUM;
    }
  } {}
  do_test vacuum-6.4 {
    execsql {
      select count(*) from "abc abc" WHERE a = X'00112233';
    }
  } {1}
}

# Check what happens when an in-memory database is vacuumed. The
# [delete_file] command covers us in case the library was compiled
# without in-memory database support.
#
forcedelete :memory:
do_test vacuum-7.0 {
  sqlite3 db2 :memory:
  execsql {
    CREATE TABLE t1(t);
    VACUUM;
  } db2
} {}
do_test vacuum-7.1 {
  execsql {
    CREATE TABLE t2(t);
    CREATE TABLE t3(t);
    DROP TABLE t2;
    PRAGMA freelist_count;
  }
} {1}
do_test vacuum-7.2 {
  execsql {
    VACUUM;
    pragma integrity_check;
  } db2
} {ok}
do_test vacuum-7.3 {
  execsql { PRAGMA freelist_count; } db2
} {0}
ifcapable autovacuum {
  do_test vacuum-7.4 {
    execsql { PRAGMA auto_vacuum } db2
  } {0}
  do_test vacuum-7.5 {
    execsql { PRAGMA auto_vacuum = 1} db2
    execsql { PRAGMA auto_vacuum } db2
  } {0}
  do_test vacuum-7.6 {
    execsql { PRAGMA auto_vacuum = 1} db2
    execsql { VACUUM } db2
    execsql { PRAGMA auto_vacuum } db2
  } {1}
}
db2 close

# Ticket #873.  VACUUM a database that has ' in its name.
#
do_test vacuum-8.1 {
  forcedelete a'z.db
  forcedelete a'z.db-journal
  sqlite3 db2 a'z.db
  execsql {
    CREATE TABLE t1(t);
    VACUUM;
  } db2
} {}
db2 close

# Ticket #1095:  Vacuum a table that uses AUTOINCREMENT
#
ifcapable {autoinc} {
  do_test vacuum-9.1 {
    execsql {
      DROP TABLE 'abc abc';
      CREATE TABLE autoinc(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
      INSERT INTO autoinc(b) VALUES('hi');
      INSERT INTO autoinc(b) VALUES('there');
      DELETE FROM autoinc;
    }
    set ::cksum [cksum]
    expr {$::cksum!=""}
  } {1}
  do_test vacuum-9.2 {
    execsql {
      VACUUM;
    }
    cksum
  } $::cksum
  do_test vacuum-9.3 {
    execsql {
      INSERT INTO autoinc(b) VALUES('one');
      INSERT INTO autoinc(b) VALUES('two');
    }
    set ::cksum [cksum]
    expr {$::cksum!=""}
  } {1}
  do_test vacuum-9.4 {
    execsql {
      VACUUM;
    }
    cksum
  } $::cksum
}

forcedelete {a'z.db}

# Test that "PRAGMA count_changes" does not interfere with VACUUM or cause
# it to return any rows to the user.
#
do_test vacuum-10.1 {
  db close
  forcedelete test.db
  sqlite3 db test.db
  execsql {
    CREATE TABLE t8(a, b);
    INSERT INTO t8 VALUES('a', 'b');
    INSERT INTO t8 VALUES('c', 'd');
    PRAGMA count_changes = 1;
  }
} {}
do_test vacuum-10.2 { execsql VACUUM } {}

finish_test