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.
16 source [file join [file dirname [info script]] rbu_common.tcl]
17 if_no_rbu_support { finish_test ; return }
18 set testprefix rbudiff
20 set PROG [test_find_sqldiff]
23 proc get_rbudiff_sql {db1 db2} {
24 exec $::PROG --rbu $db1 $db2
27 proc get_vtab_rbudiff_sql {db1 db2} {
28 exec $::PROG --vtab --rbu $db1 $db2
31 proc step_rbu {target rbu} {
33 sqlite3rbu rbu $target $rbu
36 if {$rc != "SQLITE_OK"} break
41 proc apply_rbudiff {sql target} {
47 step_rbu $target rbu.db
51 set ret "'[string map {' ''} $id]'"
55 # The only argument is the output of an [sqldiff -rbu] run. This command
56 # tests that the contents of the rbu_count table is correct. An exception
57 # is thrown if it is not.
59 proc test_rbucount {sql} {
63 SELECT name FROM sqlite_master WHERE name LIKE 'data%' AND type='table'
65 set a [tmpdb eval "SELECT count(*) FROM [sqlesc $name]"]
66 set b [tmpdb eval {SELECT cnt FROM rbu_count WHERE tbl = $name}]
69 error "rbu_count error - tbl = $name"
76 proc rbudiff_cksum {db1} {
80 foreach tbl [dbtmp eval {SELECT name FROM sqlite_master WHERE type='table'}] {
82 dbtmp eval "PRAGMA table_info = [sqlesc $tbl]" {
83 lappend cols "quote( $name )"
85 append txt [dbtmp eval \
86 "SELECT [join $cols {||'.'||}] FROM [sqlesc $tbl] ORDER BY 1"
94 foreach {tn init mod} {
96 CREATE TABLE t1(a PRIMARY KEY, b, c);
97 INSERT INTO t1 VALUES(1, 2, 3);
98 INSERT INTO t1 VALUES(4, 5, 6);
100 CREATE TABLE t2(a, b, c, PRIMARY KEY(b, c));
101 INSERT INTO t2 VALUES(1, 2, 3);
102 INSERT INTO t2 VALUES(4, 5, 6);
104 INSERT INTO t1 VALUES(7, 8, 9);
105 DELETE FROM t1 WHERE a=4;
106 UPDATE t1 SET c = 11 WHERE a = 1;
108 INSERT INTO t2 VALUES(7, 8, 9);
109 DELETE FROM t2 WHERE a=4;
110 UPDATE t2 SET c = 11 WHERE a = 1;
114 CREATE TABLE t1(a, b, c, PRIMARY KEY(a, b, c));
115 INSERT INTO t1 VALUES('u', 'v', 'w');
116 INSERT INTO t1 VALUES('x', 'y', 'z');
118 DELETE FROM t1 WHERE a='u';
119 INSERT INTO t1 VALUES('a', 'b', 'c');
123 CREATE TABLE t1(i INTEGER PRIMARY KEY, x);
124 INSERT INTO t1 VALUES(1,
125 X'0000000000000000111111111111111122222222222222223333333333333333'
127 CREATE TABLE t2(y INTEGER PRIMARY KEY, x);
128 INSERT INTO t2 VALUES(1,
129 X'0000000000000000111111111111111122222222222222223333333333333333'
133 INSERT INTO t1 VALUES(1,
134 X'0000000000000000111111111111111122222555555552223333333333333333'
137 INSERT INTO t2 VALUES(1,
138 X'0000000000000000111111111111111122222222222222223333333FFF333333'
143 CREATE TABLE x1(a, b, c, PRIMARY KEY(a, b, c));
144 INSERT INTO x1 VALUES('u', 'v', NULL);
145 INSERT INTO x1 VALUES('x', 'y', 'z');
146 INSERT INTO x1 VALUES('a', NULL, 'b');
148 INSERT INTO x1 VALUES('a', 'b', 'c');
152 CREATE TABLE t1(a PRIMARY KEY, b);
153 INSERT INTO t1 VALUES(1, NULL);
154 INSERT INTO t1 VALUES(2, X'');
156 UPDATE t1 SET b = X'' WHERE a=1;
157 UPDATE t1 SET b = NULL WHERE a=2;
163 forcedelete test.db test.db2
167 db eval "$init ; $mod"
171 set sql [get_rbudiff_sql test.db test.db2]
172 apply_rbudiff $sql test.db
174 do_test 1.$tn.3 { rbudiff_cksum test.db } [rbudiff_cksum test.db2]
176 forcedelete test.db test.db2
178 db eval "$init ; $mod"
184 set sql [get_rbudiff_sql test.db test.db2]
185 apply_rbudiff $sql test.db
187 do_test 1.$tn.5 { rbudiff_cksum test.db } [rbudiff_cksum test.db2]
190 #-------------------------------------------------------------------------
191 # Test that if the --vtab switch is present, [sqldiff] handles virtual
192 # table types fts[345] and rtree correctly.
194 ifcapable fts3&&fts5&&rtree {
196 foreach {tn init mod} {
198 CREATE VIRTUAL TABLE t1 USING fts5(c);
199 INSERT INTO t1 VALUES('a b c');
200 INSERT INTO t1 VALUES('a b c');
202 DELETE FROM t1 WHERE rowid = 1;
203 INSERT INTO t1 VALUES('a b c');
207 CREATE VIRTUAL TABLE "x y" USING 'rtree'(id, x1, x2);
208 INSERT INTO "x y" VALUES(1, 2, 3);
209 INSERT INTO "x y" VALUES(2, 4, 6);
211 DELETE FROM "x y" WHERE rowid = 1;
212 INSERT INTO "x y" VALUES(3, 6, 9);
216 CREATE VIRTUAL TABLE 'x''y' USING fts3;
217 INSERT INTO 'x''y' VALUES('one two three');
218 INSERT INTO 'x''y' VALUES('four five six');
220 DELETE FROM 'x''y' WHERE rowid = 1;
221 INSERT INTO 'x''y' VALUES('one two three');
225 forcedelete test.db test.db2
229 db eval "$init ; $mod"
233 set sql [get_vtab_rbudiff_sql test.db test.db2]
234 apply_rbudiff $sql test.db
236 do_test 2.$tn.2 { rbudiff_cksum test.db } [rbudiff_cksum test.db2]
242 foreach {tn init mod} {
244 CREATE VIRTUAL TABLE t1 USING fts5(c);
245 INSERT INTO t1 VALUES('a b c');
246 INSERT INTO t1 VALUES('a b c');
248 DELETE FROM t1 WHERE rowid = 1;
249 INSERT INTO t1 VALUES('a b c');
253 CREATE VIRTUAL TABLE t1 USING FTs5(c);
254 INSERT INTO t1 VALUES('a b c');
255 INSERT INTO t1 VALUES('a b c');
257 DELETE FROM t1 WHERE rowid = 1;
258 INSERT INTO t1 VALUES('a b c');
263 tablE t1 USING FTs5(c);
264 INSERT INTO t1 VALUES('a b c');
265 INSERT INTO t1 VALUES('a b c');
267 DELETE FROM t1 WHERE rowid = 1;
268 INSERT INTO t1 VALUES('a b c');
271 creAte virTUal tablE t1 USING FTs5(c);
272 INSERT INTO t1 VALUES('a b c');
273 INSERT INTO t1 VALUES('a b c');
275 DELETE FROM t1 WHERE rowid = 1;
276 INSERT INTO t1 VALUES('a b c');
280 forcedelete test.db test.db2
284 db eval "$init ; $mod"
285 db eval { INSERT INTO t1(t1) VALUES('optimize') }
289 set sql [get_vtab_rbudiff_sql test.db test.db2]
290 apply_rbudiff $sql test.db
296 db2 eval { SELECT * FROM t1 ORDER BY rowid }
297 } [db eval { SELECT * FROM t1 ORDER BY rowid }]
300 db2 eval { INSERT INTO t1(t1) VALUES('integrity-check') }