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 the SQLite library, focusing
12 # on the incremental vacuum feature.
14 # The tests in this file were added at the same time as optimizations
17 # * Truncate the database after a rollback mode commit, and
19 # * Avoid moving pages to locations from which they may need to be moved
20 # a second time if an incremental-vacuum proccess is allowed to vacuum
21 # the entire database.
24 set testdir [file dirname $argv0]
25 source $testdir/tester.tcl
26 set testprefix incrvacuum3
28 # If this build of the library does not support auto-vacuum, omit this
30 ifcapable {!autovacuum || !pragma} {
35 proc check_on_disk {} {
37 # Copy the wal and journal files for database "test.db" to "test2.db".
38 forcedelete test2.db test2.db-journal test2.db-wal
39 if {[file exists test.db-journal]} {
40 forcecopy test.db-journal test2.db-journal
42 if {[file exists test.db-wal]} {
43 forcecopy test.db-wal test2.db-wal
46 # Now copy the database file itself. Do this using open/read/puts
47 # instead of the [file copy] command in order to avoid attempting
48 # to read the 512 bytes begining at offset $sqlite_pending_byte.
50 set sz [file size test.db]
52 set fd2 [open test2.db w]
53 fconfigure $fd -encoding binary -translation binary
54 fconfigure $fd2 -encoding binary -translation binary
55 if {$sz>$::sqlite_pending_byte} {
56 puts -nonewline $fd2 [read $fd $::sqlite_pending_byte]
57 seek $fd [expr $::sqlite_pending_byte+512]
58 seek $fd2 [expr $::sqlite_pending_byte+512]
60 puts -nonewline $fd2 [read $fd]
64 # Open "test2.db" and check it is Ok.
65 sqlite3 dbcheck test2.db
66 set ret [dbcheck eval { PRAGMA integrity_check }]
71 # Run these tests once in rollback journal mode, and once in wal mode.
73 foreach {T jrnl_mode} {
78 forcedelete test.db test.db-journal test.db-wal
81 PRAGMA cache_size = 5;
82 PRAGMA page_size = 1024;
83 PRAGMA auto_vacuum = 2;
85 db eval "PRAGMA journal_mode = $jrnl_mode"
89 CREATE TABLE t1(x UNIQUE);
90 INSERT INTO t1 VALUES(randomblob(400));
91 INSERT INTO t1 VALUES(randomblob(400));
92 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 4
93 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 8
94 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 16
95 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 32
96 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
97 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
98 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256
102 DELETE FROM t1 WHERE rowid%8;
107 PRAGMA incremental_vacuum = 100;
108 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
109 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
110 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256
117 PRAGMA incremental_vacuum = 100;
119 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
120 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
121 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256
124 5 { ROLLBACK to two }
126 6 { ROLLBACK to one }
129 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
130 PRAGMA incremental_vacuum = 1000;
131 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
132 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256
138 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
139 PRAGMA incremental_vacuum = 1000;
140 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
144 do_execsql_test $T.1.$tn.1 $sql
145 do_execsql_test $T.1.$tn.2 {PRAGMA integrity_check} ok
146 do_test $T.1.$tn.3 { check_on_disk } ok
149 do_execsql_test $T.1.x.1 { PRAGMA freelist_count } 0
150 do_execsql_test $T.1.x.2 { SELECT count(*) FROM t1 } 128