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 # This file tests that the sessions module handles foreign key constraint
13 # violations when applying changesets as required.
16 if {![info exists testdir]} {
17 set testdir [file join [file dirname [info script]] .. .. test]
19 source [file join [file dirname [info script]] session_common.tcl]
20 source $testdir/tester.tcl
21 ifcapable !session {finish_test; return}
22 set testprefix session9
25 #--------------------------------------------------------------------
31 PRAGMA foreign_keys = 1;
32 CREATE TABLE p1(a PRIMARY KEY, b);
33 CREATE TABLE c1(a PRIMARY KEY, b REFERENCES p1);
34 CREATE TABLE c2(a PRIMARY KEY,
35 b REFERENCES p1 DEFERRABLE INITIALLY DEFERRED
38 INSERT INTO p1 VALUES(1, 'one');
39 INSERT INTO p1 VALUES(2, 'two');
40 INSERT INTO p1 VALUES(3, 'three');
41 INSERT INTO p1 VALUES(4, 'four');
45 proc capture_changeset {sql} {
46 sqlite3session S db main
48 foreach t [db eval {SELECT name FROM sqlite_master WHERE type='table'}] {
60 set cc [capture_changeset {
61 INSERT INTO c1 VALUES('ii', 2);
62 INSERT INTO c2 VALUES('iii', 3);
67 proc xConflict {args} {
68 lappend ::xConflict {*}$args
72 foreach {tn delrow trans conflictargs conflictret} {
73 1 2 0 {FOREIGN_KEY 1} OMIT
74 2 3 0 {FOREIGN_KEY 1} OMIT
75 3 2 1 {FOREIGN_KEY 1} OMIT
76 4 3 1 {FOREIGN_KEY 1} OMIT
77 5 2 0 {FOREIGN_KEY 1} ABORT
78 6 3 0 {FOREIGN_KEY 1} ABORT
79 7 2 1 {FOREIGN_KEY 1} ABORT
80 8 3 1 {FOREIGN_KEY 1} ABORT
84 set A(ABORT) {1 SQLITE_CONSTRAINT}
87 execsql { DELETE FROM p1 WHERE a=($delrow+0) }
88 if {$trans} { execsql BEGIN }
90 set ::xConflict [list]
91 list [catch {sqlite3changeset_apply db $::cc xConflict} msg] $msg
94 do_test 1.2.$tn.2 { set ::xConflict } $conflictargs
99 execsql { SELECT count(*) FROM c1 UNION ALL SELECT count(*) FROM c2 }
102 do_test 1.2.$tn.4 { expr ![sqlite3_get_autocommit db] } $trans
104 if { $trans } { execsql COMMIT }
108 #--------------------------------------------------------------------
109 # Test that closing a transaction clears the defer_foreign_keys flag.
111 foreach {tn open noclose close} {
115 3 {SAVEPOINT one} {} {RELEASE one}
116 4 {SAVEPOINT one} {ROLLBACK TO one} {RELEASE one}
119 do_execsql_test 2.$tn.1 { PRAGMA defer_foreign_keys } {0}
121 do_execsql_test 2.$tn.2 {
122 PRAGMA defer_foreign_keys = 1;
123 PRAGMA defer_foreign_keys;
127 do_execsql_test 2.$tn.3 { PRAGMA defer_foreign_keys } {1}
130 do_execsql_test 2.$tn.4 { PRAGMA defer_foreign_keys } {0}
133 #--------------------------------------------------------------------
134 # Test that a cyclic relationship can be inserted and deleted.
136 # This situation does not come up in practice, but testing it serves to
137 # show that it does not matter which order parent and child keys
138 # are processed in internally when applying a changeset.
142 do_execsql_test 3.1 {
143 CREATE TABLE t1(a PRIMARY KEY, b);
144 CREATE TABLE t2(x PRIMARY KEY, y);
147 # Create changesets as follows:
149 # $cc1 - Insert a row into t1.
150 # $cc2 - Insert a row into t2.
151 # $cc - Combination of $cc1 and $cc2.
153 # $ccdel1 - Delete the row from t1.
154 # $ccdel2 - Delete the row from t2.
155 # $ccdel - Combination of $cc1 and $cc2.
158 set cc1 [capture_changeset {
159 INSERT INTO t1 VALUES('one', 'value one');
161 set ccdel1 [capture_changeset { DELETE FROM t1; }]
162 set cc2 [capture_changeset {
163 INSERT INTO t2 VALUES('value one', 'one');
165 set ccdel2 [capture_changeset { DELETE FROM t2; }]
166 set cc [capture_changeset {
167 INSERT INTO t1 VALUES('one', 'value one');
168 INSERT INTO t2 VALUES('value one', 'one');
170 set ccdel [capture_changeset {
177 # Now modify the database schema to create a cyclic foreign key dependency
178 # between tables t1 and t2. This means that although changesets $cc and
179 # $ccdel can be applied, none of the others may without violating the
180 # foreign key constraints.
186 CREATE TABLE t1(a PRIMARY KEY, b REFERENCES t2);
187 CREATE TABLE t2(x PRIMARY KEY, y REFERENCES t1);
191 proc conflict_handler {args} { return "ABORT" }
192 sqlite3changeset_apply db $cc conflict_handler
198 } {one {value one} {value one} one}
201 list [catch {sqlite3changeset_apply db $::ccdel1 conflict_handler} msg] $msg
202 } {1 SQLITE_CONSTRAINT}
205 list [catch {sqlite3changeset_apply db $::ccdel2 conflict_handler} msg] $msg
206 } {1 SQLITE_CONSTRAINT}
209 list [catch {sqlite3changeset_apply db $::ccdel conflict_handler} msg] $msg
211 do_execsql_test 3.3.4.2 {
217 list [catch {sqlite3changeset_apply db $::cc1 conflict_handler} msg] $msg
218 } {1 SQLITE_CONSTRAINT}
220 list [catch {sqlite3changeset_apply db $::cc2 conflict_handler} msg] $msg
221 } {1 SQLITE_CONSTRAINT}
223 #--------------------------------------------------------------------
224 # Test that if a change that affects FK processing is not applied
225 # due to a separate constraint, SQLite does not get confused and
226 # increment FK counters anyway.
229 do_execsql_test 4.1 {
230 CREATE TABLE p1(x PRIMARY KEY, y);
231 CREATE TABLE c1(a PRIMARY KEY, b REFERENCES p1);
232 INSERT INTO p1 VALUES(1,1);
235 do_execsql_test 4.2.1 {
237 PRAGMA defer_foreign_keys = 1;
238 INSERT INTO c1 VALUES('x', 'x');
240 do_catchsql_test 4.2.2 { COMMIT } {1 {FOREIGN KEY constraint failed}}
241 do_catchsql_test 4.2.3 { ROLLBACK } {0 {}}
243 do_execsql_test 4.3.1 {
245 PRAGMA defer_foreign_keys = 1;
246 INSERT INTO c1 VALUES(1, 1);
248 do_catchsql_test 4.3.2 {
249 INSERT INTO c1 VALUES(1, 'x')
250 } {1 {UNIQUE constraint failed: c1.a}}
252 do_catchsql_test 4.3.3 { COMMIT } {0 {}}
253 do_catchsql_test 4.3.4 { BEGIN ; COMMIT } {0 {}}
255 #--------------------------------------------------------------------
256 # Test that if a DELETE change cannot be applied due to an
257 # SQLITE_CONSTRAINT error thrown by a trigger program, things do not
262 do_execsql_test 5.1 {
263 CREATE TABLE x1(x PRIMARY KEY, y);
264 CREATE TABLE x2(x PRIMARY KEY, y);
265 INSERT INTO x2 VALUES(1, 1);
266 INSERT INTO x1 VALUES(1, 1);
269 set ::cc [changeset_from_sql { DELETE FROM x1; }]
271 do_execsql_test 5.2 {
272 INSERT INTO x1 VALUES(1, 1);
273 CREATE TRIGGER tr1 AFTER DELETE ON x1 BEGIN
274 INSERT INTO x2 VALUES(old.x, old.y);
278 proc conflict_handler {args} { return "ABORT" }
280 list [catch {sqlite3changeset_apply db $::cc conflict_handler} msg] $msg
283 do_execsql_test 5.4 {