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 */
133 string compare [sig db] [sig db2]
137 sqlite3_backup B db2 main db main
139 execsql { UPDATE t1 SET b = randomblob(500) }
140 list [B step 1000] [B finish]
141 } {SQLITE_DONE SQLITE_OK}
143 string compare [sig db] [sig db2]
149 execsql { PRAGMA cache_size = 10 }
150 sqlite3_backup B db2 main db main
154 UPDATE t1 SET b = randomblob(500);
156 expr [file size test.db-wal] > 10*1024
163 list [B step 1000] [B finish]
164 } {SQLITE_DONE SQLITE_OK}
166 string compare [sig db] [sig db2]
172 execsql { PRAGMA cache_size = 10 }
173 sqlite3_backup B db2 main db main
177 UPDATE t1 SET b = randomblob(500);
179 expr [file size test.db-wal] > 10*1024
181 do_test walbak-2.10 {
184 do_test walbak-2.11 {
187 list [B step 1000] [B finish]
188 } {SQLITE_DONE SQLITE_OK}
189 do_test walbak-2.12 {
190 string compare [sig db] [sig db2]
195 #-------------------------------------------------------------------------
196 # Run some backup operations to copy back and forth between WAL and:
198 # walbak-3.1.*: an in-memory database
200 # walbak-3.2.*: a temporary database
202 # walbak-3.3.*: a database in rollback mode.
204 # walbak-3.4.*: a database in rollback mode that (initially) uses a
205 # different page-size.
207 # Check that this does not confuse any connected clients.
213 db eval { PRAGMA page_size = 1024 ; PRAGMA journal_mode = WAL }
214 db2 eval { PRAGMA page_size = 1024 }
220 db eval { PRAGMA page_size = 1024 ; PRAGMA journal_mode = WAL }
221 db2 eval { PRAGMA page_size = 1024 }
227 db eval { PRAGMA page_size = 1024 ; PRAGMA journal_mode = WAL }
228 db2 eval { PRAGMA page_size = 1024 ; PRAGMA journal_mode = PERSIST }
234 db eval { PRAGMA page_size = 1024 ; PRAGMA journal_mode = WAL }
236 PRAGMA page_size = 2048;
237 PRAGMA journal_mode = PERSIST;
243 if {$tn==4 && [sqlite3 -has-codec]} continue
244 foreach f [glob -nocomplain test.db*] { forcedelete $f }
248 do_test walbak-3.$tn.1 {
250 CREATE TABLE t1(a, b);
251 INSERT INTO t1 VALUES(1, 2);
252 INSERT INTO t1 VALUES(3, 4);
257 do_test walbak-3.$tn.2 {
258 sqlite3_backup B db2 main db main
261 execsql { SELECT * FROM t1 } db2
264 do_test walbak-3.$tn.3 {
266 INSERT INTO t1 VALUES(5, 6);
267 INSERT INTO t1 VALUES(7, 8);
272 do_test walbak-3.$tn.4 {
273 sqlite3_backup B db main db2 main
276 execsql { SELECT * FROM t1 }
279 # Check that [db] is still in WAL mode.
280 do_test walbak-3.$tn.5 {
281 execsql { PRAGMA journal_mode }
283 do_test walbak-3.$tn.6 {
284 execsql { PRAGMA wal_checkpoint }
285 hexio_read test.db 18 2
288 # If it was not an in-memory database, check that [db2] is still in
290 if {[file exists test.db2]} {
291 do_test walbak-3.$tn.7 {
292 execsql { PRAGMA journal_mode } db2
294 do_test walbak-3.$tn.8 {
295 execsql { PRAGMA wal_checkpoint }
296 hexio_read test.db 18 2
304 #-------------------------------------------------------------------------
305 # Test that the following holds when a backup operation is run:
307 # Source | Destination inital | Destination final
308 # ---------------------------------------------------
309 # Rollback Rollback Rollback
314 foreach {tn src dest dest_final} {
315 1 delete delete delete
322 forcedelete test.db test.db2
324 do_test walbak-4.$tn.1 {
326 db eval "PRAGMA journal_mode = $src"
328 CREATE TABLE t1(a, b);
329 INSERT INTO t1 VALUES('I', 'II');
330 INSERT INTO t1 VALUES('III', 'IV');
334 db2 eval "PRAGMA journal_mode = $dest"
336 CREATE TABLE t2(x, y);
337 INSERT INTO t2 VALUES('1', '2');
338 INSERT INTO t2 VALUES('3', '4');
342 do_test walbak-4.$tn.2 { execsql { PRAGMA journal_mode } db } $src
343 do_test walbak-4.$tn.3 { execsql { PRAGMA journal_mode } db2 } $dest
345 do_test walbak-4.$tn.4 { db backup test.db2 } {}
346 do_test walbak-4.$tn.5 {
347 execsql { SELECT * FROM t1 } db2
349 do_test walbak-4.$tn.5 { execsql { PRAGMA journal_mode } db2 } $dest_final
353 do_test walbak-4.$tn.6 { file exists test.db2-wal } 0
355 do_test walbak-4.$tn.7 { execsql { PRAGMA journal_mode } db2 } $dest_final