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: vacuum2.test,v 1.10 2009/02/18 20:31:18 drh Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 set testprefix vacuum2
20 # Do not use a codec for tests in this file, as the database file is
21 # manipulated directly using tcl scripts (using the [hexio_write] command).
25 # If the VACUUM statement is disabled in the current build, skip all
26 # the tests in this file.
28 ifcapable {!vacuum||!autoinc} {
37 # Ticket #1121 - make sure vacuum works if all autoincrement tables
42 CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
48 # Ticket #2518. Make sure vacuum increments the change counter
49 # in the database header.
55 INSERT INTO t1 VALUES(1);
57 hexio_get_int [hexio_read test.db 24 4]
58 } [expr {[hexio_get_int [hexio_read test.db 24 4]]+3}]
63 hexio_get_int [hexio_read test.db 24 4]
64 } [expr {[hexio_get_int [hexio_read test.db 24 4]]+1}]
66 ############################################################################
67 # Verify that we can use the auto_vacuum pragma to request a new
68 # autovacuum setting, do a VACUUM, and the new setting takes effect.
69 # Make sure this happens correctly even if there are multiple open
70 # connections to the same database file.
73 set pageSize [db eval {pragma page_size}]
75 # We are currently not autovacuuming so the database should be 3 pages
76 # in size. 1 page for each of sqlite_master, t1, and t2.
80 INSERT INTO t1 VALUES('hello');
81 INSERT INTO t2 VALUES('out there');
83 expr {[file size test.db]/$pageSize}
90 # Convert the database to an autovacuumed database.
91 ifcapable autovacuum {
94 PRAGMA auto_vacuum=FULL;
97 expr {[file size test.db]/$pageSize}
100 do_test vacuum2-3.4 {
103 do_test vacuum2-3.5 {
106 do_test vacuum2-3.6 {
107 execsql {PRAGMA integrity_check} db2
109 do_test vacuum2-3.7 {
110 execsql {PRAGMA integrity_check} db
113 # Convert the database back to a non-autovacuumed database.
114 do_test vacuum2-3.13 {
116 PRAGMA auto_vacuum=NONE;
119 expr {[file size test.db]/$pageSize}
121 do_test vacuum2-3.14 {
124 do_test vacuum2-3.15 {
127 do_test vacuum2-3.16 {
128 execsql {PRAGMA integrity_check} db2
130 do_test vacuum2-3.17 {
131 execsql {PRAGMA integrity_check} db
136 ifcapable autovacuum {
137 do_test vacuum2-4.1 {
142 pragma auto_vacuum=1;
143 create table t(a, b);
144 insert into t values(1, 2);
145 insert into t values(1, 2);
146 pragma auto_vacuum=0;
151 do_test vacuum2-4.2 {
153 pragma auto_vacuum=1;
158 do_test vacuum2-4.3 {
160 pragma integrity_check
163 do_test vacuum2-4.4 {
170 do_test vacuum2-4.5 { # Ticket #3663
172 pragma auto_vacuum=2;
177 do_test vacuum2-4.6 {
179 pragma integrity_check
182 do_test vacuum2-4.7 {
192 #-------------------------------------------------------------------------
193 # The following block of tests verify the behaviour of the library when
194 # a database is VACUUMed when there are one or more unfinalized SQL
195 # statements reading the same database using the same db handle.
200 do_execsql_test vacuum2-5.1 {
201 CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
202 INSERT INTO t1 VALUES(1, randomblob(500));
203 INSERT INTO t1 SELECT a+1, randomblob(500) FROM t1; -- 2
204 INSERT INTO t1 SELECT a+2, randomblob(500) FROM t1; -- 4
205 INSERT INTO t1 SELECT a+4, randomblob(500) FROM t1; -- 8
206 INSERT INTO t1 SELECT a+8, randomblob(500) FROM t1; -- 16
209 do_test vacuum2-5.2 {
211 db eval {SELECT a, b FROM t1} { if {$a == 8} { execsql VACUUM } }
213 } {1 {cannot VACUUM - SQL statements in progress}}
215 do_test vacuum2-5.3 {
217 db eval {SELECT 1, 2, 3} { execsql VACUUM }
219 } {1 {cannot VACUUM - SQL statements in progress}}
221 do_test vacuum2-5.4 {
224 db eval {SELECT a, b FROM t1 WHERE a<=10} {
225 if {$a==6} { set res [catchsql VACUUM] }
229 } {1 2 3 4 5 6 7 8 9 10 {1 {cannot VACUUM - SQL statements in progress}}}
231 #-------------------------------------------------------------------------
232 # Check that if the definition of a collation sequence is changed and
233 # VACUUM run, records are store in the (new) correct order following the
234 # VACUUM. Even if the modified collation is attached to a PK of a WITHOUT
237 proc cmp {lhs rhs} { string compare $lhs $rhs }
239 do_execsql_test 6.0 {
240 CREATE TABLE t6(x PRIMARY KEY COLLATE cmp, y) WITHOUT ROWID;
241 CREATE INDEX t6y ON t6(y);
242 INSERT INTO t6 VALUES('i', 'one');
243 INSERT INTO t6 VALUES('ii', 'one');
244 INSERT INTO t6 VALUES('iii', 'one');
247 proc cmp {lhs rhs} { string compare $rhs $lhs }
248 do_execsql_test 6.2 VACUUM