sl@0
|
1 |
# 2007 April 26
|
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 incremental vacuum feature.
|
sl@0
|
13 |
#
|
sl@0
|
14 |
# Note: There are also some tests for incremental vacuum and IO
|
sl@0
|
15 |
# errors in incrvacuum_ioerr.test.
|
sl@0
|
16 |
#
|
sl@0
|
17 |
# $Id: incrvacuum.test,v 1.20 2008/09/10 10:57:28 danielk1977 Exp $
|
sl@0
|
18 |
|
sl@0
|
19 |
set testdir [file dirname $argv0]
|
sl@0
|
20 |
source $testdir/tester.tcl
|
sl@0
|
21 |
|
sl@0
|
22 |
# If this build of the library does not support auto-vacuum, omit this
|
sl@0
|
23 |
# whole file.
|
sl@0
|
24 |
ifcapable {!autovacuum || !pragma} {
|
sl@0
|
25 |
finish_test
|
sl@0
|
26 |
return
|
sl@0
|
27 |
}
|
sl@0
|
28 |
|
sl@0
|
29 |
#---------------------------------------------------------------------
|
sl@0
|
30 |
# Test the pragma on an empty database.
|
sl@0
|
31 |
#
|
sl@0
|
32 |
do_test incrvacuum-1.1 {
|
sl@0
|
33 |
execsql {
|
sl@0
|
34 |
pragma auto_vacuum;
|
sl@0
|
35 |
}
|
sl@0
|
36 |
} $sqlite_options(default_autovacuum)
|
sl@0
|
37 |
do_test incrvacuum-1.2.0 {
|
sl@0
|
38 |
# File size is sometimes 1 instead of 0 due to the hack we put in
|
sl@0
|
39 |
# to work around ticket #3260. Search for comments on #3260 in
|
sl@0
|
40 |
# os_unix.c.
|
sl@0
|
41 |
expr {[file size test.db] > 1}
|
sl@0
|
42 |
} {0}
|
sl@0
|
43 |
do_test incrvacuum-1.2 {
|
sl@0
|
44 |
# This command will create the database.
|
sl@0
|
45 |
execsql {
|
sl@0
|
46 |
pragma auto_vacuum = 'full';
|
sl@0
|
47 |
pragma auto_vacuum;
|
sl@0
|
48 |
}
|
sl@0
|
49 |
} {1}
|
sl@0
|
50 |
do_test incrvacuum-1.2.1 {
|
sl@0
|
51 |
expr {[file size test.db] > 0}
|
sl@0
|
52 |
} {1}
|
sl@0
|
53 |
do_test incrvacuum-1.3 {
|
sl@0
|
54 |
execsql {
|
sl@0
|
55 |
pragma auto_vacuum = 'incremental';
|
sl@0
|
56 |
pragma auto_vacuum;
|
sl@0
|
57 |
}
|
sl@0
|
58 |
} {2}
|
sl@0
|
59 |
do_test incrvacuum-1.4 {
|
sl@0
|
60 |
# In this case the invalid value is ignored and the auto_vacuum
|
sl@0
|
61 |
# setting remains unchanged.
|
sl@0
|
62 |
execsql {
|
sl@0
|
63 |
pragma auto_vacuum = 'invalid';
|
sl@0
|
64 |
pragma auto_vacuum;
|
sl@0
|
65 |
}
|
sl@0
|
66 |
} {2}
|
sl@0
|
67 |
do_test incrvacuum-1.5 {
|
sl@0
|
68 |
execsql {
|
sl@0
|
69 |
pragma auto_vacuum = 1;
|
sl@0
|
70 |
pragma auto_vacuum;
|
sl@0
|
71 |
}
|
sl@0
|
72 |
} {1}
|
sl@0
|
73 |
do_test incrvacuum-1.6 {
|
sl@0
|
74 |
execsql {
|
sl@0
|
75 |
pragma auto_vacuum = '2';
|
sl@0
|
76 |
pragma auto_vacuum;
|
sl@0
|
77 |
}
|
sl@0
|
78 |
} {2}
|
sl@0
|
79 |
do_test incrvacuum-1.7 {
|
sl@0
|
80 |
# Invalid value. auto_vacuum setting remains unchanged.
|
sl@0
|
81 |
execsql {
|
sl@0
|
82 |
pragma auto_vacuum = 5;
|
sl@0
|
83 |
pragma auto_vacuum;
|
sl@0
|
84 |
}
|
sl@0
|
85 |
} {2}
|
sl@0
|
86 |
|
sl@0
|
87 |
#---------------------------------------------------------------------
|
sl@0
|
88 |
# Test the pragma on a non-empty database. It is possible to toggle
|
sl@0
|
89 |
# the connection between "full" and "incremental" mode, but not to
|
sl@0
|
90 |
# change from either of these to "none", or from "none" to "full" or
|
sl@0
|
91 |
# "incremental".
|
sl@0
|
92 |
#
|
sl@0
|
93 |
do_test incrvacuum-2.1 {
|
sl@0
|
94 |
execsql {
|
sl@0
|
95 |
pragma auto_vacuum = 1;
|
sl@0
|
96 |
CREATE TABLE abc(a, b, c);
|
sl@0
|
97 |
}
|
sl@0
|
98 |
} {}
|
sl@0
|
99 |
do_test incrvacuum-2.2 {
|
sl@0
|
100 |
execsql {
|
sl@0
|
101 |
pragma auto_vacuum = 'none';
|
sl@0
|
102 |
pragma auto_vacuum;
|
sl@0
|
103 |
}
|
sl@0
|
104 |
} {1}
|
sl@0
|
105 |
do_test incrvacuum-2.2.1 {
|
sl@0
|
106 |
db close
|
sl@0
|
107 |
sqlite3 db test.db
|
sl@0
|
108 |
execsql {
|
sl@0
|
109 |
pragma auto_vacuum;
|
sl@0
|
110 |
}
|
sl@0
|
111 |
} {1}
|
sl@0
|
112 |
do_test incrvacuum-2.3 {
|
sl@0
|
113 |
execsql {
|
sl@0
|
114 |
pragma auto_vacuum = 'incremental';
|
sl@0
|
115 |
pragma auto_vacuum;
|
sl@0
|
116 |
}
|
sl@0
|
117 |
} {2}
|
sl@0
|
118 |
do_test incrvacuum-2.4 {
|
sl@0
|
119 |
execsql {
|
sl@0
|
120 |
pragma auto_vacuum = 'full';
|
sl@0
|
121 |
pragma auto_vacuum;
|
sl@0
|
122 |
}
|
sl@0
|
123 |
} {1}
|
sl@0
|
124 |
|
sl@0
|
125 |
#---------------------------------------------------------------------
|
sl@0
|
126 |
# Test that when the auto_vacuum mode is "incremental", the database
|
sl@0
|
127 |
# does not shrink when pages are removed from it. But it does if
|
sl@0
|
128 |
# the mode is set to "full".
|
sl@0
|
129 |
#
|
sl@0
|
130 |
do_test incrvacuum-3.1 {
|
sl@0
|
131 |
execsql {
|
sl@0
|
132 |
pragma auto_vacuum;
|
sl@0
|
133 |
}
|
sl@0
|
134 |
} {1}
|
sl@0
|
135 |
do_test incrvacuum-3.2 {
|
sl@0
|
136 |
set ::str [string repeat 1234567890 110]
|
sl@0
|
137 |
execsql {
|
sl@0
|
138 |
PRAGMA auto_vacuum = 2;
|
sl@0
|
139 |
BEGIN;
|
sl@0
|
140 |
CREATE TABLE tbl2(str);
|
sl@0
|
141 |
INSERT INTO tbl2 VALUES($::str);
|
sl@0
|
142 |
COMMIT;
|
sl@0
|
143 |
}
|
sl@0
|
144 |
# 5 pages:
|
sl@0
|
145 |
#
|
sl@0
|
146 |
# 1 -> database header
|
sl@0
|
147 |
# 2 -> first back-pointer page
|
sl@0
|
148 |
# 3 -> table abc
|
sl@0
|
149 |
# 4 -> table tbl2
|
sl@0
|
150 |
# 5 -> table tbl2 overflow page.
|
sl@0
|
151 |
#
|
sl@0
|
152 |
expr {[file size test.db] / 1024}
|
sl@0
|
153 |
} {5}
|
sl@0
|
154 |
do_test incrvacuum-3.3 {
|
sl@0
|
155 |
execsql {
|
sl@0
|
156 |
DROP TABLE abc;
|
sl@0
|
157 |
DELETE FROM tbl2;
|
sl@0
|
158 |
}
|
sl@0
|
159 |
expr {[file size test.db] / 1024}
|
sl@0
|
160 |
} {5}
|
sl@0
|
161 |
do_test incrvacuum-3.4 {
|
sl@0
|
162 |
execsql {
|
sl@0
|
163 |
PRAGMA auto_vacuum = 1;
|
sl@0
|
164 |
INSERT INTO tbl2 VALUES('hello world');
|
sl@0
|
165 |
}
|
sl@0
|
166 |
expr {[file size test.db] / 1024}
|
sl@0
|
167 |
} {3}
|
sl@0
|
168 |
|
sl@0
|
169 |
#---------------------------------------------------------------------
|
sl@0
|
170 |
# Try to run a very simple incremental vacuum. Also verify that
|
sl@0
|
171 |
# PRAGMA incremental_vacuum is a harmless no-op against a database that
|
sl@0
|
172 |
# does not support auto-vacuum.
|
sl@0
|
173 |
#
|
sl@0
|
174 |
do_test incrvacuum-4.1 {
|
sl@0
|
175 |
set ::str [string repeat 1234567890 110]
|
sl@0
|
176 |
execsql {
|
sl@0
|
177 |
PRAGMA auto_vacuum = 2;
|
sl@0
|
178 |
INSERT INTO tbl2 VALUES($::str);
|
sl@0
|
179 |
CREATE TABLE tbl1(a, b, c);
|
sl@0
|
180 |
}
|
sl@0
|
181 |
expr {[file size test.db] / 1024}
|
sl@0
|
182 |
} {5}
|
sl@0
|
183 |
do_test incrvacuum-4.2 {
|
sl@0
|
184 |
execsql {
|
sl@0
|
185 |
DELETE FROM tbl2;
|
sl@0
|
186 |
DROP TABLE tbl1;
|
sl@0
|
187 |
}
|
sl@0
|
188 |
expr {[file size test.db] / 1024}
|
sl@0
|
189 |
} {5}
|
sl@0
|
190 |
do_test incrvacuum-4.3 {
|
sl@0
|
191 |
set ::nStep 0
|
sl@0
|
192 |
db eval {pragma incremental_vacuum(10)} {
|
sl@0
|
193 |
incr ::nStep
|
sl@0
|
194 |
}
|
sl@0
|
195 |
list [expr {[file size test.db] / 1024}] $::nStep
|
sl@0
|
196 |
} {3 2}
|
sl@0
|
197 |
|
sl@0
|
198 |
#---------------------------------------------------------------------
|
sl@0
|
199 |
# The following tests - incrvacuum-5.* - test incremental vacuum
|
sl@0
|
200 |
# from within a transaction.
|
sl@0
|
201 |
#
|
sl@0
|
202 |
do_test incrvacuum-5.1.1 {
|
sl@0
|
203 |
expr {[file size test.db] / 1024}
|
sl@0
|
204 |
} {3}
|
sl@0
|
205 |
do_test incrvacuum-5.1.2 {
|
sl@0
|
206 |
execsql {
|
sl@0
|
207 |
BEGIN;
|
sl@0
|
208 |
DROP TABLE tbl2;
|
sl@0
|
209 |
PRAGMA incremental_vacuum;
|
sl@0
|
210 |
COMMIT;
|
sl@0
|
211 |
}
|
sl@0
|
212 |
expr {[file size test.db] / 1024}
|
sl@0
|
213 |
} {1}
|
sl@0
|
214 |
|
sl@0
|
215 |
do_test incrvacuum-5.2.1 {
|
sl@0
|
216 |
set ::str [string repeat abcdefghij 110]
|
sl@0
|
217 |
execsql {
|
sl@0
|
218 |
BEGIN;
|
sl@0
|
219 |
CREATE TABLE tbl1(a);
|
sl@0
|
220 |
INSERT INTO tbl1 VALUES($::str);
|
sl@0
|
221 |
PRAGMA incremental_vacuum; -- this is a no-op.
|
sl@0
|
222 |
COMMIT;
|
sl@0
|
223 |
}
|
sl@0
|
224 |
expr {[file size test.db] / 1024}
|
sl@0
|
225 |
} {4}
|
sl@0
|
226 |
do_test incrvacuum-5.2.2 {
|
sl@0
|
227 |
set ::str [string repeat abcdefghij 110]
|
sl@0
|
228 |
execsql {
|
sl@0
|
229 |
BEGIN;
|
sl@0
|
230 |
INSERT INTO tbl1 VALUES($::str);
|
sl@0
|
231 |
INSERT INTO tbl1 SELECT * FROM tbl1;
|
sl@0
|
232 |
DELETE FROM tbl1 WHERE oid%2; -- Put 2 overflow pages on free-list.
|
sl@0
|
233 |
COMMIT;
|
sl@0
|
234 |
}
|
sl@0
|
235 |
expr {[file size test.db] / 1024}
|
sl@0
|
236 |
} {7}
|
sl@0
|
237 |
do_test incrvacuum-5.2.3 {
|
sl@0
|
238 |
execsql {
|
sl@0
|
239 |
BEGIN;
|
sl@0
|
240 |
PRAGMA incremental_vacuum; -- Vacuum up the two pages.
|
sl@0
|
241 |
CREATE TABLE tbl2(b); -- Use one free page as a table root.
|
sl@0
|
242 |
INSERT INTO tbl2 VALUES('a nice string');
|
sl@0
|
243 |
COMMIT;
|
sl@0
|
244 |
}
|
sl@0
|
245 |
expr {[file size test.db] / 1024}
|
sl@0
|
246 |
} {6}
|
sl@0
|
247 |
do_test incrvacuum-5.2.4 {
|
sl@0
|
248 |
execsql {
|
sl@0
|
249 |
SELECT * FROM tbl2;
|
sl@0
|
250 |
}
|
sl@0
|
251 |
} {{a nice string}}
|
sl@0
|
252 |
do_test incrvacuum-5.2.5 {
|
sl@0
|
253 |
execsql {
|
sl@0
|
254 |
DROP TABLE tbl1;
|
sl@0
|
255 |
DROP TABLE tbl2;
|
sl@0
|
256 |
PRAGMA incremental_vacuum;
|
sl@0
|
257 |
}
|
sl@0
|
258 |
expr {[file size test.db] / 1024}
|
sl@0
|
259 |
} {1}
|
sl@0
|
260 |
|
sl@0
|
261 |
|
sl@0
|
262 |
# Test cases incrvacuum-5.3.* use the following list as input data.
|
sl@0
|
263 |
# Two new databases are opened, one with incremental vacuum enabled,
|
sl@0
|
264 |
# the other with no auto-vacuum completely disabled. After executing
|
sl@0
|
265 |
# each element of the following list on both databases, test that
|
sl@0
|
266 |
# the integrity-check passes and the contents of each are identical.
|
sl@0
|
267 |
#
|
sl@0
|
268 |
set TestScriptList [list {
|
sl@0
|
269 |
BEGIN;
|
sl@0
|
270 |
CREATE TABLE t1(a, b);
|
sl@0
|
271 |
CREATE TABLE t2(a, b);
|
sl@0
|
272 |
CREATE INDEX t1_i ON t1(a);
|
sl@0
|
273 |
CREATE INDEX t2_i ON t2(a);
|
sl@0
|
274 |
} {
|
sl@0
|
275 |
INSERT INTO t1 VALUES($::str1, $::str2);
|
sl@0
|
276 |
INSERT INTO t1 VALUES($::str1||$::str2, $::str2||$::str1);
|
sl@0
|
277 |
INSERT INTO t2 SELECT b, a FROM t1;
|
sl@0
|
278 |
INSERT INTO t2 SELECT a, b FROM t1;
|
sl@0
|
279 |
INSERT INTO t1 SELECT b, a FROM t2;
|
sl@0
|
280 |
UPDATE t2 SET b = '';
|
sl@0
|
281 |
PRAGMA incremental_vacuum;
|
sl@0
|
282 |
} {
|
sl@0
|
283 |
UPDATE t2 SET b = (SELECT b FROM t1 WHERE t1.oid = t2.oid);
|
sl@0
|
284 |
PRAGMA incremental_vacuum;
|
sl@0
|
285 |
} {
|
sl@0
|
286 |
CREATE TABLE t3(a, b);
|
sl@0
|
287 |
INSERT INTO t3 SELECT * FROM t2;
|
sl@0
|
288 |
DROP TABLE t2;
|
sl@0
|
289 |
PRAGMA incremental_vacuum;
|
sl@0
|
290 |
} {
|
sl@0
|
291 |
CREATE INDEX t3_i ON t3(a);
|
sl@0
|
292 |
COMMIT;
|
sl@0
|
293 |
} {
|
sl@0
|
294 |
BEGIN;
|
sl@0
|
295 |
DROP INDEX t3_i;
|
sl@0
|
296 |
PRAGMA incremental_vacuum;
|
sl@0
|
297 |
INSERT INTO t3 VALUES('hello', 'world');
|
sl@0
|
298 |
ROLLBACK;
|
sl@0
|
299 |
} {
|
sl@0
|
300 |
INSERT INTO t3 VALUES('hello', 'world');
|
sl@0
|
301 |
}
|
sl@0
|
302 |
]
|
sl@0
|
303 |
|
sl@0
|
304 |
# If this build omits subqueries, step 2 in the above list will not
|
sl@0
|
305 |
# work. Replace it with "" in this case.
|
sl@0
|
306 |
#
|
sl@0
|
307 |
ifcapable !subquery { lset TestScriptList 2 "" }
|
sl@0
|
308 |
|
sl@0
|
309 |
# Compare the contents of databases $A and $B.
|
sl@0
|
310 |
#
|
sl@0
|
311 |
proc compare_dbs {A B tname} {
|
sl@0
|
312 |
set tbl_list [execsql {
|
sl@0
|
313 |
SELECT tbl_name FROM sqlite_master WHERE type = 'table'
|
sl@0
|
314 |
} $A]
|
sl@0
|
315 |
|
sl@0
|
316 |
do_test ${tname}.1 [subst {
|
sl@0
|
317 |
execsql {
|
sl@0
|
318 |
SELECT tbl_name FROM sqlite_master WHERE type = 'table'
|
sl@0
|
319 |
} $B
|
sl@0
|
320 |
}] $tbl_list
|
sl@0
|
321 |
|
sl@0
|
322 |
set tn 1
|
sl@0
|
323 |
foreach tbl $tbl_list {
|
sl@0
|
324 |
set control [execsql "SELECT * FROM $tbl" $A]
|
sl@0
|
325 |
do_test ${tname}.[incr tn] [subst {
|
sl@0
|
326 |
execsql "SELECT * FROM $tbl" $B
|
sl@0
|
327 |
}] $control
|
sl@0
|
328 |
}
|
sl@0
|
329 |
}
|
sl@0
|
330 |
|
sl@0
|
331 |
set ::str1 [string repeat abcdefghij 130]
|
sl@0
|
332 |
set ::str2 [string repeat 1234567890 105]
|
sl@0
|
333 |
|
sl@0
|
334 |
file delete -force test1.db test1.db-journal test2.db test2.db-journal
|
sl@0
|
335 |
sqlite3 db1 test1.db
|
sl@0
|
336 |
sqlite3 db2 test2.db
|
sl@0
|
337 |
execsql { PRAGMA auto_vacuum = 'none' } db1
|
sl@0
|
338 |
execsql { PRAGMA auto_vacuum = 'incremental' } db2
|
sl@0
|
339 |
|
sl@0
|
340 |
set tn 1
|
sl@0
|
341 |
foreach sql $::TestScriptList {
|
sl@0
|
342 |
execsql $sql db1
|
sl@0
|
343 |
execsql $sql db2
|
sl@0
|
344 |
|
sl@0
|
345 |
compare_dbs db1 db2 incrvacuum-5.3.${tn}
|
sl@0
|
346 |
do_test incrvacuum-5.3.${tn}.integrity1 {
|
sl@0
|
347 |
execsql { PRAGMA integrity_check; } db1
|
sl@0
|
348 |
} {ok}
|
sl@0
|
349 |
do_test incrvacuum-5.3.${tn}.integrity2 {
|
sl@0
|
350 |
execsql { PRAGMA integrity_check; } db2
|
sl@0
|
351 |
} {ok}
|
sl@0
|
352 |
incr tn
|
sl@0
|
353 |
}
|
sl@0
|
354 |
db1 close
|
sl@0
|
355 |
db2 close
|
sl@0
|
356 |
#
|
sl@0
|
357 |
# End of test cases 5.3.*
|
sl@0
|
358 |
|
sl@0
|
359 |
#---------------------------------------------------------------------
|
sl@0
|
360 |
# The following tests - incrvacuum-6.* - test running incremental
|
sl@0
|
361 |
# vacuum while another statement (a read) is being executed.
|
sl@0
|
362 |
#
|
sl@0
|
363 |
for {set jj 0} {$jj < 10} {incr jj} {
|
sl@0
|
364 |
# Build some test data. Two tables are created in an empty
|
sl@0
|
365 |
# database. tbl1 data is a contiguous block starting at page 5 (pages
|
sl@0
|
366 |
# 3 and 4 are the table roots). tbl2 is a contiguous block starting
|
sl@0
|
367 |
# right after tbl1.
|
sl@0
|
368 |
#
|
sl@0
|
369 |
# Then drop tbl1 so that when an incr vacuum is run the pages
|
sl@0
|
370 |
# of tbl2 have to be moved to fill the gap.
|
sl@0
|
371 |
#
|
sl@0
|
372 |
do_test incrvacuum-6.${jj}.1 {
|
sl@0
|
373 |
execsql {
|
sl@0
|
374 |
DROP TABLE IF EXISTS tbl1;
|
sl@0
|
375 |
DROP TABLE IF EXISTS tbl2;
|
sl@0
|
376 |
PRAGMA incremental_vacuum;
|
sl@0
|
377 |
CREATE TABLE tbl1(a, b);
|
sl@0
|
378 |
CREATE TABLE tbl2(a, b);
|
sl@0
|
379 |
BEGIN;
|
sl@0
|
380 |
}
|
sl@0
|
381 |
for {set ii 0} {$ii < 1000} {incr ii} {
|
sl@0
|
382 |
db eval {INSERT INTO tbl1 VALUES($ii, $ii || $ii)}
|
sl@0
|
383 |
}
|
sl@0
|
384 |
execsql {
|
sl@0
|
385 |
INSERT INTO tbl2 SELECT * FROM tbl1;
|
sl@0
|
386 |
COMMIT;
|
sl@0
|
387 |
DROP TABLE tbl1;
|
sl@0
|
388 |
}
|
sl@0
|
389 |
expr {[file size test.db] / 1024}
|
sl@0
|
390 |
} {36}
|
sl@0
|
391 |
|
sl@0
|
392 |
# Run a linear scan query on tbl2. After reading ($jj*100) rows,
|
sl@0
|
393 |
# run the incremental vacuum to shrink the database.
|
sl@0
|
394 |
#
|
sl@0
|
395 |
do_test incrvacuum-6.${jj}.2 {
|
sl@0
|
396 |
set ::nRow 0
|
sl@0
|
397 |
db eval {SELECT a FROM tbl2} {} {
|
sl@0
|
398 |
if {$a == [expr $jj*100]} {
|
sl@0
|
399 |
db eval {PRAGMA incremental_vacuum}
|
sl@0
|
400 |
}
|
sl@0
|
401 |
incr ::nRow
|
sl@0
|
402 |
}
|
sl@0
|
403 |
list [expr {[file size test.db] / 1024}] $nRow
|
sl@0
|
404 |
} {19 1000}
|
sl@0
|
405 |
}
|
sl@0
|
406 |
|
sl@0
|
407 |
#---------------------------------------------------------------------
|
sl@0
|
408 |
# This test - incrvacuum-7.* - is to check that the database can be
|
sl@0
|
409 |
# written in the middle of an incremental vacuum.
|
sl@0
|
410 |
#
|
sl@0
|
411 |
set ::iWrite 1
|
sl@0
|
412 |
while 1 {
|
sl@0
|
413 |
do_test incrvacuum-7.${::iWrite}.1 {
|
sl@0
|
414 |
execsql {
|
sl@0
|
415 |
DROP TABLE IF EXISTS tbl1;
|
sl@0
|
416 |
DROP TABLE IF EXISTS tbl2;
|
sl@0
|
417 |
PRAGMA incremental_vacuum;
|
sl@0
|
418 |
CREATE TABLE tbl1(a, b);
|
sl@0
|
419 |
CREATE TABLE tbl2(a, b);
|
sl@0
|
420 |
BEGIN;
|
sl@0
|
421 |
}
|
sl@0
|
422 |
for {set ii 0} {$ii < 1000} {incr ii} {
|
sl@0
|
423 |
db eval {INSERT INTO tbl1 VALUES($ii, $ii || $ii)}
|
sl@0
|
424 |
}
|
sl@0
|
425 |
execsql {
|
sl@0
|
426 |
INSERT INTO tbl2 SELECT * FROM tbl1;
|
sl@0
|
427 |
COMMIT;
|
sl@0
|
428 |
DROP TABLE tbl1;
|
sl@0
|
429 |
}
|
sl@0
|
430 |
expr {[file size test.db] / 1024}
|
sl@0
|
431 |
} {36}
|
sl@0
|
432 |
|
sl@0
|
433 |
do_test incrvacuum-7.${::iWrite}.2 {
|
sl@0
|
434 |
set ::nRow 0
|
sl@0
|
435 |
db eval {PRAGMA incremental_vacuum} {
|
sl@0
|
436 |
incr ::nRow
|
sl@0
|
437 |
if {$::nRow == $::iWrite} {
|
sl@0
|
438 |
db eval {
|
sl@0
|
439 |
CREATE TABLE tbl1(a, b);
|
sl@0
|
440 |
INSERT INTO tbl1 VALUES('hello', 'world');
|
sl@0
|
441 |
}
|
sl@0
|
442 |
}
|
sl@0
|
443 |
}
|
sl@0
|
444 |
list [expr {[file size test.db] / 1024}]
|
sl@0
|
445 |
} {20}
|
sl@0
|
446 |
|
sl@0
|
447 |
do_test incrvacuum-7.${::iWrite}.3 {
|
sl@0
|
448 |
execsql {
|
sl@0
|
449 |
SELECT * FROM tbl1;
|
sl@0
|
450 |
}
|
sl@0
|
451 |
} {hello world}
|
sl@0
|
452 |
|
sl@0
|
453 |
if {$::nRow == $::iWrite} break
|
sl@0
|
454 |
incr ::iWrite
|
sl@0
|
455 |
}
|
sl@0
|
456 |
|
sl@0
|
457 |
#---------------------------------------------------------------------
|
sl@0
|
458 |
# This test - incrvacuum-8.* - is to check that nothing goes wrong
|
sl@0
|
459 |
# with an incremental-vacuum if it is the first statement executed
|
sl@0
|
460 |
# after an existing database is opened.
|
sl@0
|
461 |
#
|
sl@0
|
462 |
# At one point, this would always return SQLITE_SCHEMA (which
|
sl@0
|
463 |
# causes an infinite loop in tclsqlite.c if using the Tcl interface).
|
sl@0
|
464 |
#
|
sl@0
|
465 |
do_test incrvacuum-8.1 {
|
sl@0
|
466 |
db close
|
sl@0
|
467 |
sqlite3 db test.db
|
sl@0
|
468 |
execsql {
|
sl@0
|
469 |
PRAGMA incremental_vacuum(50);
|
sl@0
|
470 |
}
|
sl@0
|
471 |
} {}
|
sl@0
|
472 |
|
sl@0
|
473 |
#---------------------------------------------------------------------
|
sl@0
|
474 |
# At one point this test case was causing an assert() to fail.
|
sl@0
|
475 |
#
|
sl@0
|
476 |
do_test incrvacuum-9.1 {
|
sl@0
|
477 |
db close
|
sl@0
|
478 |
file delete -force test.db test.db-journal
|
sl@0
|
479 |
sqlite3 db test.db
|
sl@0
|
480 |
|
sl@0
|
481 |
execsql {
|
sl@0
|
482 |
PRAGMA auto_vacuum = 'incremental';
|
sl@0
|
483 |
CREATE TABLE t1(a, b, c);
|
sl@0
|
484 |
CREATE TABLE t2(a, b, c);
|
sl@0
|
485 |
INSERT INTO t2 VALUES(randstr(500,500),randstr(500,500),randstr(500,500));
|
sl@0
|
486 |
INSERT INTO t1 VALUES(1, 2, 3);
|
sl@0
|
487 |
INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
|
sl@0
|
488 |
INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
|
sl@0
|
489 |
INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
|
sl@0
|
490 |
INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
|
sl@0
|
491 |
INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
|
sl@0
|
492 |
INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
|
sl@0
|
493 |
INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
|
sl@0
|
494 |
INSERT INTO t1 SELECT a||a, b||b, c||c FROM t1;
|
sl@0
|
495 |
}
|
sl@0
|
496 |
} {}
|
sl@0
|
497 |
|
sl@0
|
498 |
do_test incrvacuum-9.2 {
|
sl@0
|
499 |
execsql {
|
sl@0
|
500 |
PRAGMA synchronous = 'OFF';
|
sl@0
|
501 |
BEGIN;
|
sl@0
|
502 |
UPDATE t1 SET a = a, b = b, c = c;
|
sl@0
|
503 |
DROP TABLE t2;
|
sl@0
|
504 |
PRAGMA incremental_vacuum(10);
|
sl@0
|
505 |
ROLLBACK;
|
sl@0
|
506 |
}
|
sl@0
|
507 |
} {}
|
sl@0
|
508 |
|
sl@0
|
509 |
do_test incrvacuum-9.3 {
|
sl@0
|
510 |
execsql {
|
sl@0
|
511 |
PRAGMA cache_size = 10;
|
sl@0
|
512 |
BEGIN;
|
sl@0
|
513 |
UPDATE t1 SET a = a, b = b, c = c;
|
sl@0
|
514 |
DROP TABLE t2;
|
sl@0
|
515 |
PRAGMA incremental_vacuum(10);
|
sl@0
|
516 |
ROLLBACK;
|
sl@0
|
517 |
}
|
sl@0
|
518 |
} {}
|
sl@0
|
519 |
|
sl@0
|
520 |
#---------------------------------------------------------------------
|
sl@0
|
521 |
# Test that the parameter to the incremental_vacuum pragma works. That
|
sl@0
|
522 |
# is, if the user executes "PRAGMA incremental_vacuum(N)", at most
|
sl@0
|
523 |
# N pages are vacuumed.
|
sl@0
|
524 |
#
|
sl@0
|
525 |
do_test incrvacuum-10.1 {
|
sl@0
|
526 |
execsql {
|
sl@0
|
527 |
DROP TABLE t1;
|
sl@0
|
528 |
DROP TABLE t2;
|
sl@0
|
529 |
}
|
sl@0
|
530 |
expr [file size test.db] / 1024
|
sl@0
|
531 |
} {29}
|
sl@0
|
532 |
|
sl@0
|
533 |
do_test incrvacuum-10.2 {
|
sl@0
|
534 |
execsql {
|
sl@0
|
535 |
PRAGMA incremental_vacuum(1);
|
sl@0
|
536 |
}
|
sl@0
|
537 |
expr [file size test.db] / 1024
|
sl@0
|
538 |
} {28}
|
sl@0
|
539 |
|
sl@0
|
540 |
do_test incrvacuum-10.3 {
|
sl@0
|
541 |
execsql {
|
sl@0
|
542 |
PRAGMA incremental_vacuum(5);
|
sl@0
|
543 |
}
|
sl@0
|
544 |
expr [file size test.db] / 1024
|
sl@0
|
545 |
} {23}
|
sl@0
|
546 |
|
sl@0
|
547 |
do_test incrvacuum-10.4 {
|
sl@0
|
548 |
execsql {
|
sl@0
|
549 |
PRAGMA incremental_vacuum('1');
|
sl@0
|
550 |
}
|
sl@0
|
551 |
expr [file size test.db] / 1024
|
sl@0
|
552 |
} {22}
|
sl@0
|
553 |
|
sl@0
|
554 |
do_test incrvacuum-10.5 {
|
sl@0
|
555 |
execsql {
|
sl@0
|
556 |
PRAGMA incremental_vacuum("+3");
|
sl@0
|
557 |
}
|
sl@0
|
558 |
expr [file size test.db] / 1024
|
sl@0
|
559 |
} {19}
|
sl@0
|
560 |
|
sl@0
|
561 |
do_test incrvacuum-10.6 {
|
sl@0
|
562 |
execsql {
|
sl@0
|
563 |
PRAGMA incremental_vacuum = 1;
|
sl@0
|
564 |
}
|
sl@0
|
565 |
expr [file size test.db] / 1024
|
sl@0
|
566 |
} {18}
|
sl@0
|
567 |
|
sl@0
|
568 |
do_test incrvacuum-10.7 {
|
sl@0
|
569 |
# Use a really big number as an argument to incremetal_vacuum. Should
|
sl@0
|
570 |
# be interpreted as "free all possible space".
|
sl@0
|
571 |
execsql {
|
sl@0
|
572 |
PRAGMA incremental_vacuum(2147483649);
|
sl@0
|
573 |
}
|
sl@0
|
574 |
expr [file size test.db] / 1024
|
sl@0
|
575 |
} {1}
|
sl@0
|
576 |
|
sl@0
|
577 |
do_test incrvacuum-10.8 {
|
sl@0
|
578 |
execsql {
|
sl@0
|
579 |
CREATE TABLE t1(x);
|
sl@0
|
580 |
INSERT INTO t1 VALUES(hex(randomblob(1000)));
|
sl@0
|
581 |
DROP TABLE t1;
|
sl@0
|
582 |
}
|
sl@0
|
583 |
# A negative number means free all possible space.
|
sl@0
|
584 |
execsql {
|
sl@0
|
585 |
PRAGMA incremental_vacuum=-1;
|
sl@0
|
586 |
}
|
sl@0
|
587 |
expr [file size test.db] / 1024
|
sl@0
|
588 |
} {1}
|
sl@0
|
589 |
|
sl@0
|
590 |
#----------------------------------------------------------------
|
sl@0
|
591 |
# Test that if we set the auto_vacuum mode to 'incremental', then
|
sl@0
|
592 |
# create a database, thereafter that database defaults to incremental
|
sl@0
|
593 |
# vacuum mode.
|
sl@0
|
594 |
#
|
sl@0
|
595 |
db close
|
sl@0
|
596 |
file delete -force test.db test.db-journal
|
sl@0
|
597 |
sqlite3 db test.db
|
sl@0
|
598 |
|
sl@0
|
599 |
ifcapable default_autovacuum {
|
sl@0
|
600 |
do_test incrvacuum-11.1-av-dflt-on {
|
sl@0
|
601 |
execsql {
|
sl@0
|
602 |
PRAGMA auto_vacuum;
|
sl@0
|
603 |
}
|
sl@0
|
604 |
} $AUTOVACUUM
|
sl@0
|
605 |
} else {
|
sl@0
|
606 |
do_test incrvacuum-11.1-av-dflt-off {
|
sl@0
|
607 |
execsql {
|
sl@0
|
608 |
PRAGMA auto_vacuum;
|
sl@0
|
609 |
}
|
sl@0
|
610 |
} {0}
|
sl@0
|
611 |
}
|
sl@0
|
612 |
do_test incrvacuum-11.2 {
|
sl@0
|
613 |
execsql {
|
sl@0
|
614 |
PRAGMA auto_vacuum = incremental;
|
sl@0
|
615 |
}
|
sl@0
|
616 |
} {}
|
sl@0
|
617 |
do_test incrvacuum-11.3 {
|
sl@0
|
618 |
execsql {
|
sl@0
|
619 |
PRAGMA auto_vacuum;
|
sl@0
|
620 |
}
|
sl@0
|
621 |
} {2}
|
sl@0
|
622 |
do_test incrvacuum-11.4 {
|
sl@0
|
623 |
# The database has now been created.
|
sl@0
|
624 |
expr {[file size test.db]>0}
|
sl@0
|
625 |
} {1}
|
sl@0
|
626 |
do_test incrvacuum-11.5 {
|
sl@0
|
627 |
# Close and reopen the connection.
|
sl@0
|
628 |
db close
|
sl@0
|
629 |
sqlite3 db test.db
|
sl@0
|
630 |
|
sl@0
|
631 |
# Test we are still in incremental vacuum mode.
|
sl@0
|
632 |
execsql { PRAGMA auto_vacuum; }
|
sl@0
|
633 |
} {2}
|
sl@0
|
634 |
do_test incrvacuum-11.6 {
|
sl@0
|
635 |
execsql {
|
sl@0
|
636 |
PRAGMA auto_vacuum = 'full';
|
sl@0
|
637 |
PRAGMA auto_vacuum;
|
sl@0
|
638 |
}
|
sl@0
|
639 |
} {1}
|
sl@0
|
640 |
do_test incrvacuum-11.7 {
|
sl@0
|
641 |
# Close and reopen the connection.
|
sl@0
|
642 |
db close
|
sl@0
|
643 |
sqlite3 db test.db
|
sl@0
|
644 |
|
sl@0
|
645 |
# Test we are still in "full" auto-vacuum mode.
|
sl@0
|
646 |
execsql { PRAGMA auto_vacuum; }
|
sl@0
|
647 |
} {1}
|
sl@0
|
648 |
|
sl@0
|
649 |
#----------------------------------------------------------------------
|
sl@0
|
650 |
# Special case: What happens if the database is locked when a "PRAGMA
|
sl@0
|
651 |
# auto_vacuum = XXX" statement is executed.
|
sl@0
|
652 |
#
|
sl@0
|
653 |
db close
|
sl@0
|
654 |
file delete -force test.db test.db-journal
|
sl@0
|
655 |
sqlite3 db test.db
|
sl@0
|
656 |
|
sl@0
|
657 |
do_test incrvacuum-12.1 {
|
sl@0
|
658 |
execsql {
|
sl@0
|
659 |
PRAGMA auto_vacuum = 1;
|
sl@0
|
660 |
}
|
sl@0
|
661 |
expr {[file size test.db]>0}
|
sl@0
|
662 |
} {1}
|
sl@0
|
663 |
|
sl@0
|
664 |
# Try to change the auto-vacuum from "full" to "incremental" while the
|
sl@0
|
665 |
# database is locked. Nothing should change.
|
sl@0
|
666 |
#
|
sl@0
|
667 |
do_test incrvacuum-12.2 {
|
sl@0
|
668 |
sqlite3 db2 test.db
|
sl@0
|
669 |
execsql { BEGIN EXCLUSIVE; } db2
|
sl@0
|
670 |
catchsql { PRAGMA auto_vacuum = 2; }
|
sl@0
|
671 |
} {1 {database is locked}}
|
sl@0
|
672 |
|
sl@0
|
673 |
do_test incrvacuum-12.3 {
|
sl@0
|
674 |
execsql { ROLLBACK; } db2
|
sl@0
|
675 |
execsql { PRAGMA auto_vacuum }
|
sl@0
|
676 |
} {1}
|
sl@0
|
677 |
|
sl@0
|
678 |
do_test incrvacuum-12.3 {
|
sl@0
|
679 |
execsql { SELECT * FROM sqlite_master }
|
sl@0
|
680 |
execsql { PRAGMA auto_vacuum }
|
sl@0
|
681 |
} {1}
|
sl@0
|
682 |
|
sl@0
|
683 |
#----------------------------------------------------------------------
|
sl@0
|
684 |
# Special case #2: What if one process prepares a "PRAGMA auto_vacuum = XXX"
|
sl@0
|
685 |
# statement when the database is empty, but doesn't execute it until
|
sl@0
|
686 |
# after some other process has created the database.
|
sl@0
|
687 |
#
|
sl@0
|
688 |
db2 close
|
sl@0
|
689 |
db close
|
sl@0
|
690 |
file delete -force test.db test.db-journal
|
sl@0
|
691 |
sqlite3 db test.db ; set ::DB [sqlite3_connection_pointer db]
|
sl@0
|
692 |
sqlite3 db2 test.db
|
sl@0
|
693 |
|
sl@0
|
694 |
do_test incrvacuum-13.1 {
|
sl@0
|
695 |
# File size is sometimes 1 instead of 0 due to the hack we put in
|
sl@0
|
696 |
# to work around ticket #3260. Search for comments on #3260 in
|
sl@0
|
697 |
# os_unix.c.
|
sl@0
|
698 |
expr {[file size test.db]>1}
|
sl@0
|
699 |
} {0}
|
sl@0
|
700 |
do_test incrvacuum-13.2 {
|
sl@0
|
701 |
set ::STMT [sqlite3_prepare $::DB {PRAGMA auto_vacuum = 2} -1 DUMMY]
|
sl@0
|
702 |
execsql {
|
sl@0
|
703 |
PRAGMA auto_vacuum = none;
|
sl@0
|
704 |
PRAGMA default_cache_size = 1024;
|
sl@0
|
705 |
PRAGMA auto_vacuum;
|
sl@0
|
706 |
} db2
|
sl@0
|
707 |
} {0}
|
sl@0
|
708 |
do_test incrvacuum-13.3 {
|
sl@0
|
709 |
expr {[file size test.db]>0}
|
sl@0
|
710 |
} {1}
|
sl@0
|
711 |
do_test incrvacuum-13.4 {
|
sl@0
|
712 |
set rc [sqlite3_step $::STMT]
|
sl@0
|
713 |
list $rc [sqlite3_finalize $::STMT]
|
sl@0
|
714 |
} {SQLITE_DONE SQLITE_OK}
|
sl@0
|
715 |
do_test incrvacuum-13.5 {
|
sl@0
|
716 |
execsql {
|
sl@0
|
717 |
PRAGMA auto_vacuum;
|
sl@0
|
718 |
}
|
sl@0
|
719 |
} {0}
|
sl@0
|
720 |
|
sl@0
|
721 |
|
sl@0
|
722 |
# Verify that the incremental_vacuum pragma fails gracefully if it
|
sl@0
|
723 |
# is used against an invalid database file.
|
sl@0
|
724 |
#
|
sl@0
|
725 |
do_test incrvacuum-14.1 {
|
sl@0
|
726 |
set out [open invalid.db w]
|
sl@0
|
727 |
puts $out "This is not an SQLite database file"
|
sl@0
|
728 |
close $out
|
sl@0
|
729 |
sqlite3 db3 invalid.db
|
sl@0
|
730 |
catchsql {
|
sl@0
|
731 |
PRAGMA incremental_vacuum(10);
|
sl@0
|
732 |
} db3
|
sl@0
|
733 |
} {1 {file is encrypted or is not a database}}
|
sl@0
|
734 |
|
sl@0
|
735 |
db2 close
|
sl@0
|
736 |
db3 close
|
sl@0
|
737 |
finish_test
|