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. The
12 # focus of this script is testing that SQLite can handle a subtle
13 # file format change that may be used in the future to implement
14 # "ALTER TABLE ... RENAME COLUMN ... TO".
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19 set testprefix altercol
21 # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
22 ifcapable !altertable {
27 # Drop all the tables and views in the 'main' database of database connect
28 # [db]. Sort the objects by name before dropping them.
30 proc drop_all_tables_and_views {db} {
32 SELECT name, type FROM sqlite_master
33 WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%'
36 foreach {z t} [db eval $SQL] {
41 foreach {tn before after} {
42 1 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB)}
43 {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB)}
45 2 {CREATE TABLE t1(a INTEGER, x TEXT, "b" BLOB)}
46 {CREATE TABLE t1(a INTEGER, x TEXT, "d" BLOB)}
48 3 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK(b!=''))}
49 {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK(d!=''))}
51 4 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK(t1.b!=''))}
52 {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK(t1.d!=''))}
54 5 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK( coalesce(b,c) ))}
55 {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK( coalesce(d,c) ))}
57 6 {CREATE TABLE t1(a INTEGER, "b"TEXT, c BLOB, CHECK( coalesce(b,c) ))}
58 {CREATE TABLE t1(a INTEGER, "d"TEXT, c BLOB, CHECK( coalesce(d,c) ))}
60 7 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, PRIMARY KEY(b, c))}
61 {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, PRIMARY KEY(d, c))}
63 8 {CREATE TABLE t1(a INTEGER, b TEXT PRIMARY KEY, c BLOB)}
64 {CREATE TABLE t1(a INTEGER, d TEXT PRIMARY KEY, c BLOB)}
66 9 {CREATE TABLE t1(a, b TEXT, c, PRIMARY KEY(a, b), UNIQUE("B"))}
67 {CREATE TABLE t1(a, d TEXT, c, PRIMARY KEY(a, d), UNIQUE("d"))}
69 10 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(a, c)}
70 {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(a, c)}}
72 11 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(b, c)}
73 {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(d, c)}}
75 12 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(b+b+b+b, c) WHERE b>0}
76 {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(d+d+d+d, c) WHERE d>0}}
78 13 {CREATE TABLE t1(a, b, c, FOREIGN KEY (b) REFERENCES t2)}
79 {CREATE TABLE t1(a, d, c, FOREIGN KEY (d) REFERENCES t2)}
81 14 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, PRIMARY KEY(b))}
82 {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, PRIMARY KEY(d))}
84 15 {CREATE TABLE t1(a INTEGER, b INTEGER, c BLOB, PRIMARY KEY(b))}
85 {CREATE TABLE t1(a INTEGER, d INTEGER, c BLOB, PRIMARY KEY(d))}
87 16 {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB)}
88 {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB)}
90 17 {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB, FOREIGN KEY (b) REFERENCES t2)}
91 {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB, FOREIGN KEY (d) REFERENCES t2)}
95 do_execsql_test 1.$tn.0 $before
97 do_execsql_test 1.$tn.1 {
98 INSERT INTO t1 VALUES(1, 2, 3);
101 do_execsql_test 1.$tn.2 {
102 ALTER TABLE t1 RENAME COLUMN b TO d;
105 do_execsql_test 1.$tn.3 {
109 if {[string first INDEX $before]>0} {
112 set res [list $after]
114 do_execsql_test 1.$tn.4 {
115 SELECT sql FROM sqlite_master WHERE tbl_name='t1' AND sql!=''
119 #-------------------------------------------------------------------------
121 do_execsql_test 2.0 {
122 CREATE TABLE t3(a, b, c, d, e, f, g, h, i, j, k, l, m, FOREIGN KEY (b, c, d, e, f, g, h, i, j, k, l, m) REFERENCES t4);
126 do_execsql_test -db db2 2.1 { SELECT b FROM t3 }
128 do_execsql_test 2.2 {
129 ALTER TABLE t3 RENAME b TO biglongname;
130 SELECT sql FROM sqlite_master WHERE name='t3';
131 } {{CREATE TABLE t3(a, biglongname, c, d, e, f, g, h, i, j, k, l, m, FOREIGN KEY (biglongname, c, d, e, f, g, h, i, j, k, l, m) REFERENCES t4)}}
133 do_execsql_test -db db2 2.3 { SELECT biglongname FROM t3 }
135 #-------------------------------------------------------------------------
137 do_execsql_test 3.0 {
138 CREATE TABLE t4(x, y, z);
139 CREATE TRIGGER ttt AFTER INSERT ON t4 WHEN new.y<0 BEGIN
140 SELECT x, y, z FROM t4;
141 DELETE FROM t4 WHERE y=32;
142 UPDATE t4 SET x=y+1, y=0 WHERE y=32;
143 INSERT INTO t4(x, y, z) SELECT 4, 5, 6 WHERE 0;
145 INSERT INTO t4 VALUES(3, 2, 1);
148 do_execsql_test 3.1 {
149 ALTER TABLE t4 RENAME y TO abc;
150 SELECT sql FROM sqlite_master WHERE name='t4';
151 } {{CREATE TABLE t4(x, abc, z)}}
153 do_execsql_test 3.2 {
157 do_execsql_test 3.3 { INSERT INTO t4 VALUES(6, 5, 4); } {}
159 do_execsql_test 3.4 { SELECT sql FROM sqlite_master WHERE type='trigger' } {
160 {CREATE TRIGGER ttt AFTER INSERT ON t4 WHEN new.abc<0 BEGIN
161 SELECT x, abc, z FROM t4;
162 DELETE FROM t4 WHERE abc=32;
163 UPDATE t4 SET x=abc+1, abc=0 WHERE abc=32;
164 INSERT INTO t4(x, abc, z) SELECT 4, 5, 6 WHERE 0;
168 #-------------------------------------------------------------------------
170 do_execsql_test 4.0 {
171 CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, d));
172 CREATE TABLE p1(c, d, PRIMARY KEY(c, d));
173 PRAGMA foreign_keys = 1;
174 INSERT INTO p1 VALUES(1, 2);
175 INSERT INTO p1 VALUES(3, 4);
178 do_execsql_test 4.1 {
179 ALTER TABLE p1 RENAME d TO "silly name";
180 SELECT sql FROM sqlite_master WHERE name IN ('c1', 'p1');
182 {CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "silly name"))}
183 {CREATE TABLE p1(c, "silly name", PRIMARY KEY(c, "silly name"))}
186 do_execsql_test 4.2 { INSERT INTO c1 VALUES(1, 2); }
188 do_execsql_test 4.3 {
189 CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1);
192 do_execsql_test 4.4 {
193 ALTER TABLE p1 RENAME "silly name" TO reasonable;
194 SELECT sql FROM sqlite_master WHERE name IN ('c1', 'c2', 'p1');
196 {CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "reasonable"))}
197 {CREATE TABLE p1(c, "reasonable", PRIMARY KEY(c, "reasonable"))}
198 {CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1)}
201 #-------------------------------------------------------------------------
203 do_execsql_test 5.0 {
204 CREATE TABLE t5(a, b, c);
205 CREATE INDEX t5a ON t5(a);
206 INSERT INTO t5 VALUES(1, 2, 3), (4, 5, 6);
210 do_execsql_test 5.1 {
211 ALTER TABLE t5 RENAME b TO big;
215 do_catchsql_test 6.1 {
216 ALTER TABLE sqlite_stat1 RENAME tbl TO thetable;
217 } {1 {table sqlite_stat1 may not be altered}}
219 #-------------------------------------------------------------------------
221 do_execsql_test 6.0 {
223 rid INTEGER PRIMARY KEY,
226 uuid TEXT UNIQUE NOT NULL,
228 CHECK( length(uuid)>=40 AND rid>0 )
232 do_execsql_test 6.1 {
233 ALTER TABLE "blob" RENAME COLUMN "rid" TO "a1";
236 do_catchsql_test 6.2 {
237 ALTER TABLE "blob" RENAME COLUMN "a1" TO [where];
240 do_execsql_test 6.3 {
241 SELECT "where" FROM blob;
244 #-------------------------------------------------------------------------
250 do_execsql_test 7.0 {
252 INSERT INTO c VALUES(0);
253 CREATE TABLE t6("col a", "col b", "col c");
254 CREATE TRIGGER zzz AFTER UPDATE OF "col a", "col c" ON t6 BEGIN
259 do_execsql_test 7.1.1 {
260 INSERT INTO t6 VALUES(0, 0, 0);
261 UPDATE t6 SET "col c" = 1;
265 do_execsql_test 7.1.2 {
266 ALTER TABLE t6 RENAME "col c" TO "col 3";
269 do_execsql_test 7.1.3 {
270 UPDATE t6 SET "col 3" = 0;
274 #-------------------------------------------------------------------------
278 do_execsql_test 8.0 {
279 CREATE TABLE a1(x INTEGER, y TEXT, z BLOB, PRIMARY KEY(x));
280 CREATE TABLE a2(a, b, c);
281 CREATE VIEW v1 AS SELECT x, y, z FROM a1;
284 do_execsql_test 8.1 {
285 ALTER TABLE a1 RENAME y TO yyy;
286 SELECT sql FROM sqlite_master WHERE type='view';
287 } {{CREATE VIEW v1 AS SELECT x, yyy, z FROM a1}}
289 do_execsql_test 8.2.1 {
291 CREATE VIEW v2 AS SELECT x, x+x, a, a+a FROM a1, a2;
293 do_execsql_test 8.2.2 {
294 ALTER TABLE a1 RENAME x TO xxx;
296 do_execsql_test 8.2.3 {
297 SELECT sql FROM sqlite_master WHERE type='view';
298 } {{CREATE VIEW v2 AS SELECT xxx, xxx+xxx, a, a+a FROM a1, a2}}
300 do_execsql_test 8.3.1 {
303 CREATE TABLE a2(a INTEGER PRIMARY KEY, b, c);
304 CREATE VIEW v2 AS SELECT xxx, xxx+xxx, a, a+a FROM a1, a2;
306 do_execsql_test 8.3.2 {
307 ALTER TABLE a1 RENAME xxx TO x;
309 do_execsql_test 8.3.3 {
310 SELECT sql FROM sqlite_master WHERE type='view';
311 } {{CREATE VIEW v2 AS SELECT x, x+x, a, a+a FROM a1, a2}}
313 do_execsql_test 8.4.0 {
314 CREATE TABLE b1(a, b, c);
315 CREATE TABLE b2(x, y, z);
318 do_execsql_test 8.4.1 {
319 CREATE VIEW vvv AS SELECT c+c || coalesce(c, c) FROM b1, b2 WHERE x=c GROUP BY c HAVING c>0;
320 ALTER TABLE b1 RENAME c TO "a;b";
321 SELECT sql FROM sqlite_master WHERE name='vvv';
322 } {{CREATE VIEW vvv AS SELECT "a;b"+"a;b" || coalesce("a;b", "a;b") FROM b1, b2 WHERE x="a;b" GROUP BY "a;b" HAVING "a;b">0}}
324 do_execsql_test 8.4.2 {
325 CREATE VIEW www AS SELECT b FROM b1 UNION ALL SELECT y FROM b2;
326 ALTER TABLE b1 RENAME b TO bbb;
327 SELECT sql FROM sqlite_master WHERE name='www';
328 } {{CREATE VIEW www AS SELECT bbb FROM b1 UNION ALL SELECT y FROM b2}}
330 db collate nocase {string compare}
332 do_execsql_test 8.4.3 {
333 CREATE VIEW xxx AS SELECT a FROM b1 UNION SELECT x FROM b2 ORDER BY 1 COLLATE nocase;
336 do_execsql_test 8.4.4 {
337 ALTER TABLE b2 RENAME x TO hello;
338 SELECT sql FROM sqlite_master WHERE name='xxx';
339 } {{CREATE VIEW xxx AS SELECT a FROM b1 UNION SELECT hello FROM b2 ORDER BY 1 COLLATE nocase}}
341 do_catchsql_test 8.4.5 {
342 CREATE VIEW zzz AS SELECT george, ringo FROM b1;
343 ALTER TABLE b1 RENAME a TO aaa;
344 } {1 {error in view zzz: no such column: george}}
346 #-------------------------------------------------------------------------
349 proc do_rename_column_test {tn old new lSchema} {
350 for {set i 0} {$i < 2} {incr i} {
351 drop_all_tables_and_views db
354 foreach sql $lSchema {
356 lappend lSorted [string trim $sql]
358 set lSorted [lsort $lSorted]
360 do_execsql_test $tn.$i.1 {
361 SELECT sql FROM sqlite_master WHERE sql!='' ORDER BY 1
369 do_execsql_test $tn.$i.2 "ALTER TABLE t1 RENAME $old TO $new"
371 do_execsql_test $tn.$i.3 {
372 SELECT sql FROM sqlite_master ORDER BY 1
373 } [string map [list $old $new] $lSorted]
377 foreach {tn old new lSchema} {
379 { CREATE TABLE t1(a, b, _x_) }
380 { CREATE TRIGGER AFTER INSERT ON t1 BEGIN
386 { CREATE TABLE t1(a, b, _x_) }
387 { CREATE TABLE t2(c, d, e) }
388 { CREATE TRIGGER ttt AFTER INSERT ON t2 BEGIN
394 { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) }
395 { CREATE TABLE t2(c, d, e) }
396 { CREATE TRIGGER ttt AFTER UPDATE ON t1 BEGIN
397 INSERT INTO t2 VALUES(new.a, new.b, new._x_);
402 { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) }
403 { CREATE TRIGGER ttt AFTER UPDATE ON t1 BEGIN
404 INSERT INTO t1 VALUES(new.a, new.b, new._x_)
405 ON CONFLICT (_x_) WHERE _x_>10 DO UPDATE SET _x_ = _x_+1;
410 { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) }
411 { CREATE TRIGGER ttt AFTER UPDATE ON t1 BEGIN
412 INSERT INTO t1 VALUES(new.a, new.b, new._x_)
413 ON CONFLICT (_x_) WHERE _x_>10 DO NOTHING;
417 do_rename_column_test 9.$tn $old $new $lSchema
420 #-------------------------------------------------------------------------
421 # Test that views can be edited even if there are missing collation
422 # sequences or user defined functions.
427 foreach {tn old new lSchema} {
429 { CREATE TABLE t1(a, b, _x_) }
430 { CREATE VIEW s1 AS SELECT a, b, _x_ FROM t1 WHERE _x_='abc' COLLATE xyz }
434 { CREATE TABLE t1(a, b, _x_) }
435 { CREATE VIEW v1 AS SELECT a, b, _x_ FROM t1 WHERE scalar(_x_) }
439 { CREATE TABLE t1(a, b, _x_) }
440 { CREATE VIEW v1 AS SELECT a, b, _x_ FROM t1 WHERE _x_ = unicode(1, 2, 3) }
444 { CREATE TABLE t1(a, b, _x_) }
445 { CREATE VIRTUAL TABLE e1 USING echo(t1) }
448 register_echo_module db
449 do_rename_column_test 10.$tn $old $new $lSchema
452 #--------------------------------------------------------------------------
453 # Test that if a view or trigger refers to a virtual table for which the
454 # module is not available, RENAME COLUMN cannot proceed.
457 register_echo_module db
458 do_execsql_test 11.0 {
459 CREATE TABLE x1(a, b, c);
460 CREATE VIRTUAL TABLE e1 USING echo(x1);
465 do_execsql_test 11.1 {
466 ALTER TABLE x1 RENAME b TO bbb;
467 SELECT sql FROM sqlite_master;
468 } { {CREATE TABLE x1(a, bbb, c)} {CREATE VIRTUAL TABLE e1 USING echo(x1)} }
470 do_execsql_test 11.2 {
471 CREATE VIEW v1 AS SELECT e1.*, x1.c FROM e1, x1;
474 do_catchsql_test 11.3 {
475 ALTER TABLE x1 RENAME c TO ccc;
476 } {1 {error in view v1: no such module: echo}}
479 #-------------------------------------------------------------------------
480 # Test some error conditions:
482 # 1. Renaming a column of a system table,
483 # 2. Renaming a column of a VIEW,
484 # 3. Renaming a column of a virtual table.
485 # 4. Renaming a column that does not exist.
486 # 5. Renaming a column of a table that does not exist.
489 do_execsql_test 12.1.1 {
490 CREATE TABLE t1(a, b);
491 CREATE INDEX t1a ON t1(a);
492 INSERT INTO t1 VALUES(1, 1), (2, 2), (3, 4);
495 do_catchsql_test 12.1.2 {
496 ALTER TABLE sqlite_stat1 RENAME idx TO theindex;
497 } {1 {table sqlite_stat1 may not be altered}}
498 do_execsql_test 12.1.3 {
499 SELECT sql FROM sqlite_master WHERE tbl_name = 'sqlite_stat1'
500 } {{CREATE TABLE sqlite_stat1(tbl,idx,stat)}}
502 do_execsql_test 12.2.1 {
503 CREATE VIEW v1 AS SELECT * FROM t1;
504 CREATE VIEW v2(c, d) AS SELECT * FROM t1;
506 do_catchsql_test 12.2.2 {
507 ALTER TABLE v1 RENAME a TO z;
508 } {1 {cannot rename columns of view "v1"}}
509 do_catchsql_test 12.2.3 {
510 ALTER TABLE v2 RENAME c TO y;
511 } {1 {cannot rename columns of view "v2"}}
514 do_execsql_test 12.3.1 {
515 CREATE VIRTUAL TABLE ft USING fts5(a, b, c);
517 do_catchsql_test 12.3.2 {
518 ALTER TABLE ft RENAME a TO z;
519 } {1 {cannot rename columns of virtual table "ft"}}
522 do_execsql_test 12.4.1 {
523 CREATE TABLE t2(x, y, z);
525 do_catchsql_test 12.4.2 {
526 ALTER TABLE t2 RENAME COLUMN a TO b;
527 } {1 {no such column: "a"}}
529 do_catchsql_test 12.5.1 {
530 ALTER TABLE t3 RENAME COLUMN a TO b;
531 } {1 {no such table: t3}}
533 #-------------------------------------------------------------------------
534 # Test the effect of some parse/resolve errors.
537 do_execsql_test 13.1.1 {
538 CREATE TABLE x1(i INTEGER, t TEXT UNIQUE);
539 CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
540 SELECT * FROM nosuchtable;
544 do_catchsql_test 13.1.2 {
545 ALTER TABLE x1 RENAME COLUMN t TO ttt;
546 } {1 {error in trigger tr1: no such table: main.nosuchtable}}
548 do_execsql_test 13.1.3 {
550 CREATE INDEX x1i ON x1(i);
551 SELECT sql FROM sqlite_master WHERE name='x1i';
552 } {{CREATE INDEX x1i ON x1(i)}}
554 sqlite3_db_config db DEFENSIVE 0
555 do_execsql_test 13.1.4 {
556 PRAGMA writable_schema = 1;
557 UPDATE sqlite_master SET sql = 'CREATE INDEX x1i ON x1(j)' WHERE name='x1i';
560 do_catchsql_test 13.1.5 {
561 ALTER TABLE x1 RENAME COLUMN t TO ttt;
562 } {1 {error in index x1i: no such column: j}}
564 do_execsql_test 13.1.6 {
565 UPDATE sqlite_master SET sql = '' WHERE name='x1i';
568 do_catchsql_test 13.1.7 {
569 ALTER TABLE x1 RENAME COLUMN t TO ttt;
570 } {1 {error in index x1i: }}
572 do_execsql_test 13.1.8 {
573 DELETE FROM sqlite_master WHERE name = 'x1i';
576 do_execsql_test 13.2.0 {
577 CREATE TABLE data(x UNIQUE, y, z);
579 foreach {tn trigger error} {
581 CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
582 UPDATE data SET x=x+1 WHERE zzz=new.i;
584 } {no such column: zzz}
587 CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
588 INSERT INTO data(x, y) VALUES(new.i, new.t, 1)
589 ON CONFLICT (x) DO UPDATE SET z=zz+1;
591 } {no such column: zz}
594 CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
595 INSERT INTO x1(i, t) VALUES(new.i+1, new.t||'1')
596 ON CONFLICT (tttttt) DO UPDATE SET t=i+1;
598 } {no such column: tttttt}
601 CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
602 INSERT INTO nosuchtable VALUES(new.i, new.t);
604 } {no such table: main.nosuchtable}
606 do_execsql_test 13.2.$tn.1 "
607 DROP TRIGGER IF EXISTS tr1;
611 do_catchsql_test 13.2.$tn.2 {
612 ALTER TABLE x1 RENAME COLUMN t TO ttt;
613 } "1 {error in trigger tr1: $error}"
616 #-------------------------------------------------------------------------
617 # Passing invalid parameters directly to sqlite_rename_column().
619 sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db
620 do_execsql_test 14.1 {
621 CREATE TABLE ddd(sql, type, object, db, tbl, icol, znew, bquote);
622 INSERT INTO ddd VALUES(
623 'CREATE TABLE x1(i INTEGER, t TEXT)',
624 'table', 'x1', 'main', 'x1', -1, 'zzz', 0
626 'CREATE TABLE x1(i INTEGER, t TEXT)',
627 'table', 'x1', 'main', 'x1', 2, 'zzz', 0
629 'CREATE TABLE x1(i INTEGER, t TEXT)',
630 'table', 'x1', 'main', 'notable', 0, 'zzz', 0
632 'CREATE TABLE x1(i INTEGER, t TEXT)',
633 'table', 'x1', 'main', 'ddd', -1, 'zzz', 0
637 do_execsql_test 14.2 {
639 sqlite_rename_column(sql, type, object, db, tbl, icol, znew, bquote, 0)
642 sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db
644 # If the INTERNAL_FUNCTIONS test-control is disabled (which is the default)
645 # then the sqlite_rename_table() SQL function is not accessible to
648 do_catchsql_test 14.3 {
649 SELECT sqlite_rename_column(0,0,0,0,0,0,0,0,0);
650 } {1 {no such function: sqlite_rename_column}}
652 #-------------------------------------------------------------------------
655 do_execsql_test 15.0 {
656 CREATE TABLE xxx(a, b, c);
657 SELECT a AS d FROM xxx WHERE d=0;
660 do_execsql_test 15.1 {
661 CREATE VIEW vvv AS SELECT a AS d FROM xxx WHERE d=0;
662 ALTER TABLE xxx RENAME a TO xyz;
665 do_execsql_test 15.2 {
666 SELECT sql FROM sqlite_master WHERE type='view';
667 } {{CREATE VIEW vvv AS SELECT xyz AS d FROM xxx WHERE d=0}}
669 #-------------------------------------------------------------------------
671 do_execsql_test 16.1.0 {
672 CREATE TABLE t1(a,b,c);
673 CREATE TABLE t2(d,e,f);
674 INSERT INTO t1 VALUES(1,2,3);
675 INSERT INTO t2 VALUES(4,5,6);
676 CREATE VIEW v4 AS SELECT a, d FROM t1, t2;
680 do_catchsql_test 16.1.1 {
681 ALTER TABLE t2 RENAME d TO a;
682 } {1 {error in view v4 after rename: ambiguous column name: a}}
684 do_execsql_test 16.1.2 {
688 do_execsql_test 16.1.3 {
689 CREATE UNIQUE INDEX t2d ON t2(d);
690 CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
691 INSERT INTO t2 VALUES(new.a, new.b, new.c)
692 ON CONFLICT(d) DO UPDATE SET f = excluded.f;
696 do_execsql_test 16.1.4 {
697 INSERT INTO t1 VALUES(4, 8, 456);
701 do_execsql_test 16.1.5 {
702 ALTER TABLE t2 RENAME COLUMN f TO "big f";
703 INSERT INTO t1 VALUES(4, 0, 20456);
707 do_execsql_test 16.1.6 {
708 ALTER TABLE t1 RENAME COLUMN c TO "big c";
709 INSERT INTO t1 VALUES(4, 0, 0);
713 do_execsql_test 16.2.1 {
714 CREATE VIEW temp.v5 AS SELECT "big c" FROM t1;
718 do_execsql_test 16.2.2 {
719 ALTER TABLE t1 RENAME COLUMN "big c" TO reallybigc;
722 do_execsql_test 16.2.3 {
726 #-------------------------------------------------------------------------
728 do_execsql_test 17.0 {
729 CREATE TABLE u7(x, y, z);
730 CREATE TRIGGER u7t AFTER INSERT ON u7 BEGIN
731 INSERT INTO u8 VALUES(new.x, new.y, new.z);
734 do_catchsql_test 17.1 {
735 ALTER TABLE u7 RENAME x TO xxx;
736 } {1 {error in trigger u7t: no such table: main.u8}}
738 do_execsql_test 17.2 {
739 CREATE TEMP TABLE uu7(x, y, z);
740 CREATE TRIGGER uu7t AFTER INSERT ON uu7 BEGIN
741 INSERT INTO u8 VALUES(new.x, new.y, new.z);
744 do_catchsql_test 17.3 {
745 ALTER TABLE uu7 RENAME x TO xxx;
746 } {1 {error in trigger uu7t: no such table: u8}}
750 do_execsql_test 18.0 {
751 ATTACH 'test.db2' AS aux;
753 CREATE TABLE aux.log(v);
754 CREATE TEMP TRIGGER tr1 AFTER INSERT ON t1 BEGIN
755 INSERT INTO log VALUES(new.a);
757 INSERT INTO t1 VALUES(111);
761 do_execsql_test 18.1 {
762 ALTER TABLE t1 RENAME a TO b;
766 do_execsql_test 19.0 {
767 CREATE TABLE t1(a, b);
768 CREATE TABLE t2(c, d);
769 CREATE VIEW v2(e) AS SELECT coalesce(t2.c,t1.a) FROM t1, t2 WHERE t1.b=t2.d;
772 do_execsql_test 19.1 {
773 ALTER TABLE t1 RENAME a TO f;
774 SELECT sql FROM sqlite_master WHERE name = 'v2';
776 {CREATE VIEW v2(e) AS SELECT coalesce(t2.c,t1.f) FROM t1, t2 WHERE t1.b=t2.d}
779 # 2019-01-08: https://www.sqlite.org/src/tktview/bc8d94f0fbd633fd9a051e3
781 # ALTER TABLE RENAME COLUMN does not work for tables that have redundant
782 # UNIQUE constraints.
785 do_execsql_test 20.100 {
786 CREATE TABLE t1(aaa,b,c,UNIQUE(aaA),PRIMARY KEY(aAa),UNIQUE(aAA));
787 ALTER TABLE t1 RENAME aaa TO bbb;
788 SELECT sql FROM sqlite_master WHERE name='t1';
789 } {{CREATE TABLE t1(bbb,b,c,UNIQUE(bbb),PRIMARY KEY(bbb),UNIQUE(bbb))}}
790 do_execsql_test 20.105 {
792 CREATE TABLE t1(aaa,b,c,UNIQUE(aaA),PRIMARY KEY(aAa),UNIQUE(aAA))WITHOUT ROWID;
793 ALTER TABLE t1 RENAME aaa TO bbb;
794 SELECT sql FROM sqlite_master WHERE name='t1';
795 } {{CREATE TABLE t1(bbb,b,c,UNIQUE(bbb),PRIMARY KEY(bbb),UNIQUE(bbb))WITHOUT ROWID}}
796 do_execsql_test 20.110 {
798 CREATE TABLE t1(aa UNIQUE,bb UNIQUE,cc UNIQUE,UNIQUE(aA),PRIMARY KEY(bB),UNIQUE(cC));
799 ALTER TABLE t1 RENAME aa TO xx;
800 ALTER TABLE t1 RENAME bb TO yy;
801 ALTER TABLE t1 RENAME cc TO zz;
802 SELECT sql FROM sqlite_master WHERE name='t1';
803 } {{CREATE TABLE t1(xx UNIQUE,yy UNIQUE,zz UNIQUE,UNIQUE(xx),PRIMARY KEY(yy),UNIQUE(zz))}}
805 #-------------------------------------------------------------------------
807 do_execsql_test 21.0 {
808 CREATE TABLE t1(a, b, c NOT NULL);
809 CREATE TRIGGER tr1 AFTER INSERT ON t1 WHEN new.c IS NOT NULL BEGIN
810 SELECT c NOT NULL FROM t1;
814 do_execsql_test 21.1 {
815 ALTER TABLE t1 RENAME c TO d;
818 do_execsql_test 21.2 {
819 SELECT sql FROM sqlite_schema WHERE name IS 'tr1'
820 } {{CREATE TRIGGER tr1 AFTER INSERT ON t1 WHEN new.d IS NOT NULL BEGIN
821 SELECT d NOT NULL FROM t1;
825 #-------------------------------------------------------------------------
828 do_execsql_test 22.0 {
829 CREATE TABLE t1(a, b);
830 CREATE TABLE t2(c, othername, extra AS (c + 1));
831 ALTER TABLE t1 RENAME a to othername;
832 SELECT sql FROM sqlite_schema;
834 {CREATE TABLE t1(othername, b)}
835 {CREATE TABLE t2(c, othername, extra AS (c + 1))}
838 #-------------------------------------------------------------------------
841 do_execsql_test 22.0 {
842 CREATE TABLE t1(a, b);
843 CREATE INDEX x1 on t1("c"=b);
844 INSERT INTO t1 VALUES('a', 'a');
845 INSERT INTO t1 VALUES('b', 'b');
846 INSERT INTO t1 VALUES('c', 'c');
847 ALTER TABLE t1 RENAME COLUMN a TO "c";
848 PRAGMA integrity_check;
852 do_execsql_test 23.0 {
853 CREATE TABLE t1('a'"b",c);
854 CREATE INDEX i1 ON t1('a');
855 INSERT INTO t1 VALUES(1,2), (3,4);
856 ALTER TABLE t1 RENAME COLUMN a TO x;
857 PRAGMA integrity_check;
858 SELECT sql FROM sqlite_schema WHERE name='t1';
860 } {ok {CREATE TABLE t1("x" "b",c)}}