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 snapshot2
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 that it is not possible to obtain a snapshot immediately after
31 # a wal mode database with an empty wal file is opened. But it is after
32 # the file has been written, even by some other connection.
35 PRAGMA journal_mode = wal;
36 CREATE TABLE t1(a, b, c);
37 INSERT INTO t1 VALUES(1, 2, 3);
38 INSERT INTO t1 VALUES(4, 5, 6);
42 do_test 1.1.1 { list [file exists test.db] [file exists test.db-wal] } {1 0}
45 do_execsql_test 1.1.2 { SELECT * FROM t1 } {1 2 3 4 5 6}
49 list [catch { sqlite3_snapshot_get_blob db main } msg] $msg
54 execsql { INSERT INTO t1 VALUES(7, 8, 9) }
56 string length [sqlite3_snapshot_get_blob db main]
61 do_test 1.2.1 { list [file exists test.db] [file exists test.db-wal] } {1 0}
64 do_execsql_test 1.2.2 { SELECT * FROM t1 } {1 2 3 4 5 6 7 8 9}
68 list [catch { sqlite3_snapshot_get_blob db main } msg] $msg
74 execsql { INSERT INTO t1 VALUES(10, 11, 12) } db2
76 string length [sqlite3_snapshot_get_blob db main]
81 #-------------------------------------------------------------------------
82 # Simple tests for sqlite3_snapshot_recover().
87 PRAGMA journal_mode = wal;
88 INSERT INTO t1 VALUES(1);
89 INSERT INTO t1 VALUES(2);
93 db trans { set snap [sqlite3_snapshot_get_blob db main] }
94 sqlite3_db_config db NO_CKPT_ON_CLOSE 1
98 execsql {SELECT * FROM sqlite_master}
100 sqlite3_snapshot_open_blob db main $snap
102 execsql { INSERT INTO t1 VALUES(3); }
106 sqlite3_db_config db NO_CKPT_ON_CLOSE 1
110 execsql {SELECT * FROM sqlite_master}
112 list [catch { sqlite3_snapshot_open_blob db main $snap } msg] $msg
113 } {1 SQLITE_BUSY_SNAPSHOT}
117 sqlite3_snapshot_recover db main
119 sqlite3_snapshot_open_blob db main $snap
120 execsql { SELECT * FROM t1 }
125 execsql { SELECT * FROM t1 }
129 execsql { PRAGMA wal_checkpoint }
130 sqlite3_db_config db NO_CKPT_ON_CLOSE 1
134 sqlite3_snapshot_recover db main
136 list [catch { sqlite3_snapshot_open_blob db main $snap } msg] $msg
137 } {1 SQLITE_BUSY_SNAPSHOT}
139 #-------------------------------------------------------------------------
140 # Check that calling sqlite3_snapshot_recover() does not confuse the
143 do_execsql_test 3.0 {
144 PRAGMA journal_mode = wal;
145 CREATE TABLE t1(x, y);
146 INSERT INTO t1 VALUES('a', 'b');
147 INSERT INTO t1 VALUES('c', 'd');
151 execsql { INSERT INTO t1 VALUES('e', 'f') } db2
153 sqlite3_snapshot_recover db main
155 do_execsql_test 3.2 {
159 #-------------------------------------------------------------------------
160 # Check that sqlite3_snapshot_recover() returns an error if it is called
161 # with an open read-transaction. Or on a database that does not exist. Or
162 # on the temp database. Or on a db that is not in wal mode.
165 sqlite3_snapshot_recover db main
170 SELECT * FROM sqlite_master;
172 list [catch { sqlite3_snapshot_recover db main } msg] $msg
176 sqlite3_snapshot_recover db main
179 list [catch { sqlite3_snapshot_recover db aux } msg] $msg
184 ATTACH 'test.db2' AS aux;
185 PRAGMA aux.journal_mode = wal;
186 CREATE TABLE aux.t2(x, y);
188 list [catch { sqlite3_snapshot_recover db aux } msg] $msg
191 list [catch { sqlite3_snapshot_recover db temp } msg] $msg
195 PRAGMA aux.journal_mode = delete;
197 list [catch { sqlite3_snapshot_recover db aux } msg] $msg
200 #-------------------------------------------------------------------------
203 do_execsql_test 5.0 {
205 PRAGMA journal_mode = wal;
206 INSERT INTO t2 VALUES('abc');
207 INSERT INTO t2 VALUES('def');
208 INSERT INTO t2 VALUES('ghi');
216 set snap [sqlite3_snapshot_get_blob db2 main]
222 sqlite3_snapshot_open_blob db2 main $snap
223 db2 eval { SELECT * FROM t2 ; END }
227 execsql { PRAGMA wal_checkpoint = RESTART }
229 sqlite3_snapshot_open_blob db2 main $snap
230 db2 eval { SELECT * FROM t2 ; END }
234 execsql { INSERT INTO t2 VALUES('jkl') }
236 list [catch { sqlite3_snapshot_open_blob db2 main $snap } msg] $msg
237 } {1 SQLITE_BUSY_SNAPSHOT}