Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/conflict3.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / conflict3.test` 512 lines ยท 13925 bytes Raw Blame History 1 # 2013-11-05 2 # 3 # The author disclaims copyright to this source code. In place of 4 # a legal notice, here is a blessing: 5 # 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. 9 # 10 #*********************************************************************** 11 # 12 # This file implements tests for the conflict resolution extension 13 # to SQLite. 14 # 15 # This file focuses on making sure that combinations of REPLACE, 16 # IGNORE, and FAIL conflict resolution play well together. 17 # 18 19 set testdir [file dirname $argv0] 20 source $testdir/tester.tcl 21 set testprefix conflict3 22 23 ifcapable !conflict { 24 finish_test 25 return 26 } 27 28 do_execsql_test 1.1 { 29 CREATE TABLE t1( 30 a INTEGER PRIMARY KEY ON CONFLICT REPLACE, 31 b UNIQUE ON CONFLICT IGNORE, 32 c UNIQUE ON CONFLICT FAIL 33 ); 34 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4); 35 SELECT a,b,c FROM t1 ORDER BY a; 36 } {1 2 3 2 3 4} 37 38 # Insert a row that conflicts on column B. The insert should be ignored. 39 # 40 do_execsql_test 1.2 { 41 INSERT INTO t1(a,b,c) VALUES(3,2,5); 42 SELECT a,b,c FROM t1 ORDER BY a; 43 } {1 2 3 2 3 4} 44 45 # Insert two rows where the second conflicts on C. The first row should 46 # be inserted then there should be a constraint error. 47 # 48 do_catchsql_test 1.3 { 49 INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4); 50 } {1 {UNIQUE constraint failed: t1.c}} 51 do_execsql_test 1.4 { 52 SELECT a,b,c FROM t1 ORDER BY a; 53 } {1 2 3 2 3 4 4 5 6} 54 55 # Table t2 is the same as t1 and the tests are the same except 56 # that t2 has ON CONFLICT ABORT for column c which is overridden 57 # to IGNORE using an ON CONFLICT clause in the INSERT. 58 do_execsql_test 1.4 { 59 CREATE TABLE t2( 60 a INTEGER PRIMARY KEY ON CONFLICT REPLACE, 61 b UNIQUE ON CONFLICT IGNORE, 62 c UNIQUE ON CONFLICT ABORT 63 ); 64 INSERT INTO t2(a,b,c) VALUES(1,2,3), (2,3,4); 65 SELECT a,b,c FROM t2 ORDER BY a; 66 } {1 2 3 2 3 4} 67 do_execsql_test 1.5 { 68 INSERT INTO t2(a,b,c) VALUES(4,5,6), (5,6,4) 69 ON CONFLICT(c) DO NOTHING; 70 } {} 71 do_execsql_test 1.6 { 72 SELECT a,b,c FROM t2 ORDER BY a; 73 } {1 2 3 2 3 4 4 5 6} 74 do_execsql_test 1.7 { 75 INSERT INTO t2(a,b,c) VALUES(9,3,8),(10,11,12) 76 ON CONFLICT(c) DO NOTHING; 77 } {} 78 do_execsql_test 1.8 { 79 SELECT a,b,c FROM t2 ORDER BY a; 80 } {1 2 3 2 3 4 4 5 6 10 11 12} 81 do_execsql_test 1.99 {PRAGMA integrity_check} ok 82 83 84 # Repeat the tests above, but this time on a table non-INTEGER primary key. 85 # 86 do_execsql_test 2.1 { 87 DROP TABLE t1; 88 CREATE TABLE t1( 89 a INT PRIMARY KEY ON CONFLICT REPLACE, 90 b UNIQUE ON CONFLICT IGNORE, 91 c UNIQUE ON CONFLICT FAIL 92 ); 93 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4); 94 SELECT a,b,c FROM t1 ORDER BY a; 95 } {1 2 3 2 3 4} 96 97 # Insert a row that conflicts on column B. The insert should be ignored. 98 # 99 do_execsql_test 2.2 { 100 INSERT INTO t1(a,b,c) VALUES(3,2,5); 101 SELECT a,b,c FROM t1 ORDER BY a; 102 } {1 2 3 2 3 4} 103 104 # Insert two rows where the second conflicts on C. The first row show go 105 # and and then there should be a constraint error. 106 # 107 do_test 2.3 { 108 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);} 109 } {1 {UNIQUE constraint failed: t1.c}} 110 do_execsql_test 2.4 { 111 SELECT a,b,c FROM t1 ORDER BY a; 112 } {1 2 3 2 3 4 4 5 6} 113 114 # Table t2 is the same as t1 and the tests are the same except 115 # that t2 has ON CONFLICT ABORT for column c which is overridden 116 # to IGNORE using an ON CONFLICT clause in the INSERT. 117 do_execsql_test 2.5 { 118 DROP TABLE t2; 119 CREATE TABLE t2( 120 a INT PRIMARY KEY ON CONFLICT REPLACE, 121 b UNIQUE ON CONFLICT FAIL, 122 c UNIQUE ON CONFLICT ABORT 123 ); 124 INSERT INTO t2(a,b,c) VALUES(1,2,3), (2,3,4); 125 SELECT a,b,c FROM t2 ORDER BY a; 126 } {1 2 3 2 3 4} 127 do_execsql_test 2.6 { 128 INSERT INTO t2(a,b,c) VALUES(4,5,6), (5,6,4) 129 ON CONFLICT(c) DO NOTHING; 130 } {} 131 do_execsql_test 2.7 { 132 SELECT a,b,c FROM t2 ORDER BY a; 133 } {1 2 3 2 3 4 4 5 6} 134 do_execsql_test 2.99 {PRAGMA integrity_check} ok 135 136 137 # Repeat again on a WITHOUT ROWID table. 138 # 139 do_execsql_test 3.1 { 140 DROP TABLE t1; 141 CREATE TABLE t1( 142 a INT PRIMARY KEY ON CONFLICT REPLACE, 143 b UNIQUE ON CONFLICT IGNORE, 144 c UNIQUE ON CONFLICT FAIL 145 ) WITHOUT ROWID; 146 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4); 147 SELECT a,b,c FROM t1 ORDER BY a; 148 } {1 2 3 2 3 4} 149 150 # Insert a row that conflicts on column B. The insert should be ignored. 151 # 152 do_execsql_test 3.2 { 153 INSERT INTO t1(a,b,c) VALUES(3,2,5); 154 SELECT a,b,c FROM t1 ORDER BY a; 155 } {1 2 3 2 3 4} 156 157 # Insert two rows where the second conflicts on C. The first row show go 158 # and and then there should be a constraint error. 159 # 160 do_catchsql_test 3.3 { 161 INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4); 162 } {1 {UNIQUE constraint failed: t1.c}} 163 do_execsql_test 3.4 { 164 SELECT a,b,c FROM t1 ORDER BY a; 165 } {1 2 3 2 3 4 4 5 6} 166 167 # Table t2 is the same as t1 and the tests are the same except 168 # that t2 has ON CONFLICT ABORT for column c which is overridden 169 # to IGNORE using an ON CONFLICT clause in the INSERT. 170 do_execsql_test 3.5 { 171 DROP TABLE t2; 172 CREATE TABLE t2( 173 a INT PRIMARY KEY ON CONFLICT REPLACE, 174 b UNIQUE ON CONFLICT FAIL, 175 c UNIQUE ON CONFLICT ABORT 176 ) WITHOUT ROWID; 177 INSERT INTO t2(a,b,c) VALUES(1,2,3), (2,3,4); 178 SELECT a,b,c FROM t2 ORDER BY a; 179 } {1 2 3 2 3 4} 180 do_execsql_test 3.6 { 181 INSERT INTO t2(a,b,c) VALUES(4,5,6), (5,6,4) 182 ON CONFLICT(c) DO NOTHING; 183 } {} 184 do_execsql_test 3.7 { 185 SELECT a,b,c FROM t2 ORDER BY a; 186 } {1 2 3 2 3 4 4 5 6} 187 do_execsql_test 3.99 {PRAGMA integrity_check} ok 188 189 # Arrange the table rows in a different order and repeat. 190 # 191 do_execsql_test 4.1 { 192 DROP TABLE t1; 193 CREATE TABLE t1( 194 b UNIQUE ON CONFLICT IGNORE, 195 c UNIQUE ON CONFLICT FAIL, 196 a INT PRIMARY KEY ON CONFLICT REPLACE 197 ) WITHOUT ROWID; 198 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4); 199 SELECT a,b,c FROM t1 ORDER BY a; 200 } {1 2 3 2 3 4} 201 202 # Insert a row that conflicts on column B. The insert should be ignored. 203 # 204 do_execsql_test 4.2 { 205 INSERT INTO t1(a,b,c) VALUES(3,2,5); 206 SELECT a,b,c FROM t1 ORDER BY a; 207 } {1 2 3 2 3 4} 208 209 # Insert two rows where the second conflicts on C. The first row show go 210 # and and then there should be a constraint error. 211 # 212 do_catchsql_test 4.3 { 213 INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4); 214 } {1 {UNIQUE constraint failed: t1.c}} 215 do_execsql_test 4.4 { 216 SELECT a,b,c FROM t1 ORDER BY a; 217 } {1 2 3 2 3 4 4 5 6} 218 do_execsql_test 4.99 {PRAGMA integrity_check} ok 219 220 # Arrange the table rows in a different order and repeat. 221 # 222 do_execsql_test 5.1 { 223 DROP TABLE t1; 224 CREATE TABLE t1( 225 b UNIQUE ON CONFLICT IGNORE, 226 a INT PRIMARY KEY ON CONFLICT REPLACE, 227 c UNIQUE ON CONFLICT FAIL 228 ); 229 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4); 230 SELECT a,b,c FROM t1 ORDER BY a; 231 } {1 2 3 2 3 4} 232 233 # Insert a row that conflicts on column B. The insert should be ignored. 234 # 235 do_execsql_test 5.2 { 236 INSERT INTO t1(a,b,c) VALUES(3,2,5); 237 SELECT a,b,c FROM t1 ORDER BY a; 238 } {1 2 3 2 3 4} 239 240 # Insert two rows where the second conflicts on C. The first row show go 241 # and and then there should be a constraint error. 242 # 243 do_catchsql_test 5.3 { 244 INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4); 245 } {1 {UNIQUE constraint failed: t1.c}} 246 do_execsql_test 5.4 { 247 SELECT a,b,c FROM t1 ORDER BY a; 248 } {1 2 3 2 3 4 4 5 6} 249 250 # Arrange the table rows in a different order and repeat. 251 # 252 do_execsql_test 6.1 { 253 DROP TABLE t1; 254 CREATE TABLE t1( 255 c UNIQUE ON CONFLICT FAIL, 256 a INT PRIMARY KEY ON CONFLICT REPLACE, 257 b UNIQUE ON CONFLICT IGNORE 258 ); 259 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4); 260 SELECT a,b,c FROM t1 ORDER BY a; 261 } {1 2 3 2 3 4} 262 263 # Insert a row that conflicts on column B. The insert should be ignored. 264 # 265 do_execsql_test 6.2 { 266 INSERT INTO t1(a,b,c) VALUES(3,2,5); 267 SELECT a,b,c FROM t1 ORDER BY a; 268 } {1 2 3 2 3 4} 269 270 # Insert two rows where the second conflicts on C. The first row show go 271 # and and then there should be a constraint error. 272 # 273 do_catchsql_test 6.3 { 274 INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4); 275 } {1 {UNIQUE constraint failed: t1.c}} 276 do_execsql_test 6.4 { 277 SELECT a,b,c FROM t1 ORDER BY a; 278 } {1 2 3 2 3 4 4 5 6} 279 280 # Change which column is the PRIMARY KEY 281 # 282 do_execsql_test 7.1 { 283 DROP TABLE t1; 284 CREATE TABLE t1( 285 a UNIQUE ON CONFLICT REPLACE, 286 b INTEGER PRIMARY KEY ON CONFLICT IGNORE, 287 c UNIQUE ON CONFLICT FAIL 288 ); 289 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4); 290 SELECT a,b,c FROM t1 ORDER BY a; 291 } {1 2 3 2 3 4} 292 293 # Insert a row that conflicts on column B. The insert should be ignored. 294 # 295 do_execsql_test 7.2 { 296 INSERT INTO t1(a,b,c) VALUES(3,2,5); 297 SELECT a,b,c FROM t1 ORDER BY a; 298 } {1 2 3 2 3 4} 299 300 # Insert two rows where the second conflicts on C. The first row show go 301 # and and then there should be a constraint error. 302 # 303 do_catchsql_test 7.3 { 304 INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4); 305 } {1 {UNIQUE constraint failed: t1.c}} 306 do_execsql_test 7.4 { 307 SELECT a,b,c FROM t1 ORDER BY a; 308 } {1 2 3 2 3 4 4 5 6} 309 310 # Change which column is the PRIMARY KEY 311 # 312 do_execsql_test 8.1 { 313 DROP TABLE t1; 314 CREATE TABLE t1( 315 a UNIQUE ON CONFLICT REPLACE, 316 b INT PRIMARY KEY ON CONFLICT IGNORE, 317 c UNIQUE ON CONFLICT FAIL 318 ); 319 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4); 320 SELECT a,b,c FROM t1 ORDER BY a; 321 } {1 2 3 2 3 4} 322 323 # Insert a row that conflicts on column B. The insert should be ignored. 324 # 325 do_execsql_test 8.2 { 326 INSERT INTO t1(a,b,c) VALUES(3,2,5); 327 SELECT a,b,c FROM t1 ORDER BY a; 328 } {1 2 3 2 3 4} 329 330 # Insert two rows where the second conflicts on C. The first row show go 331 # and and then there should be a constraint error. 332 # 333 do_catchsql_test 8.3 { 334 INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4); 335 } {1 {UNIQUE constraint failed: t1.c}} 336 do_execsql_test 8.4 { 337 SELECT a,b,c FROM t1 ORDER BY a; 338 } {1 2 3 2 3 4 4 5 6} 339 340 # Change which column is the PRIMARY KEY 341 # 342 do_execsql_test 9.1 { 343 DROP TABLE t1; 344 CREATE TABLE t1( 345 a UNIQUE ON CONFLICT REPLACE, 346 b INT PRIMARY KEY ON CONFLICT IGNORE, 347 c UNIQUE ON CONFLICT FAIL 348 ) WITHOUT ROWID; 349 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4); 350 SELECT a,b,c FROM t1 ORDER BY a; 351 } {1 2 3 2 3 4} 352 353 # Insert a row that conflicts on column B. The insert should be ignored. 354 # 355 do_execsql_test 9.2 { 356 INSERT INTO t1(a,b,c) VALUES(3,2,5); 357 SELECT a,b,c FROM t1 ORDER BY a; 358 } {1 2 3 2 3 4} 359 360 # Insert two rows where the second conflicts on C. The first row show go 361 # and and then there should be a constraint error. 362 # 363 do_catchsql_test 9.3 { 364 INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4); 365 } {1 {UNIQUE constraint failed: t1.c}} 366 do_execsql_test 9.4 { 367 SELECT a,b,c FROM t1 ORDER BY a; 368 } {1 2 3 2 3 4 4 5 6} 369 370 # Change which column is the PRIMARY KEY 371 # 372 do_execsql_test 10.1 { 373 DROP TABLE t1; 374 CREATE TABLE t1( 375 a UNIQUE ON CONFLICT REPLACE, 376 b UNIQUE ON CONFLICT IGNORE, 377 c INTEGER PRIMARY KEY ON CONFLICT FAIL 378 ); 379 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4); 380 SELECT a,b,c FROM t1 ORDER BY a; 381 } {1 2 3 2 3 4} 382 383 # Insert a row that conflicts on column B. The insert should be ignored. 384 # 385 do_execsql_test 10.2 { 386 INSERT INTO t1(a,b,c) VALUES(3,2,5); 387 SELECT a,b,c FROM t1 ORDER BY a; 388 } {1 2 3 2 3 4} 389 390 # Insert two rows where the second conflicts on C. The first row show go 391 # and and then there should be a constraint error. 392 # 393 do_catchsql_test 10.3 { 394 INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4); 395 } {1 {UNIQUE constraint failed: t1.c}} 396 do_execsql_test 10.4 { 397 SELECT a,b,c FROM t1 ORDER BY a; 398 } {1 2 3 2 3 4 4 5 6} 399 400 # Change which column is the PRIMARY KEY 401 # 402 do_execsql_test 11.1 { 403 DROP TABLE t1; 404 CREATE TABLE t1( 405 a UNIQUE ON CONFLICT REPLACE, 406 b UNIQUE ON CONFLICT IGNORE, 407 c PRIMARY KEY ON CONFLICT FAIL 408 ) WITHOUT ROWID; 409 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4); 410 SELECT a,b,c FROM t1 ORDER BY a; 411 } {1 2 3 2 3 4} 412 413 # Insert a row that conflicts on column B. The insert should be ignored. 414 # 415 do_execsql_test 11.2 { 416 INSERT INTO t1(a,b,c) VALUES(3,2,5); 417 SELECT a,b,c FROM t1 ORDER BY a; 418 } {1 2 3 2 3 4} 419 420 # Insert two rows where the second conflicts on C. The first row show go 421 # and and then there should be a constraint error. 422 # 423 do_catchsql_test 11.3 { 424 INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4); 425 } {1 {UNIQUE constraint failed: t1.c}} 426 do_execsql_test 11.4 { 427 SELECT a,b,c FROM t1 ORDER BY a; 428 } {1 2 3 2 3 4 4 5 6} 429 430 # Check that ticket [`f68dc596c4`] has been fixed. 431 # 432 do_execsql_test 12.1 { 433 DROP TABLE t2; 434 CREATE TABLE t2(a INTEGER PRIMARY KEY, b TEXT); 435 INSERT INTO t2 VALUES(111, '111'); 436 } 437 do_execsql_test 12.2 { 438 REPLACE INTO t2 VALUES(NULL, '112'), (111, '111B'); 439 } 440 do_execsql_test 12.3 { 441 SELECT * FROM t2; 442 } {111 111B 112 112} 443 444 #------------------------------------------------------------------------- 445 ifcapable trigger { 446 reset_db 447 do_execsql_test 13.1.0 { 448 PRAGMA recursive_triggers = true; 449 CREATE TABLE t0 (c0 UNIQUE, c1 UNIQUE); 450 CREATE TRIGGER tr0 AFTER DELETE ON t0 BEGIN 451 DELETE FROM t0; 452 END; 453 454 INSERT INTO t0 VALUES(1, NULL); 455 INSERT INTO t0 VALUES(0, NULL); 456 } 457 458 do_catchsql_test 13.1.1 { 459 UPDATE OR REPLACE t0 SET c1 = 1; 460 } {1 {constraint failed}} 461 462 integrity_check 13.1.2 463 464 do_execsql_test 13.1.3 { 465 SELECT * FROM t0 466 } {1 {} 0 {}} 467 468 do_execsql_test 13.2.0 { 469 CREATE TABLE t2 (a PRIMARY KEY, b UNIQUE, c UNIQUE) WITHOUT ROWID; 470 CREATE TRIGGER tr3 AFTER DELETE ON t2 BEGIN 471 DELETE FROM t2; 472 END; 473 474 INSERT INTO t2 VALUES(1, 1, 1); 475 INSERT INTO t2 VALUES(2, 2, 2); 476 } 477 478 do_catchsql_test 13.2.1 { 479 UPDATE OR REPLACE t2 SET c = 0; 480 } {1 {constraint failed}} 481 482 integrity_check 13.2.2 483 484 do_execsql_test 13.2.3 { 485 SELECT * FROM t2 486 } {1 1 1 2 2 2} 487 488 do_execsql_test 13.3.0 { 489 CREATE TABLE t1(a, b); 490 CREATE TABLE log(x); 491 CREATE INDEX i1 ON t1(a); 492 INSERT INTO t1 VALUES(1, 2); 493 494 CREATE TRIGGER tb BEFORE UPDATE ON t1 BEGIN 495 DELETE FROM t1; 496 END; 497 CREATE TRIGGER ta AFTER UPDATE ON t1 BEGIN 498 INSERT INTO log VALUES('fired!'); 499 END; 500 501 UPDATE t1 SET b=3; 502 } 503 504 do_execsql_test 13.3.1 { 505 SELECT * FROM t1; 506 } {} 507 do_execsql_test 13.3.2 { 508 SELECT * FROM log; 509 } {} 510 } 511 512 finish_test