[sql] Import reference version of SQLite 3.7.6.3.
[chromium-blink-merge.git] / third_party / sqlite / sqlite-src-3070603 / test / vacuum2.test
blob35524bb26c2d4b76d87fe28aa9a98833f3a7eb75
1 # 2005 February 15
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
19 # Do not use a codec for tests in this file, as the database file is
20 # manipulated directly using tcl scripts (using the [hexio_write] command).
22 do_not_use_codec
24 # If the VACUUM statement is disabled in the current build, skip all
25 # the tests in this file.
27 ifcapable {!vacuum||!autoinc} {
28   finish_test
29   return
31 if $AUTOVACUUM {
32   finish_test
33   return
36 # Ticket #1121 - make sure vacuum works if all autoincrement tables
37 # have been deleted.
39 do_test vacuum2-1.1 {
40   execsql {
41     CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
42     DROP TABLE t1;
43     VACUUM;
44   }
45 } {}
47 # Ticket #2518.  Make sure vacuum increments the change counter
48 # in the database header.
50 do_test vacuum2-2.1 {
51   execsql {
52     CREATE TABLE t1(x);
53     CREATE TABLE t2(y);
54     INSERT INTO t1 VALUES(1);
55   }
56   hexio_get_int [hexio_read test.db 24 4]
57 } [expr {[hexio_get_int [hexio_read test.db 24 4]]+3}]
58 do_test vacuum2-2.1 {
59   execsql {
60     VACUUM
61   }
62   hexio_get_int [hexio_read test.db 24 4]
63 } [expr {[hexio_get_int [hexio_read test.db 24 4]]+1}]
65 ############################################################################
66 # Verify that we can use the auto_vacuum pragma to request a new
67 # autovacuum setting, do a VACUUM, and the new setting takes effect.
68 # Make sure this happens correctly even if there are multiple open
69 # connections to the same database file.
71 sqlite3 db2 test.db
72 set pageSize [db eval {pragma page_size}]
74 # We are currently not autovacuuming so the database should be 3 pages
75 # in size.  1 page for each of sqlite_master, t1, and t2.
77 do_test vacuum2-3.1 {
78   execsql {
79     INSERT INTO t1 VALUES('hello');
80     INSERT INTO t2 VALUES('out there');
81   }
82   expr {[file size test.db]/$pageSize}
83 } {3}
84 set cksum [cksum]
85 do_test vacuum2-3.2 {
86   cksum db2
87 } $cksum
89 # Convert the database to an autovacuumed database.
90 ifcapable autovacuum {
91   do_test vacuum2-3.3 {
92     execsql {
93       PRAGMA auto_vacuum=FULL;
94       VACUUM;
95     }
96     expr {[file size test.db]/$pageSize}
97   } {4}
99 do_test vacuum2-3.4 {
100   cksum db2
101 } $cksum
102 do_test vacuum2-3.5 {
103   cksum
104 } $cksum
105 do_test vacuum2-3.6 {
106   execsql {PRAGMA integrity_check} db2
107 } {ok}
108 do_test vacuum2-3.7 {
109   execsql {PRAGMA integrity_check} db
110 } {ok}
112 # Convert the database back to a non-autovacuumed database.
113 do_test vacuum2-3.13 {
114   execsql {
115     PRAGMA auto_vacuum=NONE;
116     VACUUM;
117   }
118   expr {[file size test.db]/$pageSize}
119 } {3}
120 do_test vacuum2-3.14 {
121   cksum db2
122 } $cksum
123 do_test vacuum2-3.15 {
124   cksum
125 } $cksum
126 do_test vacuum2-3.16 {
127   execsql {PRAGMA integrity_check} db2
128 } {ok}
129 do_test vacuum2-3.17 {
130   execsql {PRAGMA integrity_check} db
131 } {ok}
133 db2 close
135 ifcapable autovacuum {
136   do_test vacuum2-4.1 {
137     db close
138     file delete -force test.db
139     sqlite3 db test.db
140     execsql {
141       pragma auto_vacuum=1;
142       create table t(a, b);
143       insert into t values(1, 2);
144       insert into t values(1, 2);
145       pragma auto_vacuum=0;
146       vacuum;
147       pragma auto_vacuum;
148     }
149   } {0}
150   do_test vacuum2-4.2 {
151     execsql {
152       pragma auto_vacuum=1;
153       vacuum;
154       pragma auto_vacuum;
155     }
156   } {1}
157   do_test vacuum2-4.3 {
158     execsql {
159       pragma integrity_check
160     }
161   } {ok}
162   do_test vacuum2-4.4 {
163     db close
164     sqlite3 db test.db
165     execsql {
166       pragma auto_vacuum;
167     }
168   } {1}
169   do_test vacuum2-4.5 {  # Ticket #3663
170     execsql {
171       pragma auto_vacuum=2;
172       vacuum;
173       pragma auto_vacuum;
174     }
175   } {2}
176   do_test vacuum2-4.6 {
177     execsql {
178       pragma integrity_check
179     }
180   } {ok}
181   do_test vacuum2-4.7 {
182     db close
183     sqlite3 db test.db
184     execsql {
185       pragma auto_vacuum;
186     }
187   } {2}
191 #-------------------------------------------------------------------------
192 # The following block of tests verify the behaviour of the library when
193 # a database is VACUUMed when there are one or more unfinalized SQL 
194 # statements reading the same database using the same db handle.
196 db close
197 forcedelete test.db
198 sqlite3 db test.db
199 do_execsql_test vacuum2-5.1 {
200   CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
201   INSERT INTO t1 VALUES(1, randomblob(500));
202   INSERT INTO t1 SELECT a+1, randomblob(500) FROM t1;      -- 2
203   INSERT INTO t1 SELECT a+2, randomblob(500) FROM t1;      -- 4 
204   INSERT INTO t1 SELECT a+4, randomblob(500) FROM t1;      -- 8 
205   INSERT INTO t1 SELECT a+8, randomblob(500) FROM t1;      -- 16 
206 } {}
208 do_test vacuum2-5.2 {
209   list [catch {
210     db eval {SELECT a, b FROM t1} { if {$a == 8} { execsql VACUUM } }
211   } msg] $msg
212 } {1 {cannot VACUUM - SQL statements in progress}}
214 do_test vacuum2-5.3 {
215   list [catch {
216     db eval {SELECT 1, 2, 3} { execsql VACUUM }
217   } msg] $msg
218 } {1 {cannot VACUUM - SQL statements in progress}}
220 do_test vacuum2-5.4 {
221   set res ""
222   set res2 ""
223   db eval {SELECT a, b FROM t1 WHERE a<=10} {
224     if {$a==6} { set res [catchsql VACUUM] }
225     lappend res2 $a
226   }
227   lappend res2 $res
228 } {1 2 3 4 5 6 7 8 9 10 {1 {cannot VACUUM - SQL statements in progress}}}
231 finish_test