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 #***********************************************************************
12 # Tests for the [sqldiff --rbu] command.
15 if {![info exists testdir]} {
16 set testdir [file join [file dirname [info script]] .. .. test]
18 source $testdir/tester.tcl
19 set testprefix rbudiff
21 set PROG [test_find_sqldiff]
24 proc get_rbudiff_sql {db1 db2} {
25 exec $::PROG --rbu $db1 $db2
28 proc get_vtab_rbudiff_sql {db1 db2} {
29 exec $::PROG --vtab --rbu $db1 $db2
32 proc step_rbu {target rbu} {
34 sqlite3rbu rbu $target $rbu
37 if {$rc != "SQLITE_OK"} break
42 proc apply_rbudiff {sql target} {
48 step_rbu $target rbu.db
52 set ret "'[string map {' ''} $id]'"
56 # The only argument is the output of an [sqldiff -rbu] run. This command
57 # tests that the contents of the rbu_count table is correct. An exception
58 # is thrown if it is not.
60 proc test_rbucount {sql} {
64 SELECT name FROM sqlite_master WHERE name LIKE 'data%' AND type='table'
66 set a [tmpdb eval "SELECT count(*) FROM [sqlesc $name]"]
67 set b [tmpdb eval {SELECT cnt FROM rbu_count WHERE tbl = $name}]
70 error "rbu_count error - tbl = $name"
77 proc rbudiff_cksum {db1} {
81 foreach tbl [dbtmp eval {SELECT name FROM sqlite_master WHERE type='table'}] {
83 dbtmp eval "PRAGMA table_info = [sqlesc $tbl]" {
84 lappend cols "quote( $name )"
86 append txt [dbtmp eval \
87 "SELECT [join $cols {||'.'||}] FROM [sqlesc $tbl] ORDER BY 1"
95 foreach {tn init mod} {
97 CREATE TABLE t1(a PRIMARY KEY, b, c);
98 INSERT INTO t1 VALUES(1, 2, 3);
99 INSERT INTO t1 VALUES(4, 5, 6);
101 CREATE TABLE t2(a, b, c, PRIMARY KEY(b, c));
102 INSERT INTO t2 VALUES(1, 2, 3);
103 INSERT INTO t2 VALUES(4, 5, 6);
105 INSERT INTO t1 VALUES(7, 8, 9);
106 DELETE FROM t1 WHERE a=4;
107 UPDATE t1 SET c = 11 WHERE a = 1;
109 INSERT INTO t2 VALUES(7, 8, 9);
110 DELETE FROM t2 WHERE a=4;
111 UPDATE t2 SET c = 11 WHERE a = 1;
115 CREATE TABLE t1(a, b, c, PRIMARY KEY(a, b, c));
116 INSERT INTO t1 VALUES('u', 'v', 'w');
117 INSERT INTO t1 VALUES('x', 'y', 'z');
119 DELETE FROM t1 WHERE a='u';
120 INSERT INTO t1 VALUES('a', 'b', 'c');
124 CREATE TABLE t1(i INTEGER PRIMARY KEY, x);
125 INSERT INTO t1 VALUES(1,
126 X'0000000000000000111111111111111122222222222222223333333333333333'
128 CREATE TABLE t2(y INTEGER PRIMARY KEY, x);
129 INSERT INTO t2 VALUES(1,
130 X'0000000000000000111111111111111122222222222222223333333333333333'
134 INSERT INTO t1 VALUES(1,
135 X'0000000000000000111111111111111122222555555552223333333333333333'
138 INSERT INTO t2 VALUES(1,
139 X'0000000000000000111111111111111122222222222222223333333FFF333333'
144 CREATE TABLE x1(a, b, c, PRIMARY KEY(a, b, c));
145 INSERT INTO x1 VALUES('u', 'v', NULL);
146 INSERT INTO x1 VALUES('x', 'y', 'z');
147 INSERT INTO x1 VALUES('a', NULL, 'b');
149 INSERT INTO x1 VALUES('a', 'b', 'c');
153 CREATE TABLE t1(a PRIMARY KEY, b);
154 INSERT INTO t1 VALUES(1, NULL);
155 INSERT INTO t1 VALUES(2, X'');
157 UPDATE t1 SET b = X'' WHERE a=1;
158 UPDATE t1 SET b = NULL WHERE a=2;
164 forcedelete test.db test.db2
168 db eval "$init ; $mod"
172 set sql [get_rbudiff_sql test.db test.db2]
173 apply_rbudiff $sql test.db
175 do_test 1.$tn.3 { rbudiff_cksum test.db } [rbudiff_cksum test.db2]
177 forcedelete test.db test.db2
179 db eval "$init ; $mod"
185 set sql [get_rbudiff_sql test.db test.db2]
186 apply_rbudiff $sql test.db
188 do_test 1.$tn.5 { rbudiff_cksum test.db } [rbudiff_cksum test.db2]
191 #-------------------------------------------------------------------------
192 # Test that if the --vtab switch is present, [sqldiff] handles virtual
193 # table types fts[345] and rtree correctly.
195 ifcapable fts3&&fts5&&rtree {
197 foreach {tn init mod} {
199 CREATE VIRTUAL TABLE t1 USING fts5(c);
200 INSERT INTO t1 VALUES('a b c');
201 INSERT INTO t1 VALUES('a b c');
203 DELETE FROM t1 WHERE rowid = 1;
204 INSERT INTO t1 VALUES('a b c');
208 CREATE VIRTUAL TABLE "x y" USING 'rtree'(id, x1, x2);
209 INSERT INTO "x y" VALUES(1, 2, 3);
210 INSERT INTO "x y" VALUES(2, 4, 6);
212 DELETE FROM "x y" WHERE rowid = 1;
213 INSERT INTO "x y" VALUES(3, 6, 9);
217 CREATE VIRTUAL TABLE 'x''y' USING fts3;
218 INSERT INTO 'x''y' VALUES('one two three');
219 INSERT INTO 'x''y' VALUES('four five six');
221 DELETE FROM 'x''y' WHERE rowid = 1;
222 INSERT INTO 'x''y' VALUES('one two three');
226 forcedelete test.db test.db2
230 db eval "$init ; $mod"
234 set sql [get_vtab_rbudiff_sql test.db test.db2]
235 apply_rbudiff $sql test.db
237 do_test 2.$tn.2 { rbudiff_cksum test.db } [rbudiff_cksum test.db2]
243 foreach {tn init mod} {
245 CREATE VIRTUAL TABLE t1 USING fts5(c);
246 INSERT INTO t1 VALUES('a b c');
247 INSERT INTO t1 VALUES('a b c');
249 DELETE FROM t1 WHERE rowid = 1;
250 INSERT INTO t1 VALUES('a b c');
254 CREATE VIRTUAL TABLE t1 USING FTs5(c);
255 INSERT INTO t1 VALUES('a b c');
256 INSERT INTO t1 VALUES('a b c');
258 DELETE FROM t1 WHERE rowid = 1;
259 INSERT INTO t1 VALUES('a b c');
264 tablE t1 USING FTs5(c);
265 INSERT INTO t1 VALUES('a b c');
266 INSERT INTO t1 VALUES('a b c');
268 DELETE FROM t1 WHERE rowid = 1;
269 INSERT INTO t1 VALUES('a b c');
273 forcedelete test.db test.db2
277 db eval "$init ; $mod"
278 db eval { INSERT INTO t1(t1) VALUES('optimize') }
282 set sql [get_vtab_rbudiff_sql test.db test.db2]
283 apply_rbudiff $sql test.db
289 db2 eval { SELECT * FROM t1 ORDER BY rowid }
290 } [db eval { SELECT * FROM t1 ORDER BY rowid }]
293 db2 eval { INSERT INTO t1(t1) VALUES('integrity-check') }