interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / snapshot_up.test

185 lines · 4357 bytes Raw Blame History
1# 2018 August 6
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
12# Tests for calling sqlite3_snapshot_open() when there is already
13# a read transaction open on the database.
14#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18ifcapable !snapshot {finish_test; return}
19set testprefix snapshot_up
20
21# This test does not work with the inmemory_journal permutation. The reason
22# is that each connection opened as part of this permutation executes
23# "PRAGMA journal_mode=memory", which fails if the database is in wal mode
24# and there are one or more existing connections.
25if {[permutation]=="inmemory_journal"} {
26 finish_test
27 return
28}
29
30db timeout 1000
31
32do_execsql_test 1.0 {
33 CREATE TABLE t1(a, b, c);
34 PRAGMA journal_mode = wal;
35 INSERT INTO t1 VALUES(1, 2, 3);
36 INSERT INTO t1 VALUES(4, 5, 6);
37 INSERT INTO t1 VALUES(7, 8, 9);
38} {wal}
39
40do_test 1.1 {
41 execsql BEGIN
42 set ::snap1 [sqlite3_snapshot_get db main]
43 execsql COMMIT
44 execsql { INSERT INTO t1 VALUES(10, 11, 12); }
45 execsql BEGIN
46 set ::snap2 [sqlite3_snapshot_get db main]
47 execsql COMMIT
48 execsql { INSERT INTO t1 VALUES(13, 14, 15); }
49 execsql BEGIN
50 set ::snap3 [sqlite3_snapshot_get db main]
51 execsql COMMIT
52} {}
53
54do_execsql_test 1.2 {
55 BEGIN;
56 SELECT * FROM t1
57} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15}
58
59do_test 1.3 {
60 sqlite3_snapshot_open db main $::snap1
61 execsql { SELECT * FROM t1 }
62} {1 2 3 4 5 6 7 8 9}
63
64do_test 1.4 {
65 sqlite3_snapshot_open db main $::snap2
66 execsql { SELECT * FROM t1 }
67} {1 2 3 4 5 6 7 8 9 10 11 12}
68
69do_test 1.5 {
70 sqlite3 db2 test.db
71 execsql { PRAGMA wal_checkpoint } db2
72} {0 5 4}
73
74do_execsql_test 1.6 {
75 SELECT * FROM t1
76} {1 2 3 4 5 6 7 8 9 10 11 12}
77
78do_test 1.7 {
79 list [catch { sqlite3_snapshot_open db main $::snap1 } msg] $msg
80} {1 SQLITE_ERROR_SNAPSHOT}
81
82do_execsql_test 1.8 {
83 SELECT * FROM t1
84} {1 2 3 4 5 6 7 8 9 10 11 12}
85
86do_test 1.9 {
87 execsql { COMMIT ; BEGIN }
88 list [catch { sqlite3_snapshot_open db main $::snap1 } msg] $msg
89} {1 SQLITE_ERROR_SNAPSHOT}
90
91do_test 1.10 {
92 execsql { COMMIT }
93 execsql {
94 PRAGMA wal_checkpoint;
95 DELETE FROM t1 WHERE a = 1;
96 } db2
97 execsql BEGIN
98 set ::snap4 [sqlite3_snapshot_get db main]
99 execsql COMMIT
100 execsql {
101 DELETE FROM t1 WHERE a = 4;
102 } db2
103} {}
104
105do_test 1.11 {
106 execsql {
107 BEGIN;
108 SELECT * FROM t1
109 }
110} {7 8 9 10 11 12 13 14 15}
111do_test 1.12 {
112 sqlite3_snapshot_open db main $::snap4
113 execsql { SELECT * FROM t1 }
114} {4 5 6 7 8 9 10 11 12 13 14 15}
115
116do_test 1.13 {
117 list [catch { sqlite3_snapshot_open db main $::snap3 } msg] $msg
118} {1 SQLITE_ERROR_SNAPSHOT}
119do_test 1.14 {
120 execsql { SELECT * FROM t1 }
121} {4 5 6 7 8 9 10 11 12 13 14 15}
122
123db close
124db2 close
125sqlite3 db test.db
126do_execsql_test 1.15 {
127 BEGIN;
128 SELECT * FROM t1
129} {7 8 9 10 11 12 13 14 15}
130do_test 1.16 {
131 list [catch { sqlite3_snapshot_open db main $::snap4 } msg] $msg
132} {1 SQLITE_ERROR_SNAPSHOT}
133do_execsql_test 1.17 { COMMIT }
134
135sqlite3_snapshot_free $::snap1
136sqlite3_snapshot_free $::snap2
137sqlite3_snapshot_free $::snap3
138sqlite3_snapshot_free $::snap4
139
140#-------------------------------------------------------------------------
141catch { db close }
142sqlite3 db test.db
143sqlite3 db2 test.db
144sqlite3 db3 test.db
145
146proc xBusy {args} { return 1 }
147db3 busy xBusy
148
149do_test 2.1 {
150 execsql { INSERT INTO t1 VALUES(16, 17, 18) } db2
151 execsql BEGIN
152 set ::snap1 [sqlite3_snapshot_get db main]
153 execsql COMMIT
154 execsql { INSERT INTO t1 VALUES(19, 20, 21) } db2
155 execsql BEGIN
156 set ::snap2 [sqlite3_snapshot_get db main]
157 execsql COMMIT
158 set {} {}
159} {}
160
161do_execsql_test -db db2 2.2 {
162 BEGIN;
163 INSERT INTO t1 VALUES(19, 20, 21);
164}
165
166do_test 2.3 {
167 execsql BEGIN
168 sqlite3_snapshot_open db main $::snap1
169 execsql { SELECT * FROM t1 }
170} {7 8 9 10 11 12 13 14 15 16 17 18}
171
172proc xBusy {args} {
173 set ::res [list [catch { sqlite3_snapshot_open db main $::snap2 } msg] $msg]
174 return 1
175}
176db3 busy xBusy
177do_test 2.4 {
178 execsql {PRAGMA wal_checkpoint = restart} db3
179 set ::res
180} {1 SQLITE_BUSY}
181
182sqlite3_snapshot_free $::snap1
183sqlite3_snapshot_free $::snap2
184
185finish_test