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 source [file join [file dirname [info script]] rbu_common.tcl]
20 # Create a simple RBU database. That expects to write to a table:
22 # CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
24 proc create_rbu1 {filename} {
26 sqlite3 rbu1 $filename
28 CREATE TABLE data_t1(a, b, c, rbu_control);
29 INSERT INTO data_t1 VALUES(1, 2, 3, 0);
30 INSERT INTO data_t1 VALUES(2, 'two', 'three', 0);
31 INSERT INTO data_t1 VALUES(3, NULL, 8.2, 0);
37 # Create a simple RBU database. That expects to write to a table:
39 # CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
41 # This RBU includes both insert and delete operations.
43 proc create_rbu4 {filename} {
45 sqlite3 rbu1 $filename
47 CREATE TABLE data_t1(a, b, c, rbu_control);
48 INSERT INTO data_t1 VALUES(1, 2, 3, 0);
49 INSERT INTO data_t1 VALUES(2, NULL, 5, 1);
50 INSERT INTO data_t1 VALUES(3, 8, 9, 0);
51 INSERT INTO data_t1 VALUES(4, NULL, 11, 1);
57 # Create a simple RBU database. That expects to write to a table:
59 # CREATE TABLE t1(c, b, '(a)' INTEGER PRIMARY KEY);
61 # This RBU includes both insert and delete operations.
63 proc create_rbu4b {filename} {
65 sqlite3 rbu1 $filename
67 CREATE TABLE data_t1(c, b, '(a)', rbu_control);
68 INSERT INTO data_t1 VALUES(3, 2, 1, 0);
69 INSERT INTO data_t1 VALUES(5, NULL, 2, 1);
70 INSERT INTO data_t1 VALUES(9, 8, 3, 0);
71 INSERT INTO data_t1 VALUES(11, NULL, 4, 1);
77 # Create a simple RBU database. That expects to write to a table:
79 # CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d);
81 # This RBU includes update statements.
83 proc create_rbu5 {filename} {
85 sqlite3 rbu5 $filename
87 CREATE TABLE data_t1(a, b, c, d, rbu_control);
88 INSERT INTO data_t1 VALUES(1, NULL, NULL, 5, '...x'); -- SET d = 5
89 INSERT INTO data_t1 VALUES(2, NULL, 10, 5, '..xx'); -- SET c=10, d = 5
90 INSERT INTO data_t1 VALUES(3, 11, NULL, NULL, '.x..'); -- SET b=11
97 # Same as [step_rbu], except using a URI to open the target db.
99 proc step_rbu_uri {target rbu} {
101 sqlite3rbu rbu file:$target?xyz=&abc=123 $rbu
104 if {$rc != "SQLITE_OK"} break
109 # Same as [step_rbu], except using an external state database - "state.db"
111 proc step_rbu_state {target rbu} {
113 sqlite3rbu rbu $target $rbu state.db
116 if {$rc != "SQLITE_OK"} break
121 proc dbfilecksum {file} {
123 set cksum [dbcksum ck main]
128 foreach {tn3 create_vfs destroy_vfs} {
131 sqlite3rbu_create_vfs -default myrbu ""
133 sqlite3rbu_destroy_vfs myrbu
141 2 step_rbu 3 step_rbu_uri 4 step_rbu_state
144 foreach {tn schema} {
146 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
149 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
150 CREATE INDEX i1 ON t1(b);
153 CREATE TABLE t1(a PRIMARY KEY, b, c) WITHOUT ROWID;
156 CREATE TABLE t1(a PRIMARY KEY, b, c) WITHOUT ROWID;
157 CREATE INDEX i1 ON t1(b);
160 CREATE TABLE t1(a, b, c, PRIMARY KEY(a, c)) WITHOUT ROWID;
161 CREATE INDEX i1 ON t1(b);
164 CREATE TABLE t1(a, b, c, PRIMARY KEY(c)) WITHOUT ROWID;
165 CREATE INDEX i1 ON t1(b, a);
168 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
169 CREATE INDEX i1 ON t1(b, c);
170 CREATE INDEX i2 ON t1(c, b);
171 CREATE INDEX i3 ON t1(a, b, c, a, b, c);
175 CREATE TABLE t1(a PRIMARY KEY, b, c);
176 CREATE INDEX i1 ON t1(b, c);
177 CREATE INDEX i2 ON t1(c, b);
178 CREATE INDEX i3 ON t1(a, b, c, a, b, c);
182 CREATE TABLE t1(a, b, c, PRIMARY KEY(a, c));
183 CREATE INDEX i1 ON t1(b);
187 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
188 CREATE INDEX i1 ON t1(b DESC);
192 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
193 CREATE INDEX i1 ON t1(b DESC, a ASC, c DESC);
197 CREATE TABLE t1(a INT PRIMARY KEY DESC, b, c) WITHOUT ROWID;
201 CREATE TABLE t1(a INT, b, c, PRIMARY KEY(a DESC)) WITHOUT ROWID;
205 CREATE TABLE t1(a, b, c, PRIMARY KEY(a DESC, c)) WITHOUT ROWID;
206 CREATE INDEX i1 ON t1(b);
210 CREATE TABLE t1(a, b, c, PRIMARY KEY(a, c DESC)) WITHOUT ROWID;
211 CREATE INDEX i1 ON t1(b);
215 CREATE TABLE t1(a, b, c, PRIMARY KEY(c DESC, a)) WITHOUT ROWID;
216 CREATE INDEX i1 ON t1(b DESC, c, a);
222 set check [dbfilecksum rbu.db]
225 do_test $tn3.1.$tn2.$tn.1 {
229 do_execsql_test $tn3.1.$tn2.$tn.2 { SELECT * FROM t1 ORDER BY a ASC } {
234 do_execsql_test $tn3.1.$tn2.$tn.3 { SELECT * FROM t1 ORDER BY b ASC } {
239 do_execsql_test $tn3.1.$tn2.$tn.4 { SELECT * FROM t1 ORDER BY c ASC } {
245 do_execsql_test $tn3.1.$tn2.$tn.5 { PRAGMA integrity_check } ok
247 if {$cmd=="step_rbu_state"} {
248 do_test $tn3.1.$tn2.$tn.6 { file exists state.db } 1
249 do_test $tn3.1.$tn2.$tn.7 { expr {$check == [dbfilecksum rbu.db]} } 1
251 do_test $tn3.1.$tn2.$tn.8 { file exists state.db } 0
252 do_test $tn3.1.$tn2.$tn.9 { expr {$check == [dbfilecksum rbu.db]} } 0
257 #-------------------------------------------------------------------------
258 # Check that an RBU cannot be applied to a table that has no PK.
260 # UPDATE: At one point RBU required that all tables featured either
261 # explicit IPK columns or were declared WITHOUT ROWID. This has been
262 # relaxed so that external PRIMARY KEYs on tables with automatic rowids
265 # UPDATE 2: Tables without any PRIMARY KEY declaration are now allowed.
266 # However the input table must feature an "rbu_rowid" column.
270 do_execsql_test $tn3.2.1 { CREATE TABLE t1(a, b, c) }
272 sqlite3rbu rbu test.db rbu.db
276 list [catch { rbu close } msg] $msg
277 } {1 {SQLITE_ERROR - table data_t1 requires rbu_rowid column}}
279 do_execsql_test $tn3.2.4 { CREATE TABLE t1(a PRIMARY KEY, b, c) }
281 sqlite3rbu rbu test.db rbu.db
285 list [catch { rbu close } msg] $msg
288 #-------------------------------------------------------------------------
289 # Check that if a UNIQUE constraint is violated the current and all
290 # subsequent [rbu step] calls return SQLITE_CONSTRAINT. And that the RBU
291 # transaction is rolled back by the [rbu close] that deletes the rbu
294 foreach {tn errcode errmsg schema} {
295 1 SQLITE_CONSTRAINT "UNIQUE constraint failed: t1.a" {
296 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
297 INSERT INTO t1 VALUES(3, 2, 1);
300 2 SQLITE_CONSTRAINT "UNIQUE constraint failed: t1.c" {
301 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c UNIQUE);
302 INSERT INTO t1 VALUES(4, 2, 'three');
305 3 SQLITE_CONSTRAINT "UNIQUE constraint failed: t1.a" {
306 CREATE TABLE t1(a PRIMARY KEY, b, c);
307 INSERT INTO t1 VALUES(3, 2, 1);
310 4 SQLITE_CONSTRAINT "UNIQUE constraint failed: t1.c" {
311 CREATE TABLE t1(a PRIMARY KEY, b, c UNIQUE);
312 INSERT INTO t1 VALUES(4, 2, 'three');
318 set cksum [dbcksum db main]
320 do_test $tn3.3.$tn.1 {
322 sqlite3rbu rbu test.db rbu.db
323 while {[set res [rbu step]]=="SQLITE_OK"} {}
327 do_test $tn3.3.$tn.2 { rbu step } $errcode
329 do_test $tn3.3.$tn.3 {
330 list [catch { rbu close } msg] $msg
331 } [list 1 "$errcode - $errmsg"]
333 do_test $tn3.3.$tn.4 { dbcksum db main } $cksum
336 #-------------------------------------------------------------------------
338 foreach {tn2 cmd} {1 run_rbu 2 step_rbu 3 step_rbu_state } {
339 foreach {tn schema} {
341 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
344 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
345 CREATE INDEX i1 ON t1(b);
348 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
349 CREATE INDEX i1 ON t1(b);
350 CREATE INDEX i2 ON t1(c, b);
351 CREATE INDEX i3 ON t1(c, b, c);
354 CREATE TABLE t1(a INT PRIMARY KEY, b, c) WITHOUT ROWID;
355 CREATE INDEX i1 ON t1(b);
356 CREATE INDEX i2 ON t1(c, b);
357 CREATE INDEX i3 ON t1(c, b, c);
360 CREATE TABLE t1(a INT PRIMARY KEY, b, c);
361 CREATE INDEX i1 ON t1(b);
362 CREATE INDEX i2 ON t1(c, b);
363 CREATE INDEX i3 ON t1(c, b, c);
367 CREATE TABLE t1(a INT PRIMARY KEY DESC, b, c);
368 CREATE INDEX i1 ON t1(b DESC);
369 CREATE INDEX i2 ON t1(c, b);
370 CREATE INDEX i3 ON t1(c DESC, b, c);
373 CREATE TABLE t1(a INT PRIMARY KEY DESC, b, c) WITHOUT ROWID;
374 CREATE INDEX i1 ON t1(b);
375 CREATE INDEX i2 ON t1(c, b);
376 CREATE INDEX i3 ON t1(c, b, c);
382 INSERT INTO t1 VALUES(2, 'hello', 'world');
383 INSERT INTO t1 VALUES(4, 'hello', 'planet');
384 INSERT INTO t1 VALUES(6, 'hello', 'xyz');
388 set check [dbfilecksum rbu.db]
391 do_test $tn3.4.$tn2.$tn.1 {
395 do_execsql_test $tn3.4.$tn2.$tn.2 {
396 SELECT * FROM t1 ORDER BY a ASC;
403 do_execsql_test $tn3.4.$tn2.$tn.3 { PRAGMA integrity_check } ok
405 if {$cmd=="step_rbu_state"} {
406 do_test $tn3.4.$tn2.$tn.4 { file exists state.db } 1
407 do_test $tn3.4.$tn2.$tn.5 { expr {$check == [dbfilecksum rbu.db]} } 1
409 do_test $tn3.4.$tn2.$tn.6 { file exists state.db } 0
410 do_test $tn3.4.$tn2.$tn.7 { expr {$check == [dbfilecksum rbu.db]} } 0
415 foreach {tn2 cmd} {1 run_rbu 2 step_rbu 3 step_rbu_state} {
416 foreach {tn schema} {
418 CREATE TABLE t1(c, b, '(a)' INTEGER PRIMARY KEY);
419 CREATE INDEX i1 ON t1(c, b);
422 CREATE TABLE t1(c, b, '(a)' PRIMARY KEY);
425 CREATE TABLE t1(c, b, '(a)' PRIMARY KEY) WITHOUT ROWID;
431 INSERT INTO t1('(a)', b, c) VALUES(2, 'hello', 'world');
432 INSERT INTO t1('(a)', b, c) VALUES(4, 'hello', 'planet');
433 INSERT INTO t1('(a)', b, c) VALUES(6, 'hello', 'xyz');
437 set check [dbfilecksum rbu.db]
440 do_test $tn3.5.$tn2.$tn.1 {
444 do_execsql_test $tn3.5.$tn2.$tn.2 {
445 SELECT * FROM t1 ORDER BY "(a)" ASC;
452 do_execsql_test $tn3.4.$tn2.$tn.3 { PRAGMA integrity_check } ok
454 if {$cmd=="step_rbu_state"} {
455 do_test $tn3.5.$tn2.$tn.4 { file exists state.db } 1
456 do_test $tn3.5.$tn2.$tn.5 { expr {$check == [dbfilecksum rbu.db]} } 1
458 do_test $tn3.5.$tn2.$tn.6 { file exists state.db } 0
459 do_test $tn3.5.$tn2.$tn.7 { expr {$check == [dbfilecksum rbu.db]} } 0
464 #-------------------------------------------------------------------------
466 foreach {tn2 cmd} {1 run_rbu 2 step_rbu 3 step_rbu_state} {
467 foreach {tn schema} {
469 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d);
472 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d);
473 CREATE INDEX i1 ON t1(d);
474 CREATE INDEX i2 ON t1(d, c);
475 CREATE INDEX i3 ON t1(d, c, b);
476 CREATE INDEX i4 ON t1(b);
477 CREATE INDEX i5 ON t1(c);
478 CREATE INDEX i6 ON t1(c, b);
481 CREATE TABLE t1(a PRIMARY KEY, b, c, d) WITHOUT ROWID;
482 CREATE INDEX i1 ON t1(d);
483 CREATE INDEX i2 ON t1(d, c);
484 CREATE INDEX i3 ON t1(d, c, b);
485 CREATE INDEX i4 ON t1(b);
486 CREATE INDEX i5 ON t1(c);
487 CREATE INDEX i6 ON t1(c, b);
490 CREATE TABLE t1(a PRIMARY KEY, b, c, d);
491 CREATE INDEX i1 ON t1(d);
492 CREATE INDEX i2 ON t1(d, c);
493 CREATE INDEX i3 ON t1(d, c, b);
494 CREATE INDEX i4 ON t1(b);
495 CREATE INDEX i5 ON t1(c);
496 CREATE INDEX i6 ON t1(c, b);
502 INSERT INTO t1 VALUES(1, 2, 3, 4);
503 INSERT INTO t1 VALUES(2, 5, 6, 7);
504 INSERT INTO t1 VALUES(3, 8, 9, 10);
508 set check [dbfilecksum rbu.db]
511 do_test $tn3.5.$tn2.$tn.1 {
515 do_execsql_test $tn3.5.$tn2.$tn.2 {
516 SELECT * FROM t1 ORDER BY a ASC;
523 do_execsql_test $tn3.6.$tn2.$tn.3 { PRAGMA integrity_check } ok
525 if {$cmd=="step_rbu_state"} {
526 do_test $tn3.6.$tn2.$tn.4 { file exists state.db } 1
527 do_test $tn3.6.$tn2.$tn.5 { expr {$check == [dbfilecksum rbu.db]} } 1
529 do_test $tn3.6.$tn2.$tn.6 { file exists state.db } 0
530 do_test $tn3.6.$tn2.$tn.7 { expr {$check == [dbfilecksum rbu.db]} } 0
535 #-------------------------------------------------------------------------
536 # Test some error cases:
538 # * A virtual table with no rbu_rowid column.
539 # * A no-PK table with no rbu_rowid column.
540 # * A PK table with an rbu_rowid column.
542 # 6: An update string of the wrong length
545 foreach {tn schema error} {
547 CREATE TABLE t1(a, b);
548 CREATE TABLE rbu.data_t1(a, b, rbu_control);
549 } {SQLITE_ERROR - table data_t1 requires rbu_rowid column}
552 CREATE VIRTUAL TABLE t1 USING fts4(a, b);
553 CREATE TABLE rbu.data_t1(a, b, rbu_control);
554 } {SQLITE_ERROR - table data_t1 requires rbu_rowid column}
557 CREATE TABLE t1(a PRIMARY KEY, b);
558 CREATE TABLE rbu.data_t1(a, b, rbu_rowid, rbu_control);
559 } {SQLITE_ERROR - table data_t1 may not have rbu_rowid column}
562 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
563 CREATE TABLE rbu.data_t1(a, b, rbu_rowid, rbu_control);
564 } {SQLITE_ERROR - table data_t1 may not have rbu_rowid column}
567 CREATE TABLE t1(a, b PRIMARY KEY) WITHOUT ROWID;
568 CREATE TABLE rbu.data_t1(a, b, rbu_rowid, rbu_control);
569 } {SQLITE_ERROR - table data_t1 may not have rbu_rowid column}
572 CREATE TABLE t1(a, b PRIMARY KEY) WITHOUT ROWID;
573 CREATE TABLE rbu.data_t1(a, b, rbu_control);
574 INSERT INTO rbu.data_t1 VALUES(1, 2, 'x.x');
575 } {SQLITE_ERROR - invalid rbu_control value}
578 CREATE TABLE t1(a, b PRIMARY KEY) WITHOUT ROWID;
579 CREATE TABLE rbu.data_t1(a, b, rbu_control);
580 INSERT INTO rbu.data_t1 VALUES(1, 2, NULL);
581 } {SQLITE_ERROR - invalid rbu_control value}
584 CREATE TABLE t1(a, b PRIMARY KEY) WITHOUT ROWID;
585 CREATE TABLE rbu.data_t1(a, b, rbu_control);
586 INSERT INTO rbu.data_t1 VALUES(1, 2, 4);
587 } {SQLITE_ERROR - invalid rbu_control value}
590 CREATE TABLE t1(a, b PRIMARY KEY) WITHOUT ROWID;
591 CREATE TABLE rbu.data_t1(a, b, rbu_control);
592 INSERT INTO rbu.data_t1 VALUES(1, 2, 3);
593 } {SQLITE_ERROR - invalid rbu_control value}
596 CREATE TABLE t2(a, b);
597 CREATE TABLE rbu.data_t1(a, b, rbu_control);
598 INSERT INTO rbu.data_t1 VALUES(1, 2, 2);
599 } {SQLITE_ERROR - no such table: t1}
602 CREATE TABLE rbu.data_t2(a, b, rbu_control);
603 INSERT INTO rbu.data_t2 VALUES(1, 2, 2);
604 } {SQLITE_ERROR - no such table: t2}
609 execsql { ATTACH 'rbu.db' AS rbu }
613 list [catch { run_rbu test.db rbu.db } msg] $msg
618 # Test that an RBU database containing no input tables is handled
623 list [catch { run_rbu test.db rbu.db } msg] $msg
626 # Test that an RBU database containing only empty data_xxx tables is
627 # also handled correctly.
630 do_execsql_test $tn3.8.2.1 {
631 CREATE TABLE t1(a PRIMARY KEY, b);
632 INSERT INTO t1 VALUES(1, 2);
633 ATTACH 'rbu.db' AS rbu;
634 CREATE TABLE data_t1(a, b, rbu_control);
638 list [catch { run_rbu test.db rbu.db } msg] $msg
641 # Test that RBU can update indexes containing NULL values.
645 do_execsql_test $tn3.9.1 {
646 CREATE TABLE t1(a PRIMARY KEY, b, c);
647 CREATE INDEX i1 ON t1(b, c);
648 INSERT INTO t1 VALUES(1, 1, NULL);
649 INSERT INTO t1 VALUES(2, NULL, 2);
650 INSERT INTO t1 VALUES(3, NULL, NULL);
652 ATTACH 'rbu.db' AS rbu;
653 CREATE TABLE rbu.data_t1(a, b, c, rbu_control);
654 INSERT INTO data_t1 VALUES(1, NULL, NULL, 1);
655 INSERT INTO data_t1 VALUES(3, NULL, NULL, 1);
659 list [catch { run_rbu test.db rbu.db } msg] $msg
662 do_execsql_test $tn3.9.3 {
665 do_execsql_test $tn3.9.4 { PRAGMA integrity_check } {ok}