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 #***********************************************************************
13 if {![info exists testdir]} {
14 set testdir [file join [file dirname [info script]] .. .. test]
16 source $testdir/tester.tcl
17 source $testdir/lock_common.tcl
18 set ::testprefix rbu12
21 DROP TABLE IF EXISTS xx;
22 DROP TABLE IF EXISTS xy;
23 CREATE TABLE xx(a, b, c PRIMARY KEY);
24 INSERT INTO xx VALUES(1, 2, 3);
25 CREATE TABLE xy(a, b, c PRIMARY KEY);
27 ATTACH 'rbu.db' AS rbu;
28 DROP TABLE IF EXISTS data_xx;
29 CREATE TABLE rbu.data_xx(a, b, c, rbu_control);
30 INSERT INTO data_xx VALUES(4, 5, 6, 0);
31 INSERT INTO data_xx VALUES(7, 8, 9, 0);
32 CREATE TABLE rbu.data_xy(a, b, c, rbu_control);
33 INSERT INTO data_xy VALUES(10, 11, 12, 0);
37 do_multiclient_test tn {
39 # Initialize a target (test.db) and rbu (rbu.db) database.
44 # Using connection 2, open a read transaction on the target database.
45 # RBU will still be able to generate "test.db-oal", but it will not be
46 # able to rename it to "test.db-wal".
49 sql2 { BEGIN; SELECT * FROM xx; }
52 sqlite3rbu rbu test.db rbu.db
55 if {$res!="SQLITE_OK"} break
60 do_test 1.$tn.3 { sql2 { SELECT * FROM xx; } } {1 2 3}
61 do_test 1.$tn.4 { sql2 { SELECT * FROM xy; } } {}
63 list [file exists test.db-wal] [file exists test.db-oal]
65 do_test 1.$tn.6 { sql2 COMMIT } {}
67 # The rbu object that hit the SQLITE_BUSY error above cannot be reused.
68 # It is stuck in a permanent SQLITE_BUSY state at this point.
70 do_test 1.$tn.7 { rbu step } {SQLITE_BUSY}
72 list [catch { rbu close } msg] $msg
75 do_test 1.$tn.9.1 { sql2 { BEGIN EXCLUSIVE } } {}
77 sqlite3rbu rbu test.db rbu.db
81 list [catch { rbu close } msg] $msg
82 } {1 {SQLITE_BUSY - database is locked}}
83 do_test 1.$tn.9.4 { sql2 COMMIT } {}
85 sqlite3rbu rbu test.db rbu.db
86 do_test 1.$tn.10.1 { sql2 { BEGIN EXCLUSIVE } } {}
91 list [catch { rbu close } msg] $msg
93 do_test 1.$tn.10.4 { sql2 COMMIT } {}
95 # A new rbu object can finish the work though.
98 sqlite3rbu rbu test.db rbu.db
102 list [file exists test.db-wal] [file exists test.db-oal]
107 if {$res!="SQLITE_OK"} break
117 do_multiclient_test tn {
119 # Initialize a target (test.db) and rbu (rbu.db) database.
125 sqlite3rbu rbu test.db rbu.db
126 while {[file exists test.db-wal]==0} {
127 if {[rbu step]!="SQLITE_OK"} {error "problem here...."}
133 do_test 2.$tn.2 { sql2 { BEGIN IMMEDIATE } } {}
136 sqlite3rbu rbu test.db rbu.db
140 do_test 2.$tn.4 { list [catch { rbu close } msg] $msg } {1 SQLITE_BUSY}
143 sql2 { SELECT * FROM xx ; COMMIT }
144 } {1 2 3 4 5 6 7 8 9}
147 sqlite3rbu rbu test.db rbu.db
152 do_test 2.$tn.7 { sql2 { BEGIN EXCLUSIVE } } {}
155 sqlite3rbu rbu test.db rbu.db
158 do_test 2.$tn.9 { list [catch { rbu close } msg] $msg } {1 SQLITE_BUSY}
160 sql2 { SELECT * FROM xx ; COMMIT }
161 } {1 2 3 4 5 6 7 8 9}
164 sqlite3rbu rbu test.db rbu.db
165 while {[rbu step]=="SQLITE_OK"} {}
171 #-------------------------------------------------------------------------
172 # Test that "PRAGMA data_version" works when an RBU client writes the
175 do_multiclient_test tn {
177 # Initialize a target (test.db) and rbu (rbu.db) database.
182 # Check the initial database contains table "xx" with a single row.
183 # Also save the current values of "PRAGMA data-version" for [db1]
187 list [sql1 { SELECT count(*) FROM xx }] [sql2 { SELECT count(*) FROM xx }]
189 set V1 [sql1 {PRAGMA data_version}]
190 set V2 [sql2 {PRAGMA data_version}]
192 # Check the values of data-version have not magically changed.
195 list [sql1 {PRAGMA data_version}] [sql2 {PRAGMA data_version}]
198 # Start stepping the RBU. From the point of view of [db1] and [db2], the
199 # data-version values remain unchanged until the database contents are
200 # modified. At which point the values are incremented.
202 sqlite3rbu rbu test.db rbu.db
204 while {[db one {SELECT count(*) FROM xx}]==1} {
205 do_test 2.$tn.3.[incr x] {
206 list [sql1 {PRAGMA data_version}] [sql2 {PRAGMA data_version}]
210 do_test 2.$tn.5.1 { expr {$V1 < [sql1 {PRAGMA data_version}]} } 1
211 do_test 2.$tn.5.2 { expr {$V2 < [sql2 {PRAGMA data_version}]} } 1
213 # Check the db contents is as expected.
216 list [sql1 {SELECT count(*) FROM xx}] [sql2 {SELECT count(*) FROM xx}]
219 set V1 [sql1 {PRAGMA data_version}]
220 set V2 [sql2 {PRAGMA data_version}]
222 # Finish applying the RBU (i.e. do the incremental checkpoint). Check that
223 # this does not cause the data-version values to change.
225 while {[rbu step]=="SQLITE_OK"} { }
229 list [sql1 {PRAGMA data_version}] [sql2 {PRAGMA data_version}]