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 altertab
17 # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
18 ifcapable !altertable {
24 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}
39 ALTER TABLE t1 RENAME TO t1new;
43 CREATE TABLE t3(c, d);
44 ALTER TABLE t3 RENAME TO t3new;
49 SELECT sql FROM sqlite_master
51 {CREATE TABLE "t1new"(a, b, CHECK("t1new".a != "t1new".b))}
52 {CREATE TABLE t2(a, b)}
53 {CREATE INDEX t2expr ON t2(a) WHERE t2.b>0}
58 ALTER TABLE t2 RENAME TO t2new;
61 SELECT sql FROM sqlite_master
63 {CREATE TABLE "t1new"(a, b, CHECK("t1new".a != "t1new".b))}
64 {CREATE TABLE "t2new"(a, b)}
65 {CREATE INDEX t2expr ON "t2new"(a) WHERE "t2new".b>0}
69 #-------------------------------------------------------------------------
72 register_echo_module db
75 CREATE TABLE abc(a, b, c);
76 INSERT INTO abc VALUES(1, 2, 3);
77 CREATE VIRTUAL TABLE eee USING echo('abc');
82 ALTER TABLE eee RENAME TO fff;
89 do_catchsql_test 2.2 {
90 ALTER TABLE fff RENAME TO ggg;
91 } {1 {no such module: echo}}
94 #-------------------------------------------------------------------------
98 CREATE TABLE txx(a, b, c);
99 INSERT INTO txx VALUES(1, 2, 3);
100 CREATE VIEW vvv AS SELECT main.txx.a, txx.b, c FROM txx;
101 CREATE VIEW uuu AS SELECT main.one.a, one.b, c FROM txx AS one;
102 CREATE VIEW temp.ttt AS SELECT main.txx.a, txx.b, one.b, main.one.a FROM txx AS one, txx;
105 do_execsql_test 3.1.1 {
108 do_execsql_test 3.1.2 {
109 ALTER TABLE txx RENAME TO "t xx";
112 do_execsql_test 3.1.3 {
113 SELECT sql FROM sqlite_master WHERE name='vvv';
114 } {{CREATE VIEW vvv AS SELECT main."t xx".a, "t xx".b, c FROM "t xx"}}
117 do_execsql_test 3.2.1 {
120 do_execsql_test 3.2.2 {
121 SELECT sql FROM sqlite_master WHERE name='uuu';;
122 } {{CREATE VIEW uuu AS SELECT main.one.a, one.b, c FROM "t xx" AS one}}
124 do_execsql_test 3.3.1 {
127 do_execsql_test 3.3.2 {
128 SELECT sql FROM sqlite_temp_master WHERE name='ttt';
129 } {{CREATE VIEW ttt AS SELECT main."t xx".a, "t xx".b, one.b, main.one.a FROM "t xx" AS one, "t xx"}}
131 #-------------------------------------------------------------------------
133 do_execsql_test 4.0 {
134 CREATE table t1(x, y);
135 CREATE table t2(a, b);
137 CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
138 SELECT t1.x, * FROM t1, t2;
139 INSERT INTO t2 VALUES(new.x, new.y);
143 do_execsql_test 4.1 {
144 INSERT INTO t1 VALUES(1, 1);
145 ALTER TABLE t1 RENAME TO t11;
146 INSERT INTO t11 VALUES(2, 2);
147 ALTER TABLE t2 RENAME TO t22;
148 INSERT INTO t11 VALUES(3, 3);
152 string trim [regsub -all {[[:space:]][[:space:]]*} $a { }]
154 db func squish squish
156 execsql { SELECT squish(sql) FROM sqlite_master WHERE name = 'tr1' }
158 CREATE TRIGGER tr1 AFTER INSERT ON "t11" BEGIN
159 SELECT "t11".x, * FROM "t11", "t22";
160 INSERT INTO "t22" VALUES(new.x, new.y);
164 #-------------------------------------------------------------------------
166 do_execsql_test 5.0 {
167 CREATE TABLE t9(a, b, c);
168 CREATE TABLE t10(a, b, c);
169 CREATE TEMP TABLE t9(a, b, c);
171 CREATE TRIGGER temp.t9t AFTER INSERT ON temp.t9 BEGIN
172 INSERT INTO t10 VALUES(new.a, new.b, new.c);
175 INSERT INTO temp.t9 VALUES(1, 2, 3);
179 do_execsql_test 5.1 {
180 ALTER TABLE temp.t9 RENAME TO 't1234567890'
183 do_execsql_test 5.2 {
184 CREATE TABLE t1(a, b);
185 CREATE TABLE t2(a, b);
186 INSERT INTO t1 VALUES(1, 2);
187 INSERT INTO t2 VALUES(3, 4);
188 CREATE VIEW v AS SELECT one.a, one.b, t2.a, t2.b FROM t1 AS one, t2;
192 do_catchsql_test 5.3 {
193 ALTER TABLE t2 RENAME TO one;
194 } {1 {error in view v after rename: ambiguous column name: one.a}}
196 do_execsql_test 5.4 {
200 do_execsql_test 5.5 {
202 CREATE VIEW temp.vv AS SELECT one.a, one.b, t2.a, t2.b FROM t1 AS one, t2;
206 do_catchsql_test 5.6 {
207 ALTER TABLE t2 RENAME TO one;
208 } {1 {error in view vv after rename: ambiguous column name: one.a}}
210 #-------------------------------------------------------------------------
213 register_tcl_module db
214 proc tcl_command {method args} {
217 return "CREATE TABLE t1(a, b, c)"
223 do_execsql_test 6.0 {
224 CREATE VIRTUAL TABLE x1 USING tcl(tcl_command);
227 do_execsql_test 6.1 {
228 ALTER TABLE x1 RENAME TO x2;
229 SELECT sql FROM sqlite_master WHERE name = 'x2'
230 } {{CREATE VIRTUAL TABLE "x2" USING tcl(tcl_command)}}
232 do_execsql_test 7.1 {
233 CREATE TABLE ddd(db, sql, zOld, zNew, bTemp);
234 INSERT INTO ddd VALUES(
235 'main', 'CREATE TABLE x1(i INTEGER, t TEXT)', 'ddd', NULL, 0
237 'main', 'CREATE TABLE x1(i INTEGER, t TEXT)', NULL, 'eee', 0
239 'main', NULL, 'ddd', 'eee', 0
243 do_execsql_test 7.2 {
245 sqlite_rename_table(db, 0, 0, sql, zOld, zNew, bTemp)
250 #-------------------------------------------------------------------------
254 do_execsql_test 8.1 {
255 ATTACH 'test.db2' AS aux;
256 PRAGMA foreign_keys = on;
257 CREATE TABLE aux.p1(a INTEGER PRIMARY KEY, b);
258 CREATE TABLE aux.c1(x INTEGER PRIMARY KEY, y REFERENCES p1(a));
259 INSERT INTO aux.p1 VALUES(1, 1);
260 INSERT INTO aux.p1 VALUES(2, 2);
261 INSERT INTO aux.c1 VALUES(NULL, 2);
262 CREATE TABLE aux.c2(x INTEGER PRIMARY KEY, y REFERENCES c1(a));
265 do_execsql_test 8.2 {
266 ALTER TABLE aux.p1 RENAME TO ppp;
269 do_execsql_test 8.2 {
270 INSERT INTO aux.c1 VALUES(NULL, 1);
271 SELECT sql FROM aux.sqlite_master WHERE name = 'c1';
272 } {{CREATE TABLE c1(x INTEGER PRIMARY KEY, y REFERENCES "ppp"(a))}}
275 do_execsql_test 9.0 {
276 CREATE TABLE t1(a, b, c);
277 CREATE VIEW v1 AS SELECT * FROM t2;
279 do_catchsql_test 9.1 {
280 ALTER TABLE t1 RENAME TO t3;
281 } {1 {error in view v1: no such table: main.t2}}
282 do_execsql_test 9.2 {
284 CREATE TRIGGER tr AFTER INSERT ON t1 BEGIN
285 INSERT INTO t2 VALUES(new.a);
288 do_catchsql_test 9.3 {
289 ALTER TABLE t1 RENAME TO t3;
290 } {1 {error in trigger tr: no such table: main.t2}}
293 do_execsql_test 9.4 {
296 ATTACH 'test.db2' AS aux;
297 CREATE TRIGGER tr AFTER INSERT ON t1 WHEN new.a IS NULL BEGIN SELECT 1, 2, 3; END;
299 CREATE TABLE aux.t1(x);
300 CREATE TEMP TRIGGER tr AFTER INSERT ON aux.t1 BEGIN SELECT 1, 2, 3; END;
302 do_execsql_test 9.5 {
303 ALTER TABLE main.t1 RENAME TO t3;
305 do_execsql_test 9.6 {
306 SELECT sql FROM sqlite_temp_master;
307 SELECT sql FROM sqlite_master WHERE type='trigger';
309 {CREATE TRIGGER tr AFTER INSERT ON aux.t1 BEGIN SELECT 1, 2, 3; END}
310 {CREATE TRIGGER tr AFTER INSERT ON "t3" WHEN new.a IS NULL BEGIN SELECT 1, 2, 3; END}
313 #-------------------------------------------------------------------------
316 do_execsql_test 10.0 {
317 CREATE VIRTUAL TABLE fff USING fts5(x, y, z);
320 do_execsql_test 10.1 {
322 INSERT INTO fff VALUES('a', 'b', 'c');
323 ALTER TABLE fff RENAME TO ggg;
327 do_execsql_test 10.2 {
332 #-------------------------------------------------------------------------
335 db func trigger trigger
337 proc trigger {args} {
338 lappend ::trigger $args
340 do_execsql_test 11.0 {
341 ATTACH 'test.db2' AS aux;
342 CREATE TABLE aux.t1(a, b, c);
343 CREATE TABLE main.t1(a, b, c);
344 CREATE TEMP TRIGGER tr AFTER INSERT ON aux.t1 BEGIN
345 SELECT trigger(new.a, new.b, new.c);
349 do_execsql_test 11.1 {
350 INSERT INTO main.t1 VALUES(1, 2, 3);
351 INSERT INTO aux.t1 VALUES(4, 5, 6);
353 do_test 11.2 { set ::trigger } {{4 5 6}}
355 do_execsql_test 11.3 {
356 SELECT name, tbl_name FROM sqlite_temp_master;
359 do_execsql_test 11.4 {
360 ALTER TABLE main.t1 RENAME TO t2;
361 SELECT name, tbl_name FROM sqlite_temp_master;
364 do_execsql_test 11.5 {
365 ALTER TABLE aux.t1 RENAME TO t2;
366 SELECT name, tbl_name FROM sqlite_temp_master;
369 do_execsql_test 11.6 {
370 INSERT INTO aux.t2 VALUES(7, 8, 9);
372 do_test 11.7 { set ::trigger } {{4 5 6} {7 8 9}}
374 #-------------------------------------------------------------------------
376 do_execsql_test 12.0 {
379 CREATE TRIGGER temp.r1 AFTER INSERT ON main.t2 BEGIN
380 INSERT INTO t1(a) VALUES(new.w);
382 CREATE TEMP TABLE t2(x);
385 do_execsql_test 12.1 {
386 ALTER TABLE main.t2 RENAME TO t3;
389 do_execsql_test 12.2 {
390 INSERT INTO t3 VALUES('WWW');
395 #-------------------------------------------------------------------------
397 do_execsql_test 13.0 {
398 CREATE TABLE t1(x, y);
399 CREATE TABLE t2(a, b);
401 CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
402 INSERT INTO log SELECT y FROM t1, t2;
406 do_execsql_test 13.1 {
407 INSERT INTO t1 VALUES(1, 2);
410 do_catchsql_test 13.2 {
411 ALTER TABLE t2 RENAME b TO y;
412 } {1 {error in trigger tr1 after rename: ambiguous column name: y}}
414 #-------------------------------------------------------------------------
418 do_execsql_test 14.0 {
419 CREATE VIRTUAL TABLE rt USING rtree(id, minx, maxx, miny, maxy);
421 CREATE TABLE "mytable" ( "fid" INTEGER PRIMARY KEY, "geom" BLOB);
423 CREATE TRIGGER tr1 AFTER UPDATE OF "geom" ON "mytable"
424 WHEN OLD."fid" = NEW."fid" AND NEW."geom" IS NULL BEGIN
425 DELETE FROM rt WHERE id = OLD."fid";
428 INSERT INTO mytable VALUES(1, X'abcd');
431 do_execsql_test 14.1 {
432 UPDATE mytable SET geom = X'1234'
435 do_execsql_test 14.2 {
436 ALTER TABLE mytable RENAME TO mytable_renamed;
439 do_execsql_test 14.3 {
440 CREATE TRIGGER tr2 AFTER INSERT ON mytable_renamed BEGIN
441 DELETE FROM rt WHERE id=(SELECT min(id) FROM rt);
445 do_execsql_test 14.4 {
446 ALTER TABLE mytable_renamed RENAME TO mytable2;
451 do_execsql_test 14.5 {
452 CREATE TABLE t1(a, b, c);
453 CREATE VIEW v1 AS SELECT * FROM t1;
454 CREATE TRIGGER xyz AFTER INSERT ON t1 BEGIN
458 do_execsql_test 14.6 {
459 ALTER TABLE t1 RENAME TO tt1;