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 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests for the conflict resolution extension
16 # This file focuses on making sure that combinations of REPLACE,
17 # IGNORE, and FAIL conflict resolution play well together.
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
22 set testprefix conflict3
31 a INTEGER PRIMARY KEY ON CONFLICT REPLACE,
32 b UNIQUE ON CONFLICT IGNORE,
33 c UNIQUE ON CONFLICT FAIL
35 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
36 SELECT a,b,c FROM t1 ORDER BY a;
39 # Insert a row that conflicts on column B. The insert should be ignored.
42 INSERT INTO t1(a,b,c) VALUES(3,2,5);
43 SELECT a,b,c FROM t1 ORDER BY a;
46 # Insert two rows where the second conflicts on C. The first row show go
47 # and and then there should be a constraint error.
50 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
51 } {1 {UNIQUE constraint failed: t1.c}}
53 SELECT a,b,c FROM t1 ORDER BY a;
56 # Replete the tests above, but this time on a table non-INTEGER primary key.
61 a INT PRIMARY KEY ON CONFLICT REPLACE,
62 b UNIQUE ON CONFLICT IGNORE,
63 c UNIQUE ON CONFLICT FAIL
65 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
66 SELECT a,b,c FROM t1 ORDER BY a;
69 # Insert a row that conflicts on column B. The insert should be ignored.
72 INSERT INTO t1(a,b,c) VALUES(3,2,5);
73 SELECT a,b,c FROM t1 ORDER BY a;
76 # Insert two rows where the second conflicts on C. The first row show go
77 # and and then there should be a constraint error.
80 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
81 } {1 {UNIQUE constraint failed: t1.c}}
83 SELECT a,b,c FROM t1 ORDER BY a;
86 # Replete again on a WITHOUT ROWID table.
91 a INT PRIMARY KEY ON CONFLICT REPLACE,
92 b UNIQUE ON CONFLICT IGNORE,
93 c UNIQUE ON CONFLICT FAIL
95 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
96 SELECT a,b,c FROM t1 ORDER BY a;
99 # Insert a row that conflicts on column B. The insert should be ignored.
101 do_execsql_test 3.2 {
102 INSERT INTO t1(a,b,c) VALUES(3,2,5);
103 SELECT a,b,c FROM t1 ORDER BY a;
106 # Insert two rows where the second conflicts on C. The first row show go
107 # and and then there should be a constraint error.
110 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
111 } {1 {UNIQUE constraint failed: t1.c}}
112 do_execsql_test 3.4 {
113 SELECT a,b,c FROM t1 ORDER BY a;
114 } {1 2 3 2 3 4 4 5 6}
116 # Arrange the table rows in a different order and repeat.
118 do_execsql_test 4.1 {
121 b UNIQUE ON CONFLICT IGNORE,
122 c UNIQUE ON CONFLICT FAIL,
123 a INT PRIMARY KEY ON CONFLICT REPLACE
125 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
126 SELECT a,b,c FROM t1 ORDER BY a;
129 # Insert a row that conflicts on column B. The insert should be ignored.
131 do_execsql_test 4.2 {
132 INSERT INTO t1(a,b,c) VALUES(3,2,5);
133 SELECT a,b,c FROM t1 ORDER BY a;
136 # Insert two rows where the second conflicts on C. The first row show go
137 # and and then there should be a constraint error.
140 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
141 } {1 {UNIQUE constraint failed: t1.c}}
142 do_execsql_test 4.4 {
143 SELECT a,b,c FROM t1 ORDER BY a;
144 } {1 2 3 2 3 4 4 5 6}
146 # Arrange the table rows in a different order and repeat.
148 do_execsql_test 5.1 {
151 b UNIQUE ON CONFLICT IGNORE,
152 a INT PRIMARY KEY ON CONFLICT REPLACE,
153 c UNIQUE ON CONFLICT FAIL
155 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
156 SELECT a,b,c FROM t1 ORDER BY a;
159 # Insert a row that conflicts on column B. The insert should be ignored.
161 do_execsql_test 5.2 {
162 INSERT INTO t1(a,b,c) VALUES(3,2,5);
163 SELECT a,b,c FROM t1 ORDER BY a;
166 # Insert two rows where the second conflicts on C. The first row show go
167 # and and then there should be a constraint error.
170 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
171 } {1 {UNIQUE constraint failed: t1.c}}
172 do_execsql_test 5.4 {
173 SELECT a,b,c FROM t1 ORDER BY a;
174 } {1 2 3 2 3 4 4 5 6}
176 # Arrange the table rows in a different order and repeat.
178 do_execsql_test 6.1 {
181 c UNIQUE ON CONFLICT FAIL,
182 a INT PRIMARY KEY ON CONFLICT REPLACE,
183 b UNIQUE ON CONFLICT IGNORE
185 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
186 SELECT a,b,c FROM t1 ORDER BY a;
189 # Insert a row that conflicts on column B. The insert should be ignored.
191 do_execsql_test 6.2 {
192 INSERT INTO t1(a,b,c) VALUES(3,2,5);
193 SELECT a,b,c FROM t1 ORDER BY a;
196 # Insert two rows where the second conflicts on C. The first row show go
197 # and and then there should be a constraint error.
200 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
201 } {1 {UNIQUE constraint failed: t1.c}}
202 do_execsql_test 6.4 {
203 SELECT a,b,c FROM t1 ORDER BY a;
204 } {1 2 3 2 3 4 4 5 6}
206 # Change which column is the PRIMARY KEY
208 do_execsql_test 7.1 {
211 a UNIQUE ON CONFLICT REPLACE,
212 b INTEGER PRIMARY KEY ON CONFLICT IGNORE,
213 c UNIQUE ON CONFLICT FAIL
215 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
216 SELECT a,b,c FROM t1 ORDER BY a;
219 # Insert a row that conflicts on column B. The insert should be ignored.
221 do_execsql_test 7.2 {
222 INSERT INTO t1(a,b,c) VALUES(3,2,5);
223 SELECT a,b,c FROM t1 ORDER BY a;
226 # Insert two rows where the second conflicts on C. The first row show go
227 # and and then there should be a constraint error.
230 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
231 } {1 {UNIQUE constraint failed: t1.c}}
232 do_execsql_test 7.4 {
233 SELECT a,b,c FROM t1 ORDER BY a;
234 } {1 2 3 2 3 4 4 5 6}
236 # Change which column is the PRIMARY KEY
238 do_execsql_test 8.1 {
241 a UNIQUE ON CONFLICT REPLACE,
242 b INT PRIMARY KEY ON CONFLICT IGNORE,
243 c UNIQUE ON CONFLICT FAIL
245 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
246 SELECT a,b,c FROM t1 ORDER BY a;
249 # Insert a row that conflicts on column B. The insert should be ignored.
251 do_execsql_test 8.2 {
252 INSERT INTO t1(a,b,c) VALUES(3,2,5);
253 SELECT a,b,c FROM t1 ORDER BY a;
256 # Insert two rows where the second conflicts on C. The first row show go
257 # and and then there should be a constraint error.
260 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
261 } {1 {UNIQUE constraint failed: t1.c}}
262 do_execsql_test 8.4 {
263 SELECT a,b,c FROM t1 ORDER BY a;
264 } {1 2 3 2 3 4 4 5 6}
266 # Change which column is the PRIMARY KEY
268 do_execsql_test 9.1 {
271 a UNIQUE ON CONFLICT REPLACE,
272 b INT PRIMARY KEY ON CONFLICT IGNORE,
273 c UNIQUE ON CONFLICT FAIL
275 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
276 SELECT a,b,c FROM t1 ORDER BY a;
279 # Insert a row that conflicts on column B. The insert should be ignored.
281 do_execsql_test 9.2 {
282 INSERT INTO t1(a,b,c) VALUES(3,2,5);
283 SELECT a,b,c FROM t1 ORDER BY a;
286 # Insert two rows where the second conflicts on C. The first row show go
287 # and and then there should be a constraint error.
290 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
291 } {1 {UNIQUE constraint failed: t1.c}}
292 do_execsql_test 9.4 {
293 SELECT a,b,c FROM t1 ORDER BY a;
294 } {1 2 3 2 3 4 4 5 6}
296 # Change which column is the PRIMARY KEY
298 do_execsql_test 10.1 {
301 a UNIQUE ON CONFLICT REPLACE,
302 b UNIQUE ON CONFLICT IGNORE,
303 c INTEGER PRIMARY KEY ON CONFLICT FAIL
305 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
306 SELECT a,b,c FROM t1 ORDER BY a;
309 # Insert a row that conflicts on column B. The insert should be ignored.
311 do_execsql_test 10.2 {
312 INSERT INTO t1(a,b,c) VALUES(3,2,5);
313 SELECT a,b,c FROM t1 ORDER BY a;
316 # Insert two rows where the second conflicts on C. The first row show go
317 # and and then there should be a constraint error.
320 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
321 } {1 {UNIQUE constraint failed: t1.c}}
322 do_execsql_test 10.4 {
323 SELECT a,b,c FROM t1 ORDER BY a;
324 } {1 2 3 2 3 4 4 5 6}
326 # Change which column is the PRIMARY KEY
328 do_execsql_test 11.1 {
331 a UNIQUE ON CONFLICT REPLACE,
332 b UNIQUE ON CONFLICT IGNORE,
333 c PRIMARY KEY ON CONFLICT FAIL
335 INSERT INTO t1(a,b,c) VALUES(1,2,3), (2,3,4);
336 SELECT a,b,c FROM t1 ORDER BY a;
339 # Insert a row that conflicts on column B. The insert should be ignored.
341 do_execsql_test 11.2 {
342 INSERT INTO t1(a,b,c) VALUES(3,2,5);
343 SELECT a,b,c FROM t1 ORDER BY a;
346 # Insert two rows where the second conflicts on C. The first row show go
347 # and and then there should be a constraint error.
350 catchsql {INSERT INTO t1(a,b,c) VALUES(4,5,6), (5,6,4);}
351 } {1 {UNIQUE constraint failed: t1.c}}
352 do_execsql_test 11.4 {
353 SELECT a,b,c FROM t1 ORDER BY a;
354 } {1 2 3 2 3 4 4 5 6}
356 # Check that ticket [f68dc596c4] has been fixed.
358 do_execsql_test 12.1 {
359 CREATE TABLE t2(a INTEGER PRIMARY KEY, b TEXT);
360 INSERT INTO t2 VALUES(111, '111');
362 do_execsql_test 12.2 {
363 REPLACE INTO t2 VALUES(NULL, '112'), (111, '111B');
365 do_execsql_test 12.3 {