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 #*************************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 set testprefix alterlegacy
17 # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
18 ifcapable !altertable {
24 PRAGMA legacy_alter_table = 1;
25 CREATE TABLE t1(a, b, CHECK(t1.a != t1.b));
26 CREATE TABLE t2(a, b);
27 CREATE INDEX t2expr ON t2(a) WHERE t2.b>0;
31 SELECT sql FROM sqlite_master
33 {CREATE TABLE t1(a, b, CHECK(t1.a != t1.b))}
34 {CREATE TABLE t2(a, b)}
35 {CREATE INDEX t2expr ON t2(a) WHERE t2.b>0}
38 # Legacy behavior is to corrupt the schema in this case, as the table name in
39 # the CHECK constraint is incorrect after "t1" is renamed. This version is
40 # slightly different - it rejects the change and rolls back the transaction.
41 do_catchsql_test 1.2 {
42 ALTER TABLE t1 RENAME TO t1new;
43 } {1 {error in table t1new after rename: no such column: t1.a}}
46 CREATE TABLE t3(c, d);
47 ALTER TABLE t3 RENAME TO t3new;
52 SELECT sql FROM sqlite_master
54 {CREATE TABLE t1(a, b, CHECK(t1.a != t1.b))}
55 {CREATE TABLE t2(a, b)}
56 {CREATE INDEX t2expr ON t2(a) WHERE t2.b>0}
60 do_catchsql_test 1.3 {
61 ALTER TABLE t2 RENAME TO t2new;
62 } {1 {error in index t2expr after rename: no such column: t2.b}}
64 SELECT sql FROM sqlite_master
66 {CREATE TABLE t1(a, b, CHECK(t1.a != t1.b))}
67 {CREATE TABLE t2(a, b)}
68 {CREATE INDEX t2expr ON t2(a) WHERE t2.b>0}
72 #-------------------------------------------------------------------------
75 register_echo_module db
78 PRAGMA legacy_alter_table = 1;
79 CREATE TABLE abc(a, b, c);
80 INSERT INTO abc VALUES(1, 2, 3);
81 CREATE VIRTUAL TABLE eee USING echo('abc');
86 ALTER TABLE eee RENAME TO fff;
93 do_catchsql_test 2.2 {
94 ALTER TABLE fff RENAME TO ggg;
95 } {1 {no such module: echo}}
98 #-------------------------------------------------------------------------
101 do_execsql_test 3.0 {
102 PRAGMA legacy_alter_table = 1;
103 CREATE TABLE txx(a, b, c);
104 INSERT INTO txx VALUES(1, 2, 3);
105 CREATE VIEW vvv AS SELECT main.txx.a, txx.b, c FROM txx;
106 CREATE VIEW uuu AS SELECT main.one.a, one.b, c FROM txx AS one;
107 CREATE VIEW temp.ttt AS SELECT main.txx.a, txx.b, one.b, main.one.a FROM txx AS one, txx;
110 do_execsql_test 3.1.1 {
113 do_execsql_test 3.1.2a {
114 ALTER TABLE txx RENAME TO "t xx";
116 do_catchsql_test 3.1.2b {
118 } {1 {no such table: main.txx}}
119 do_execsql_test 3.1.3 {
120 SELECT sql FROM sqlite_master WHERE name='vvv';
121 } {{CREATE VIEW vvv AS SELECT main.txx.a, txx.b, c FROM txx}}
124 do_catchsql_test 3.2.1 {
126 } {1 {no such table: main.txx}}
127 do_execsql_test 3.2.2 {
128 SELECT sql FROM sqlite_master WHERE name='uuu';;
129 } {{CREATE VIEW uuu AS SELECT main.one.a, one.b, c FROM txx AS one}}
131 do_catchsql_test 3.3.1 {
133 } {1 {no such table: txx}}
134 do_execsql_test 3.3.2 {
135 SELECT sql FROM sqlite_temp_master WHERE name='ttt';
136 } {{CREATE VIEW ttt AS SELECT main.txx.a, txx.b, one.b, main.one.a FROM txx AS one, txx}}
138 #-------------------------------------------------------------------------
140 do_execsql_test 4.0 {
141 PRAGMA legacy_alter_table = 1;
142 CREATE table t1(x, y);
143 CREATE table t2(a, b);
145 CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
146 SELECT t1.x, * FROM t1, t2;
147 INSERT INTO t2 VALUES(new.x, new.y);
151 do_execsql_test 4.1 {
152 INSERT INTO t1 VALUES(1, 1);
153 ALTER TABLE t1 RENAME TO t11;
155 do_catchsql_test 4.1a {
156 INSERT INTO t11 VALUES(2, 2);
157 } {1 {no such table: main.t1}}
158 do_execsql_test 4.1b {
159 ALTER TABLE t11 RENAME TO t1;
160 ALTER TABLE t2 RENAME TO t22;
162 do_catchsql_test 4.1c {
163 INSERT INTO t1 VALUES(3, 3);
164 } {1 {no such table: main.t2}}
167 string trim [regsub -all {[[:space:]][[:space:]]*} $a { }]
169 db func squish squish
171 execsql { SELECT squish(sql) FROM sqlite_master WHERE name = 'tr1' }
173 CREATE TRIGGER tr1 AFTER INSERT ON "t1" BEGIN
174 SELECT t1.x, * FROM t1, t2;
175 INSERT INTO t2 VALUES(new.x, new.y);
179 #-------------------------------------------------------------------------
181 do_execsql_test 5.0 {
182 PRAGMA legacy_alter_table = 1;
183 CREATE TABLE t9(a, b, c);
184 CREATE TABLE t10(a, b, c);
185 CREATE TEMP TABLE t9(a, b, c);
187 CREATE TRIGGER temp.t9t AFTER INSERT ON temp.t9 BEGIN
188 INSERT INTO t10 VALUES(new.a, new.b, new.c);
191 INSERT INTO temp.t9 VALUES(1, 2, 3);
195 do_execsql_test 5.1 {
196 ALTER TABLE temp.t9 RENAME TO 't1234567890'
199 do_execsql_test 5.2 {
200 CREATE TABLE t1(a, b);
201 CREATE TABLE t2(a, b);
202 INSERT INTO t1 VALUES(1, 2);
203 INSERT INTO t2 VALUES(3, 4);
204 CREATE VIEW v AS SELECT one.a, one.b, t2.a, t2.b FROM t1 AS one, t2;
208 do_execsql_test 5.3 {
209 ALTER TABLE t2 RENAME TO one;
212 do_catchsql_test 5.4 {
214 } {1 {no such table: main.t2}}
216 do_execsql_test 5.5 {
217 ALTER TABLE one RENAME TO t2;
219 CREATE VIEW temp.vv AS SELECT one.a, one.b, t2.a, t2.b FROM t1 AS one, t2;
223 do_execsql_test 5.6 {
224 ALTER TABLE t2 RENAME TO one;
226 do_catchsql_test 5.7 {
228 } {1 {no such table: t2}}
230 #-------------------------------------------------------------------------
233 register_tcl_module db
234 proc tcl_command {method args} {
237 return "CREATE TABLE t1(a, b, c)"
243 do_execsql_test 6.0 {
244 CREATE VIRTUAL TABLE x1 USING tcl(tcl_command);
247 do_execsql_test 6.1 {
248 ALTER TABLE x1 RENAME TO x2;
249 SELECT sql FROM sqlite_master WHERE name = 'x2'
250 } {{CREATE VIRTUAL TABLE "x2" USING tcl(tcl_command)}}
252 do_execsql_test 7.1 {
253 CREATE TABLE ddd(db, sql, zOld, zNew, bTemp);
254 INSERT INTO ddd VALUES(
255 'main', 'CREATE TABLE x1(i INTEGER, t TEXT)', 'ddd', NULL, 0
257 'main', 'CREATE TABLE x1(i INTEGER, t TEXT)', NULL, 'eee', 0
259 'main', NULL, 'ddd', 'eee', 0
264 #-------------------------------------------------------------------------
268 do_execsql_test 8.1 {
269 PRAGMA legacy_alter_table = 1;
270 ATTACH 'test.db2' AS aux;
271 PRAGMA foreign_keys = on;
272 CREATE TABLE aux.p1(a INTEGER PRIMARY KEY, b);
273 CREATE TABLE aux.c1(x INTEGER PRIMARY KEY, y REFERENCES p1(a));
274 INSERT INTO aux.p1 VALUES(1, 1);
275 INSERT INTO aux.p1 VALUES(2, 2);
276 INSERT INTO aux.c1 VALUES(NULL, 2);
277 CREATE TABLE aux.c2(x INTEGER PRIMARY KEY, y REFERENCES c1(a));
280 do_execsql_test 8.2 {
281 ALTER TABLE aux.p1 RENAME TO ppp;
284 do_execsql_test 8.2 {
285 INSERT INTO aux.c1 VALUES(NULL, 1);
286 SELECT sql FROM aux.sqlite_master WHERE name = 'c1';
287 } {{CREATE TABLE c1(x INTEGER PRIMARY KEY, y REFERENCES "ppp"(a))}}
290 do_execsql_test 9.0 {
291 PRAGMA legacy_alter_table = 1;
292 CREATE TABLE t1(a, b, c);
293 CREATE VIEW v1 AS SELECT * FROM t2;
295 do_execsql_test 9.1 {
296 ALTER TABLE t1 RENAME TO t3;
298 do_execsql_test 9.1b {
299 ALTER TABLE t3 RENAME TO t1;
301 do_execsql_test 9.2 {
303 CREATE TRIGGER tr AFTER INSERT ON t1 BEGIN
304 INSERT INTO t2 VALUES(new.a);
307 do_execsql_test 9.3 {
308 ALTER TABLE t1 RENAME TO t3;
312 do_execsql_test 9.4 {
313 ALTER TABLE t3 RENAME TO t1;
316 ATTACH 'test.db2' AS aux;
317 CREATE TRIGGER tr AFTER INSERT ON t1 WHEN new.a IS NULL BEGIN SELECT 1, 2, 3; END;
319 CREATE TABLE aux.t1(x);
320 CREATE TEMP TRIGGER tr AFTER INSERT ON aux.t1 BEGIN SELECT 1, 2, 3; END;
322 do_execsql_test 9.5 {
323 ALTER TABLE main.t1 RENAME TO t3;
325 do_execsql_test 9.6 {
326 SELECT sql FROM sqlite_temp_master;
327 SELECT sql FROM sqlite_master WHERE type='trigger';
329 {CREATE TRIGGER tr AFTER INSERT ON aux.t1 BEGIN SELECT 1, 2, 3; END}
330 {CREATE TRIGGER tr AFTER INSERT ON "t3" WHEN new.a IS NULL BEGIN SELECT 1, 2, 3; END}
333 #-------------------------------------------------------------------------
336 do_execsql_test 10.0 {
337 PRAGMA legacy_alter_table = 1;
338 CREATE VIRTUAL TABLE fff USING fts5(x, y, z);
341 do_execsql_test 10.1 {
343 INSERT INTO fff VALUES('a', 'b', 'c');
344 ALTER TABLE fff RENAME TO ggg;
348 do_execsql_test 10.2 {
353 #-------------------------------------------------------------------------
356 db func trigger trigger
358 proc trigger {args} {
359 lappend ::trigger $args
361 do_execsql_test 11.0 {
362 PRAGMA legacy_alter_table = 1;
363 ATTACH 'test.db2' AS aux;
364 CREATE TABLE aux.t1(a, b, c);
365 CREATE TABLE main.t1(a, b, c);
366 CREATE TEMP TRIGGER tr AFTER INSERT ON aux.t1 BEGIN
367 SELECT trigger(new.a, new.b, new.c);
371 do_execsql_test 11.1 {
372 INSERT INTO main.t1 VALUES(1, 2, 3);
373 INSERT INTO aux.t1 VALUES(4, 5, 6);
375 do_test 11.2 { set ::trigger } {{4 5 6}}
377 do_execsql_test 11.3 {
378 SELECT name, tbl_name FROM sqlite_temp_master;
381 do_execsql_test 11.4 {
382 ALTER TABLE main.t1 RENAME TO t2;
383 SELECT name, tbl_name FROM sqlite_temp_master;
386 do_execsql_test 11.5 {
387 ALTER TABLE aux.t1 RENAME TO t2;
388 SELECT name, tbl_name FROM sqlite_temp_master;
391 do_execsql_test 11.6 {
392 INSERT INTO aux.t2 VALUES(7, 8, 9);
394 do_test 11.7 { set ::trigger } {{4 5 6} {7 8 9}}
396 #-------------------------------------------------------------------------
398 do_execsql_test 12.0 {
399 PRAGMA legacy_alter_table = 1;
402 CREATE TRIGGER temp.r1 AFTER INSERT ON main.t2 BEGIN
403 INSERT INTO t1(a) VALUES(new.w);
405 CREATE TEMP TABLE t2(x);
408 do_execsql_test 12.1 {
409 ALTER TABLE main.t2 RENAME TO t3;
412 do_execsql_test 12.2 {
413 INSERT INTO t3 VALUES('WWW');
418 #-------------------------------------------------------------------------
422 do_execsql_test 14.0 {
423 PRAGMA legacy_alter_table = 1;
424 CREATE VIRTUAL TABLE rt USING rtree(id, minx, maxx, miny, maxy);
426 CREATE TABLE "mytable" ( "fid" INTEGER PRIMARY KEY, "geom" BLOB);
428 CREATE TRIGGER tr1 AFTER UPDATE OF "geom" ON "mytable"
429 WHEN OLD."fid" = NEW."fid" AND NEW."geom" IS NULL BEGIN
430 DELETE FROM rt WHERE id = OLD."fid";
433 INSERT INTO mytable VALUES(1, X'abcd');
436 do_execsql_test 14.1 {
437 UPDATE mytable SET geom = X'1234'
440 do_execsql_test 14.2 {
441 ALTER TABLE mytable RENAME TO mytable_renamed;
444 do_execsql_test 14.3 {
445 CREATE TRIGGER tr2 AFTER INSERT ON mytable_renamed BEGIN
446 DELETE FROM rt WHERE id=(SELECT min(id) FROM rt);
450 do_execsql_test 14.4 {
451 ALTER TABLE mytable_renamed RENAME TO mytable2;
456 do_execsql_test 14.5 {
457 PRAGMA legacy_alter_table = 1;
458 CREATE TABLE t1(a, b, c);
459 CREATE VIEW v1 AS SELECT * FROM t1;
460 CREATE TRIGGER xyz AFTER INSERT ON t1 BEGIN
464 do_execsql_test 14.6 {
465 ALTER TABLE t1 RENAME TO tt1;