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 focus
12 # of this file is the sqlite3_snapshot_xxx() APIs.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 ifcapable !snapshot {finish_test; return}
18 set testprefix snapshot
20 # This test does not work with the inmemory_journal permutation. The reason
21 # is that each connection opened as part of this permutation executes
22 # "PRAGMA journal_mode=memory", which fails if the database is in wal mode
23 # and there are one or more existing connections.
24 if {[permutation]=="inmemory_journal"} {
29 #-------------------------------------------------------------------------
30 # Check some error conditions in snapshot_get(). It is an error if:
32 # 1) snapshot_get() is called on a non-WAL database, or
33 # 2) there is an open write transaction on the database.
36 CREATE TABLE t1(a, b);
37 INSERT INTO t1 VALUES(1, 2);
38 INSERT INTO t1 VALUES(3, 4);
42 execsql { BEGIN; SELECT * FROM t1; }
43 list [catch { sqlite3_snapshot_get db main } msg] $msg
45 do_execsql_test 1.1.2 COMMIT
49 PRAGMA journal_mode = WAL;
51 INSERT INTO t1 VALUES(5, 6);
52 INSERT INTO t1 VALUES(7, 8);
54 list [catch { sqlite3_snapshot_get db main } msg] $msg
56 do_execsql_test 1.3.2 COMMIT
58 #-------------------------------------------------------------------------
59 # Check that a simple case works. Reuse the database created by the
60 # block of tests above.
62 do_execsql_test 2.1.0 {
68 set snapshot [sqlite3_snapshot_get db main]
71 INSERT INTO t1 VALUES(9, 10);
74 } {1 2 3 4 5 6 7 8 9 10}
78 sqlite3_snapshot_open db main $snapshot
85 sqlite3_snapshot_free $snapshot
95 } {1 2 3 4 5 6 7 8 9 10}
98 set snapshot [sqlite3_snapshot_get db2 main]
100 INSERT INTO t1 VALUES(11, 12);
103 } {1 2 3 4 5 6 7 8 9 10 11 12}
107 sqlite3_snapshot_open db main $snapshot
111 } {1 2 3 4 5 6 7 8 9 10}
114 sqlite3_snapshot_free $snapshot
121 execsql { DELETE FROM t1 WHERE a>6 }
122 set snapshot [sqlite3_snapshot_get db main]
124 INSERT INTO t1 VALUES('a', 'b');
125 INSERT INTO t1 VALUES('c', 'd');
128 } {1 2 3 4 5 6 a b c d}
131 sqlite3_snapshot_open db main $snapshot
132 execsql { SELECT * FROM t1 }
137 INSERT INTO t1 VALUES('x','y')
139 } {1 {database is locked}}
142 sqlite3_snapshot_free $snapshot
145 #-------------------------------------------------------------------------
146 # Check some errors in sqlite3_snapshot_open(). It is an error if:
148 # 1) the db is in auto-commit mode,
149 # 2) the db has an open (read or write) transaction,
150 # 3) the db is not a wal database,
152 # Reuse the database created by earlier tests.
154 do_execsql_test 3.0.0 {
155 CREATE TABLE t2(x, y);
156 INSERT INTO t2 VALUES('a', 'b');
157 INSERT INTO t2 VALUES('c', 'd');
162 set snapshot [sqlite3_snapshot_get db main]
164 execsql { INSERT INTO t2 VALUES('e', 'f'); }
168 list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg
178 list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg
185 INSERT INTO t2 VALUES('g', 'h');
187 list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg
189 do_execsql_test 3.2.4 COMMIT
192 execsql { PRAGMA journal_mode = DELETE }
194 list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg
198 sqlite3_snapshot_free $snapshot
202 #-------------------------------------------------------------------------
203 # Check that SQLITE_BUSY_SNAPSHOT is returned if the specified snapshot
204 # no longer exists because the wal file has been checkpointed.
206 # 1. Reading a snapshot from the middle of a wal file is not possible
207 # after the wal file has been checkpointed.
209 # 2. That a snapshot from the end of a wal file can not be read once
210 # the wal file has been wrapped.
212 do_execsql_test 4.1.0 {
213 PRAGMA journal_mode = wal;
214 CREATE TABLE t3(i, j);
215 INSERT INTO t3 VALUES('o', 't');
216 INSERT INTO t3 VALUES('t', 'f');
222 set snapshot [sqlite3_snapshot_get db main]
227 INSERT INTO t3 VALUES('f', 's');
230 sqlite3_snapshot_open db main $snapshot
231 execsql { SELECT * FROM t3 }
237 PRAGMA wal_checkpoint;
240 list [catch {sqlite3_snapshot_open db main $snapshot} msg] $msg
241 } {1 SQLITE_BUSY_SNAPSHOT}
243 sqlite3_snapshot_free $snapshot
249 INSERT INTO t3 VALUES('s', 'e');
250 INSERT INTO t3 VALUES('n', 't');
254 } {o t t f f s s e n t}
256 set snapshot [sqlite3_snapshot_get db main]
259 PRAGMA wal_checkpoint;
262 sqlite3_snapshot_open db main $snapshot
263 execsql { SELECT * FROM t3 }
264 } {o t t f f s s e n t}
268 INSERT INTO t3 VALUES('e', 't');
271 list [catch {sqlite3_snapshot_open db main $snapshot} msg] $msg
272 } {1 SQLITE_BUSY_SNAPSHOT}
274 sqlite3_snapshot_free $snapshot
277 #-------------------------------------------------------------------------
278 # Check that SQLITE_BUSY is returned if a checkpoint is running when
279 # sqlite3_snapshot_open() is called.
284 sqlite3 db test.db -vfs tvfs
286 do_execsql_test 5.1 {
287 PRAGMA journal_mode = wal;
288 CREATE TABLE x1(x, xx, xxx);
289 INSERT INTO x1 VALUES('z', 'zz', 'zzz');
295 set ::snapshot [sqlite3_snapshot_get db main]
296 sqlite3 db2 test.db -vfs tvfs
298 INSERT INTO x1 VALUES('a', 'aa', 'aaa');
304 proc write_callback {args} {
305 do_test 5.3.[incr ::t53] {
307 list [catch { sqlite3_snapshot_open db main $::snapshot } msg] $msg
313 tvfs script write_callback
314 db2 eval { PRAGMA wal_checkpoint }
318 sqlite3_snapshot_free $snapshot
320 #-------------------------------------------------------------------------
321 # Test that sqlite3_snapshot_get() may be called immediately after
322 # "BEGIN; PRAGMA user_version;". And that sqlite3_snapshot_open() may
323 # be called after opening the db handle and running the script
324 # "PRAGMA user_version; BEGIN".
326 do_execsql_test 6.1 {
327 PRAGMA journal_mode = wal;
328 CREATE TABLE x1(x, xx, xxx);
329 INSERT INTO x1 VALUES('z', 'zz', 'zzz');
334 set ::snapshot [sqlite3_snapshot_get db main]
336 INSERT INTO x1 VALUES('a', 'aa', 'aaa');
342 db2 eval "PRAGMA user_version ; BEGIN"
343 sqlite3_snapshot_open db2 main $::snapshot
344 db2 eval { SELECT * FROM x1 }
349 db2 eval "PRAGMA application_id"
351 sqlite3_snapshot_open db2 main $::snapshot
352 db2 eval { SELECT * FROM x1 }
359 list [catch {sqlite3_snapshot_open db2 main $::snapshot} msg] $msg
362 sqlite3_snapshot_free $snapshot
364 #-------------------------------------------------------------------------
365 # The following tests investigate the sqlite3_snapshot_cmp() API.
368 # Compare snapshots $p1 and $p2, checking that the result is $r.
370 proc do_snapshot_cmp_test {tn p1 p2 r} {
371 uplevel [list do_test $tn.1 [list sqlite3_snapshot_cmp $p1 $p2] $r]
372 uplevel [list do_test $tn.2 [list sqlite3_snapshot_cmp $p2 $p1] [expr $r*-1]]
373 uplevel [list do_test $tn.3 [list sqlite3_snapshot_cmp $p1 $p1] 0]
374 uplevel [list do_test $tn.4 [list sqlite3_snapshot_cmp $p2 $p2] 0]
380 do_execsql_test 7.1 {
381 PRAGMA journal_mode = wal;
386 execsql { BEGIN ; PRAGMA application_id }
387 set p1 [sqlite3_snapshot_get db main]
389 INSERT INTO t1 VALUES(10);
392 execsql { BEGIN ; PRAGMA application_id }
393 set p2 [sqlite3_snapshot_get db main]
397 do_snapshot_cmp_test 7.1.3 $p1 $p2 -1
398 sqlite3_snapshot_free $p1
399 sqlite3_snapshot_free $p2
401 do_execsql_test 7.2.1 {
402 INSERT INTO t1 VALUES(11);
403 INSERT INTO t1 VALUES(12);
404 INSERT INTO t1 VALUES(13);
406 PRAGMA application_id;
409 set p1 [sqlite3_snapshot_get db main]
412 INSERT INTO t1 VALUES(14);
413 PRAGMA wal_checkpoint;
415 PRAGMA application_id;
417 set p2 [sqlite3_snapshot_get db main]
421 do_snapshot_cmp_test 7.2.3 $p1 $p2 -1
422 sqlite3_snapshot_free $p2
426 INSERT INTO t1 VALUES(14);
428 PRAGMA application_id;
430 set p2 [sqlite3_snapshot_get db main]
434 do_snapshot_cmp_test 7.3.2 $p1 $p2 -1
435 sqlite3_snapshot_free $p1
436 sqlite3_snapshot_free $p2