3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the VACUUM statement.
14 # $Id: vacuum.test,v 1.43 2009/01/31 14:54:07 danielk1977 Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # If the VACUUM statement is disabled in the current build, skip all
20 # the tests in this file.
23 omit_test vacuum.test {Compiled with SQLITE_OMIT_VACUUM}
28 omit_test vacuum.test {Auto-vacuum is enabled}
37 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
38 INSERT INTO t1 VALUES(NULL,randstr(10,100),randstr(5,50));
39 INSERT INTO t1 VALUES(123456,randstr(10,100),randstr(5,50));
40 INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
41 INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
42 INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
43 INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
44 INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
45 INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
46 INSERT INTO t1 SELECT NULL, b||'-'||rowid, c||'-'||rowid FROM t1;
47 CREATE INDEX i1 ON t1(b,c);
48 CREATE UNIQUE INDEX i2 ON t1(c,a);
49 CREATE TABLE t2 AS SELECT * FROM t1;
53 set ::size1 [file size test.db]
58 # Create bogus application-defined functions for functions used
59 # internally by VACUUM, to ensure that VACUUM falls back
60 # to the built-in functions.
62 proc failing_app_func {args} {error "bad function"}
64 db func substr failing_app_func
65 db func like failing_app_func
66 db func quote failing_app_func
67 catchsql {SELECT substr(name,1,3) FROM sqlite_master}
78 expr {[file size test.db]<$::size1}
84 CREATE TABLE t2 AS SELECT * FROM t1;
85 CREATE TABLE t3 AS SELECT * FROM t1;
86 CREATE VIEW v1 AS SELECT b, c FROM t3;
87 CREATE TRIGGER r1 AFTER DELETE ON t2 BEGIN SELECT 1; END;
91 # If the library was compiled to omit view support, comment out the
92 # create view in the script $sql_script before executing it. Similarly,
93 # if triggers are not supported, comment out the trigger definition.
95 regsub {CREATE VIEW} $sql_script {-- CREATE VIEW} sql_script
98 regsub {CREATE TRIGGER} $sql_script {-- CREATE TRIGGER} sql_script
101 set ::size1 [file size test.db]
114 expr {[file size test.db]<$::size1}
118 do_test vacuum-2.1.1 {
123 } {1 {cannot VACUUM from within a transaction}}
124 do_test vacuum-2.1.2 {
125 sqlite3_get_autocommit db
127 do_test vacuum-2.1.3 {
135 CREATE TABLE t4 AS SELECT * FROM t1;
136 CREATE TABLE t5 AS SELECT * FROM t1;
141 set ::cksum [cksum db2]
150 catch {db2 eval {SELECT count(*) FROM sqlite_master}}
154 # Make sure the schema cookie is incremented by vacuum.
159 CREATE TABLE t6 AS SELECT * FROM t1;
160 CREATE TABLE t7 AS SELECT * FROM t1;
165 -- The "SELECT * FROM sqlite_master" statement ensures that this test
166 -- works when shared-cache is enabled. If shared-cache is enabled, then
167 -- db3 shares a cache with db2 (but not db - it was opened as
169 SELECT * FROM sqlite_master;
170 SELECT * FROM t7 LIMIT 1
176 INSERT INTO t7 VALUES(1234567890,'hello','world');
179 SELECT * FROM t7 WHERE a=1234567890
181 } {1234567890 hello world}
182 integrity_check vacuum-2.6
185 SELECT * FROM t7 WHERE a=1234567890
187 } {1234567890 hello world}
190 INSERT INTO t7 SELECT * FROM t6;
191 SELECT count(*) FROM t7;
194 integrity_check vacuum-2.9
195 do_test vacuum-2.10 {
198 SELECT count(*) FROM t7;
201 integrity_check vacuum-2.11
205 # Ticket #427. Make sure VACUUM works when the EMPTY_RESULT_CALLBACKS
206 # pragma is turned on.
214 PRAGMA empty_result_callbacks=on;
219 # Ticket #464. Make sure VACUUM works with the sqlite3_prepare() API.
223 sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
224 set VM [sqlite3_prepare $DB {VACUUM} -1 TAIL]
231 # Ticket #515. VACUUM after deleting and recreating the table that
232 # a view refers to. Omit this test if the library is not view-enabled.
240 CREATE TABLE Test (TestID int primary key);
241 INSERT INTO Test VALUES (NULL);
242 CREATE VIEW viewTest AS SELECT * FROM Test;
245 CREATE TABLE tempTest (TestID int primary key, Test2 int NULL);
246 INSERT INTO tempTest SELECT TestID, 1 FROM Test;
248 CREATE TABLE Test(TestID int primary key, Test2 int NULL);
249 INSERT INTO Test SELECT * FROM tempTest;
262 # Ensure vacuum works with complicated tables names.
265 CREATE TABLE "abc abc"(a, b, c);
266 INSERT INTO "abc abc" VALUES(1, 2, 3);
272 select * from "abc abc";
276 # Also ensure that blobs survive a vacuum.
277 ifcapable {bloblit} {
280 DELETE FROM "abc abc";
281 INSERT INTO "abc abc" VALUES(X'00112233', NULL, NULL);
287 select count(*) from "abc abc" WHERE a = X'00112233';
292 # Check what happens when an in-memory database is vacuumed. The
293 # [delete_file] command covers us in case the library was compiled
294 # without in-memory database support.
309 PRAGMA freelist_count;
315 pragma integrity_check;
319 execsql { PRAGMA freelist_count; } db2
321 ifcapable autovacuum {
323 execsql { PRAGMA auto_vacuum } db2
326 execsql { PRAGMA auto_vacuum = 1} db2
327 execsql { PRAGMA auto_vacuum } db2
330 execsql { PRAGMA auto_vacuum = 1} db2
331 execsql { VACUUM } db2
332 execsql { PRAGMA auto_vacuum } db2
337 # Ticket #873. VACUUM a database that has ' in its name.
341 forcedelete a'z.db-journal
350 # Ticket #1095: Vacuum a table that uses AUTOINCREMENT
352 ifcapable {autoinc} {
355 DROP TABLE 'abc abc';
356 CREATE TABLE autoinc(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
357 INSERT INTO autoinc(b) VALUES('hi');
358 INSERT INTO autoinc(b) VALUES('there');
372 INSERT INTO autoinc(b) VALUES('one');
373 INSERT INTO autoinc(b) VALUES('two');
388 # Test that "PRAGMA count_changes" does not interfere with VACUUM or cause
389 # it to return any rows to the user.
391 do_test vacuum-10.1 {
396 CREATE TABLE t8(a, b);
397 INSERT INTO t8 VALUES('a', 'b');
398 INSERT INTO t8 VALUES('c', 'd');
399 PRAGMA count_changes = 1;
402 do_test vacuum-10.2 { execsql VACUUM } {}