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 #***********************************************************************
14 if {![info exists testdir]} {
15 set testdir [file join [file dirname [info script]] .. .. test]
17 source [file join [file dirname [info script]] session_common.tcl]
18 source $testdir/tester.tcl
19 ifcapable !session {finish_test; return}
21 set testprefix sessionC
23 #-------------------------------------------------------------------------
24 # Test the outcome of a DELETE operation made as part of applying a
25 # changeset failing with SQLITE_CONSTRAINT. This may happen if an
26 # ON DELETE RESTRICT foreign key action is triggered, or if a trigger
27 # program raises a constraint somehow.
29 # UPDATE: The above is no longer true, as "PRAGMA defer_foreign_keys"
30 # now disables "RESTRICT" processing. The test below has been rewritten
31 # to use a trigger instead of a foreign key to test this case.
34 PRAGMA foreign_keys = 1;
36 CREATE TABLE p(a PRIMARY KEY, b, c);
37 CREATE TABLE c(d PRIMARY KEY, e /* REFERENCES p ON DELETE RESTRICT */);
39 CREATE TRIGGER restrict_trig BEFORE DELETE ON p BEGIN
40 SELECT raise(ABORT, 'error!') FROM c WHERE e=old.a;
43 INSERT INTO p VALUES('one', 1, 1);
44 INSERT INTO p VALUES('two', 2, 2);
45 INSERT INTO p VALUES('three', 3, 3);
47 INSERT INTO c VALUES(1, 'one');
48 INSERT INTO c VALUES(3, 'three');
53 set C [changeset_from_sql {
54 INSERT INTO c VALUES(4, 'one');
55 DELETE FROM p WHERE a='two';
59 INSERT INTO c VALUES(2, 'two');
64 proc xConflict {args} { return "ABORT" }
65 catch { sqlite3changeset_apply db $C xConflict } msg
68 do_execsql_test 1.2.2 { SELECT * FROM c } {1 one 3 three 2 two}
71 proc xConflict {args} { return "OMIT" }
72 catch { sqlite3changeset_apply db $C xConflict } msg
75 do_execsql_test 1.3.2 { SELECT * FROM c } {1 one 3 three 2 two 4 one}
76 do_execsql_test 1.3.3 {
78 } {one 1 1 two 2 2 three 3 3}
81 #-------------------------------------------------------------------------
82 # Test that concatenating a changeset with a patchset does not work.
83 # Any attempt to do so returns SQLITE_ERROR.
87 CREATE TABLE x1(t, v PRIMARY KEY);
88 INSERT INTO x1 VALUES(12, 55);
89 INSERT INTO x1 VALUES(55, 14);
95 sqlite3session S1 db main
98 UPDATE x1 SET t=13 WHERE v=55;
99 INSERT INTO x1 VALUES(99, 123);
101 set patchset [S1 patchset]
104 sqlite3session S1 db main
107 UPDATE x1 SET t=56 WHERE v=14;
108 INSERT INTO x1 VALUES(22, 998);
110 set changeset [S1 changeset]
117 set rc [catch { sqlite3changeset_concat $patchset $changeset } msg]
122 set rc [catch { sqlite3changeset_concat $changeset $patchset } msg]
127 set rc [catch { sqlite3changeset_concat {} $patchset } msg]
132 set rc [catch { sqlite3changeset_concat $patchset {} } msg]
137 set rc [catch { sqlite3changeset_concat {} $changeset } msg]
139 } [list 0 $changeset]
142 set rc [catch { sqlite3changeset_concat $changeset {} } msg]
144 } [list 0 $changeset]
147 set rc [catch { sqlite3changeset_concat {} {} } msg]
152 #-------------------------------------------------------------------------
153 # Test that the xFilter argument to sqlite3changeset_apply() works.
156 do_execsql_test 3.0 {
157 CREATE TABLE t1(a PRIMARY KEY, b);
158 CREATE TABLE t2(a PRIMARY KEY, b);
159 CREATE TABLE t3(a PRIMARY KEY, b);
163 set changeset [changeset_from_sql {
164 INSERT INTO t1 VALUES(1, 1);
165 INSERT INTO t2 VALUES(2, 2);
166 INSERT INTO t3 VALUES(3, 3);
171 proc xFilter {zName} {
172 if {$zName == "t1"} { return 1 }
175 sqlite3changeset_apply db $changeset noop xFilter
183 proc xFilter {zName} {
184 if {$zName == "t3"} { return 1 }
187 sqlite3changeset_apply db $changeset noop xFilter