OLD | NEW |
1 # 2013-07-11 | 1 # 2012 December 17 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
11 # This file implements regression tests for SQLite library. | 11 # This file implements regression tests for SQLite library. |
12 # | 12 # |
13 # This file tests the PRAGMA defer_foreign_keys and | 13 # This file tests the PRAGMA defer_foreign_keys and |
14 # SQLITE_DBSTATUS_DEFERRED_FKS | 14 # SQLITE_DBSTATUS_DEFERRED_FKS |
15 # | 15 # |
16 # EVIDENCE-OF: R-18981-16292 When the defer_foreign_keys PRAGMA is on, | 16 # EVIDENCE-OF: R-18981-16292 When the defer_foreign_keys PRAGMA is on, |
17 # enforcement of all foreign key constraints is delayed until the | 17 # enforcement of all foreign key constraints is delayed until the |
18 # outermost transaction is committed. | 18 # outermost transaction is committed. |
19 # | 19 # |
20 # EVIDENCE-OF: R-28911-57501 The defer_foreign_keys pragma defaults to | 20 # EVIDENCE-OF: R-28911-57501 The defer_foreign_keys pragma defaults to |
21 # OFF so that foreign key constraints are only deferred if they are | 21 # OFF so that foreign key constraints are only deferred if they are |
22 # created as "DEFERRABLE INITIALLY DEFERRED". | 22 # created as "DEFERRABLE INITIALLY DEFERRED". |
23 | 23 |
24 set testdir [file dirname $argv0] | 24 set testdir [file dirname $argv0] |
25 source $testdir/tester.tcl | 25 source $testdir/tester.tcl |
| 26 set testprefix fkey6 |
26 | 27 |
27 ifcapable {!foreignkey} { | 28 ifcapable {!foreignkey} { |
28 finish_test | 29 finish_test |
29 return | 30 return |
30 } | 31 } |
31 | 32 |
32 do_execsql_test fkey6-1.0 { | 33 do_execsql_test fkey6-1.0 { |
33 PRAGMA defer_foreign_keys; | 34 PRAGMA defer_foreign_keys; |
34 } {0} | 35 } {0} |
35 | 36 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 | 165 |
165 do_execsql_test fkey6-2.6 { | 166 do_execsql_test fkey6-2.6 { |
166 BEGIN; | 167 BEGIN; |
167 PRAGMA defer_foreign_keys = 1; | 168 PRAGMA defer_foreign_keys = 1; |
168 INSERT INTO c1 VALUES('three'); | 169 INSERT INTO c1 VALUES('three'); |
169 DROP TABLE c1; | 170 DROP TABLE c1; |
170 COMMIT; | 171 COMMIT; |
171 PRAGMA defer_foreign_keys; | 172 PRAGMA defer_foreign_keys; |
172 } {0} | 173 } {0} |
173 | 174 |
| 175 #-------------------------------------------------------------------------- |
| 176 # Test that defer_foreign_keys disables RESTRICT. |
| 177 # |
| 178 do_execsql_test 3.1 { |
| 179 CREATE TABLE p2(a PRIMARY KEY, b); |
| 180 CREATE TABLE c2(x, y REFERENCES p2 ON DELETE RESTRICT ON UPDATE RESTRICT); |
| 181 INSERT INTO p2 VALUES(1, 'one'); |
| 182 INSERT INTO p2 VALUES(2, 'two'); |
| 183 INSERT INTO c2 VALUES('i', 1); |
| 184 } |
| 185 |
| 186 do_catchsql_test 3.2.1 { |
| 187 BEGIN; |
| 188 UPDATE p2 SET a=a-1; |
| 189 } {1 {FOREIGN KEY constraint failed}} |
| 190 do_execsql_test 3.2.2 { COMMIT } |
| 191 |
| 192 do_execsql_test 3.2.3 { |
| 193 BEGIN; |
| 194 PRAGMA defer_foreign_keys = 1; |
| 195 UPDATE p2 SET a=a-1; |
| 196 COMMIT; |
| 197 } |
| 198 |
| 199 do_execsql_test 3.2.4 { |
| 200 BEGIN; |
| 201 PRAGMA defer_foreign_keys = 1; |
| 202 UPDATE p2 SET a=a-1; |
| 203 } |
| 204 do_catchsql_test 3.2.5 { |
| 205 COMMIT; |
| 206 } {1 {FOREIGN KEY constraint failed}} |
| 207 do_execsql_test 3.2.6 { ROLLBACK } |
| 208 |
| 209 do_execsql_test 3.3.1 { |
| 210 CREATE TRIGGER p2t AFTER DELETE ON p2 BEGIN |
| 211 INSERT INTO p2 VALUES(old.a, 'deleted!'); |
| 212 END; |
| 213 } |
| 214 do_catchsql_test 3.3.2 { |
| 215 BEGIN; |
| 216 DELETE FROM p2 WHERE a=1; |
| 217 } {1 {FOREIGN KEY constraint failed}} |
| 218 do_execsql_test 3.3.3 { COMMIT } |
| 219 |
| 220 do_execsql_test 3.3.4 { |
| 221 BEGIN; |
| 222 PRAGMA defer_foreign_keys = 1; |
| 223 DELETE FROM p2 WHERE a=1; |
| 224 COMMIT; |
| 225 SELECT * FROM p2; |
| 226 } {0 one 1 deleted!} |
| 227 |
174 | 228 |
175 finish_test | 229 finish_test |
OLD | NEW |