drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 1 | # 2018-04-12 |
| 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 | # Test cases for UPSERT |
| 13 | |
| 14 | set testdir [file dirname $argv0] |
| 15 | source $testdir/tester.tcl |
| 16 | set testprefix zipfile |
| 17 | |
| 18 | do_execsql_test upsert1-100 { |
drh | e9c2e77 | 2018-04-13 13:06:45 +0000 | [diff] [blame] | 19 | CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c DEFAULT 0); |
| 20 | CREATE UNIQUE INDEX t1x1 ON t1(b); |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 21 | INSERT INTO t1(a,b) VALUES(1,2) ON CONFLICT DO NOTHING; |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 22 | INSERT INTO t1(a,b) VALUES(1,99),(99,2) ON CONFLICT DO NOTHING; |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 23 | SELECT * FROM t1; |
drh | e9c2e77 | 2018-04-13 13:06:45 +0000 | [diff] [blame] | 24 | } {1 2 0} |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 25 | do_execsql_test upsert1-101 { |
| 26 | DELETE FROM t1; |
| 27 | INSERT INTO t1(a,b) VALUES(2,3) ON CONFLICT(a) DO NOTHING; |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 28 | INSERT INTO t1(a,b) VALUES(2,99) ON CONFLICT(a) DO NOTHING; |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 29 | SELECT * FROM t1; |
drh | e9c2e77 | 2018-04-13 13:06:45 +0000 | [diff] [blame] | 30 | } {2 3 0} |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 31 | do_execsql_test upsert1-102 { |
| 32 | DELETE FROM t1; |
drh | e9c2e77 | 2018-04-13 13:06:45 +0000 | [diff] [blame] | 33 | INSERT INTO t1(a,b) VALUES(3,4) ON CONFLICT(b) DO NOTHING; |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 34 | INSERT INTO t1(a,b) VALUES(99,4) ON CONFLICT(b) DO NOTHING; |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 35 | SELECT * FROM t1; |
drh | e9c2e77 | 2018-04-13 13:06:45 +0000 | [diff] [blame] | 36 | } {3 4 0} |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 37 | do_catchsql_test upsert1-110 { |
drh | e9c2e77 | 2018-04-13 13:06:45 +0000 | [diff] [blame] | 38 | INSERT INTO t1(a,b) VALUES(5,6) ON CONFLICT(x) DO NOTHING; |
| 39 | SELECT * FROM t1; |
| 40 | } {1 {no such column: x}} |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 41 | do_catchsql_test upsert1-120 { |
drh | e9c2e77 | 2018-04-13 13:06:45 +0000 | [diff] [blame] | 42 | INSERT INTO t1(a,b) VALUES(5,6) ON CONFLICT(c) DO NOTHING; |
| 43 | SELECT * FROM t1; |
| 44 | } {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} |
drh | 3b45d8b | 2018-04-13 13:44:48 +0000 | [diff] [blame] | 45 | breakpoint |
drh | e9c2e77 | 2018-04-13 13:06:45 +0000 | [diff] [blame] | 46 | do_catchsql_test upsert1-130 { |
| 47 | INSERT INTO t1(a,b) VALUES(5,6) ON CONFLICT(b COLLATE nocase) DO NOTHING; |
| 48 | SELECT * FROM t1; |
| 49 | } {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} |
drh | d5af542 | 2018-04-13 14:27:01 +0000 | [diff] [blame] | 50 | do_execsql_test upsert1-140 { |
| 51 | DELETE FROM t1; |
| 52 | INSERT INTO t1(a,b) VALUES(5,6) ON CONFLICT(b COLLATE binary) DO NOTHING; |
| 53 | SELECT * FROM t1; |
| 54 | } {5 6 0} |
drh | e9c2e77 | 2018-04-13 13:06:45 +0000 | [diff] [blame] | 55 | |
drh | e9c2e77 | 2018-04-13 13:06:45 +0000 | [diff] [blame] | 56 | do_catchsql_test upsert1-200 { |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 57 | DROP TABLE t1; |
| 58 | CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c DEFAULT 0); |
| 59 | CREATE UNIQUE INDEX t1x1 ON t1(a+b); |
| 60 | INSERT INTO t1(a,b) VALUES(7,8) ON CONFLICT(a+b) DO NOTHING; |
| 61 | INSERT INTO t1(a,b) VALUES(8,7),(9,6) ON CONFLICT(a+b) DO NOTHING; |
drh | e9c2e77 | 2018-04-13 13:06:45 +0000 | [diff] [blame] | 62 | SELECT * FROM t1; |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 63 | } {0 {7 8 0}} |
| 64 | do_catchsql_test upsert1-201 { |
| 65 | INSERT INTO t1(a,b) VALUES(8,7),(9,6) ON CONFLICT(a) DO NOTHING; |
| 66 | } {1 {UNIQUE constraint failed: index 't1x1'}} |
drh | 3b45d8b | 2018-04-13 13:44:48 +0000 | [diff] [blame] | 67 | do_catchsql_test upsert1-210 { |
| 68 | DELETE FROM t1; |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 69 | INSERT INTO t1(a,b) VALUES(9,10) ON CONFLICT(a+(+b)) DO NOTHING; |
drh | 3b45d8b | 2018-04-13 13:44:48 +0000 | [diff] [blame] | 70 | SELECT * FROM t1; |
| 71 | } {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} |
| 72 | |
drh | d5af542 | 2018-04-13 14:27:01 +0000 | [diff] [blame] | 73 | do_catchsql_test upsert1-300 { |
| 74 | DROP INDEX t1x1; |
| 75 | DELETE FROM t1; |
| 76 | CREATE UNIQUE INDEX t1x1 ON t1(b) WHERE b>10; |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 77 | INSERT INTO t1(a,b) VALUES(1,2),(3,2) ON CONFLICT(b) DO NOTHING; |
drh | d5af542 | 2018-04-13 14:27:01 +0000 | [diff] [blame] | 78 | SELECT * FROM t1; |
| 79 | } {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} |
| 80 | do_catchsql_test upsert1-310 { |
| 81 | DELETE FROM t1; |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 82 | INSERT INTO t1(a,b) VALUES(1,2),(3,2) ON CONFLICT(b) WHERE b!=10 DO NOTHING; |
drh | d5af542 | 2018-04-13 14:27:01 +0000 | [diff] [blame] | 83 | SELECT * FROM t1; |
| 84 | } {1 {ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint}} |
| 85 | do_execsql_test upsert1-320 { |
| 86 | DELETE FROM t1; |
drh | c8a0c90 | 2018-04-13 15:14:33 +0000 | [diff] [blame] | 87 | INSERT INTO t1(a,b) VALUES(1,2),(3,2),(4,20),(5,20) |
| 88 | ON CONFLICT(b) WHERE b>10 DO NOTHING; |
| 89 | SELECT *, 'x' FROM t1 ORDER BY b, a; |
| 90 | } {1 2 0 x 3 2 0 x 4 20 0 x} |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 91 | |
drh | 7963691 | 2018-04-19 23:52:39 +0000 | [diff] [blame] | 92 | # Upsert works with count_changes=on; |
| 93 | do_execsql_test upsert1-400 { |
| 94 | DROP TABLE IF EXISTS t2; |
| 95 | CREATE TABLE t2(a TEXT UNIQUE, b INT DEFAULT 1); |
| 96 | INSERT INTO t2(a) VALUES('one'),('two'),('three'); |
| 97 | PRAGMA count_changes=ON; |
| 98 | INSERT INTO t2(a) VALUES('one'),('one'),('three'),('four') |
| 99 | ON CONFLICT(a) DO UPDATE SET b=b+1; |
| 100 | } {1} |
| 101 | do_execsql_test upsert1-410 { |
| 102 | PRAGMA count_changes=OFF; |
| 103 | SELECT a, b FROM t2 ORDER BY a; |
| 104 | } {four 1 one 3 three 2 two 1} |
| 105 | |
drh | f49ff6f | 2018-04-23 20:38:40 +0000 | [diff] [blame] | 106 | # Problem found by AFL prior to any release |
| 107 | do_execsql_test upsert1-500 { |
| 108 | DROP TABLE t1; |
| 109 | CREATE TABLE t1(x INTEGER PRIMARY KEY, y INT UNIQUE); |
| 110 | INSERT INTO t1(x,y) SELECT 1,2 WHERE true |
| 111 | ON CONFLICT(x) DO UPDATE SET y=max(t1.y,excluded.y) AND true; |
| 112 | SELECT * FROM t1; |
| 113 | } {1 2} |
drh | 7963691 | 2018-04-19 23:52:39 +0000 | [diff] [blame] | 114 | |
drh | 222a384 | 2018-07-11 13:34:24 +0000 | [diff] [blame] | 115 | # 2018-07-11 |
| 116 | # Ticket https://sqlite.org/src/tktview/79cad5e4b2e219dd197242e9e5f4 |
| 117 | # UPSERT leads to a corrupt index. |
| 118 | # |
| 119 | do_execsql_test upsert1-600 { |
| 120 | DROP TABLE t1; |
| 121 | CREATE TABLE t1(b UNIQUE, a INT PRIMARY KEY) WITHOUT ROWID; |
| 122 | INSERT OR IGNORE INTO t1(a) VALUES('1') ON CONFLICT(a) DO NOTHING; |
| 123 | PRAGMA integrity_check; |
| 124 | } {ok} |
| 125 | do_execsql_test upsert1-610 { |
| 126 | DELETE FROM t1; |
| 127 | INSERT OR IGNORE INTO t1(a) VALUES('1'),(1) ON CONFLICT(a) DO NOTHING; |
| 128 | PRAGMA integrity_check; |
| 129 | } {ok} |
| 130 | |
drh | 788d55a | 2018-04-13 01:15:09 +0000 | [diff] [blame] | 131 | finish_test |