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 operation of the library in
13 # "PRAGMA journal_mode=WAL" mode.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 source $testdir/wal_common.tcl
19 source $testdir/malloc_common.tcl
23 ifcapable !wal {finish_test ; return }
28 # walback-1.*: Simple tests.
30 # walback-2.*: Test backups when the source db is modified mid-backup.
32 # walback-3.*: Backup of WAL sources into rollback destinations, and
36 # Make sure a simple backup from a WAL database works.
40 PRAGMA synchronous = NORMAL;
41 PRAGMA page_size = 1024;
42 PRAGMA auto_vacuum = 0;
43 PRAGMA journal_mode = wal;
45 CREATE TABLE t1(a PRIMARY KEY, b);
46 INSERT INTO t1 VALUES('I', 'one');
51 forcedelete bak.db bak.db-journal bak.db-wal
59 PRAGMA main.journal_mode;
63 execsql { PRAGMA integrity_check } db2
67 # Try a VACUUM on a WAL database.
72 PRAGMA main.journal_mode;
76 list [file size test.db] [file size test.db-wal]
77 } [list 1024 [wal_file_size 6 1024]]
79 execsql { PRAGMA wal_checkpoint }
80 list [file size test.db] [file size test.db-wal]
81 } [list [expr 3*1024] [wal_file_size 6 1024]]
82 do_test walbak-1.6.1 {
83 hexio_read test.db 18 2
87 CREATE TABLE t2(a, b);
88 INSERT INTO t2 SELECT * FROM t1;
91 list [file size test.db] [file size test.db-wal]
92 } [list [expr 3*1024] [wal_file_size 6 1024]]
95 list [file size test.db] [file size test.db-wal]
96 } [list [expr 3*1024] [wal_file_size 8 1024]]
98 execsql { PRAGMA wal_checkpoint }
99 list [file size test.db] [file size test.db-wal]
100 } [list [expr 2*1024] [wal_file_size 8 1024]]
102 #-------------------------------------------------------------------------
103 # Backups when the source db is modified mid-backup.
107 PRAGMA integrity_check;
108 SELECT md5sum(a, b) FROM t1;
115 execsql { PRAGMA journal_mode = WAL }
117 CREATE TABLE t1(a PRIMARY KEY, b);
119 INSERT INTO t1 VALUES(randomblob(500), randomblob(500));
120 INSERT INTO t1 SELECT randomblob(500), randomblob(500) FROM t1; /* 2 */
121 INSERT INTO t1 SELECT randomblob(500), randomblob(500) FROM t1; /* 4 */
122 INSERT INTO t1 SELECT randomblob(500), randomblob(500) FROM t1; /* 8 */
123 INSERT INTO t1 SELECT randomblob(500), randomblob(500) FROM t1; /* 16 */
124 INSERT INTO t1 SELECT randomblob(500), randomblob(500) FROM t1; /* 32 */
125 INSERT INTO t1 SELECT randomblob(500), randomblob(500) FROM t1; /* 64 */
132 string compare [sig db] [sig db2]
136 sqlite3_backup B db2 main db main
138 execsql { UPDATE t1 SET b = randomblob(500) }
139 list [B step 1000] [B finish]
140 } {SQLITE_DONE SQLITE_OK}
142 string compare [sig db] [sig db2]
148 execsql { PRAGMA cache_size = 10 }
149 sqlite3_backup B db2 main db main
153 UPDATE t1 SET b = randomblob(500);
155 expr [file size test.db-wal] > 10*1024
162 list [B step 1000] [B finish]
163 } {SQLITE_DONE SQLITE_OK}
165 string compare [sig db] [sig db2]
171 execsql { PRAGMA cache_size = 10 }
172 sqlite3_backup B db2 main db main
176 UPDATE t1 SET b = randomblob(500);
178 expr [file size test.db-wal] > 10*1024
180 do_test walbak-2.10 {
183 do_test walbak-2.11 {
186 list [B step 1000] [B finish]
187 } {SQLITE_DONE SQLITE_OK}
188 do_test walbak-2.12 {
189 string compare [sig db] [sig db2]
194 #-------------------------------------------------------------------------
195 # Run some backup operations to copy back and forth between WAL and:
197 # walbak-3.1.*: an in-memory database
199 # walbak-3.2.*: a temporary database
201 # walbak-3.3.*: a database in rollback mode.
203 # walbak-3.4.*: a database in rollback mode that (initially) uses a
204 # different page-size.
206 # Check that this does not confuse any connected clients.
212 db eval { PRAGMA page_size = 1024 ; PRAGMA journal_mode = WAL }
213 db2 eval { PRAGMA page_size = 1024 }
219 db eval { PRAGMA page_size = 1024 ; PRAGMA journal_mode = WAL }
220 db2 eval { PRAGMA page_size = 1024 }
226 db eval { PRAGMA page_size = 1024 ; PRAGMA journal_mode = WAL }
227 db2 eval { PRAGMA page_size = 1024 ; PRAGMA journal_mode = PERSIST }
233 db eval { PRAGMA page_size = 1024 ; PRAGMA journal_mode = WAL }
235 PRAGMA page_size = 2048;
236 PRAGMA journal_mode = PERSIST;
242 foreach f [glob -nocomplain test.db*] { forcedelete $f }
246 do_test walbak-3.$tn.1 {
248 CREATE TABLE t1(a, b);
249 INSERT INTO t1 VALUES(1, 2);
250 INSERT INTO t1 VALUES(3, 4);
255 do_test walbak-3.$tn.2 {
256 sqlite3_backup B db2 main db main
259 execsql { SELECT * FROM t1 } db2
262 do_test walbak-3.$tn.3 {
264 INSERT INTO t1 VALUES(5, 6);
265 INSERT INTO t1 VALUES(7, 8);
270 do_test walbak-3.$tn.4 {
271 sqlite3_backup B db main db2 main
274 execsql { SELECT * FROM t1 }
277 # Check that [db] is still in WAL mode.
278 do_test walbak-3.$tn.5 {
279 execsql { PRAGMA journal_mode }
281 do_test walbak-3.$tn.6 {
282 execsql { PRAGMA wal_checkpoint }
283 hexio_read test.db 18 2
286 # If it was not an in-memory database, check that [db2] is still in
288 if {[file exists test.db2]} {
289 do_test walbak-3.$tn.7 {
290 execsql { PRAGMA journal_mode } db2
292 do_test walbak-3.$tn.8 {
293 execsql { PRAGMA wal_checkpoint }
294 hexio_read test.db 18 2
302 #-------------------------------------------------------------------------
303 # Test that the following holds when a backup operation is run:
305 # Source | Destination inital | Destination final
306 # ---------------------------------------------------
307 # Rollback Rollback Rollback
312 foreach {tn src dest dest_final} {
313 1 delete delete delete
320 forcedelete test.db test.db2
322 do_test walbak-4.$tn.1 {
324 db eval "PRAGMA journal_mode = $src"
326 CREATE TABLE t1(a, b);
327 INSERT INTO t1 VALUES('I', 'II');
328 INSERT INTO t1 VALUES('III', 'IV');
332 db2 eval "PRAGMA journal_mode = $dest"
334 CREATE TABLE t2(x, y);
335 INSERT INTO t2 VALUES('1', '2');
336 INSERT INTO t2 VALUES('3', '4');
340 do_test walbak-4.$tn.2 { execsql { PRAGMA journal_mode } db } $src
341 do_test walbak-4.$tn.3 { execsql { PRAGMA journal_mode } db2 } $dest
343 do_test walbak-4.$tn.4 { db backup test.db2 } {}
344 do_test walbak-4.$tn.5 {
345 execsql { SELECT * FROM t1 } db2
347 do_test walbak-4.$tn.5 { execsql { PRAGMA journal_mode } db2 } $dest_final
351 do_test walbak-4.$tn.6 { file exists test.db2-wal } 0
353 do_test walbak-4.$tn.7 { execsql { PRAGMA journal_mode } db2 } $dest_final