xref: /sqlite-3.40.0/test/altertab2.test (revision e89feee5)
1# 2018 September 30
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
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15set testprefix altertab
16
17# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
18ifcapable !altertable {
19  finish_test
20  return
21}
22
23ifcapable fts5 {
24  do_execsql_test 1.0 {
25    CREATE TABLE rr(a, b);
26    CREATE VIRTUAL TABLE ff USING fts5(a, b);
27    CREATE TRIGGER tr1 AFTER INSERT ON rr BEGIN
28      INSERT INTO ff VALUES(new.a, new.b);
29    END;
30    INSERT INTO rr VALUES('hello', 'world');
31    SELECT * FROM ff;
32  } {hello world}
33
34  do_execsql_test 1.1 {
35    ALTER TABLE ff RENAME TO ffff;
36  }
37
38  do_execsql_test 1.2 {
39    INSERT INTO rr VALUES('in', 'tcl');
40    SELECT * FROM ffff;
41  } {hello world in tcl}
42}
43
44#-------------------------------------------------------------------------
45# Check that table names that appear in REFERENCES clauses are updated
46# when a table is renamed unless:
47#
48#   a) "PRAGMA legacy_alter_table" is true, and
49#   b) "PRAGMA foreign_keys" is false.
50#
51do_execsql_test 2.0 {
52  CREATE TABLE p1(a PRIMARY KEY, b);
53  CREATE TABLE c1(x REFERENCES p1);
54  CREATE TABLE c2(x, FOREIGN KEY (x) REFERENCES p1);
55  CREATE TABLE c3(x, FOREIGN KEY (x) REFERENCES p1(a));
56}
57
58do_execsql_test 2.1 {
59  ALTER TABLE p1 RENAME TO p2;
60  SELECT sql FROM sqlite_master WHERE name LIKE 'c%';
61} {
62  {CREATE TABLE c1(x REFERENCES "p2")}
63  {CREATE TABLE c2(x, FOREIGN KEY (x) REFERENCES "p2")}
64  {CREATE TABLE c3(x, FOREIGN KEY (x) REFERENCES "p2"(a))}
65}
66
67do_execsql_test 2.2 {
68  PRAGMA legacy_alter_table = 1;
69  ALTER TABLE p2 RENAME TO p3;
70  SELECT sql FROM sqlite_master WHERE name LIKE 'c%';
71} {
72  {CREATE TABLE c1(x REFERENCES "p2")}
73  {CREATE TABLE c2(x, FOREIGN KEY (x) REFERENCES "p2")}
74  {CREATE TABLE c3(x, FOREIGN KEY (x) REFERENCES "p2"(a))}
75}
76
77do_execsql_test 2.3 {
78  ALTER TABLE p3 RENAME TO p2;
79  PRAGMA foreign_keys = 1;
80  ALTER TABLE p2 RENAME TO p3;
81  SELECT sql FROM sqlite_master WHERE name LIKE 'c%';
82} {
83  {CREATE TABLE c1(x REFERENCES "p3")}
84  {CREATE TABLE c2(x, FOREIGN KEY (x) REFERENCES "p3")}
85  {CREATE TABLE c3(x, FOREIGN KEY (x) REFERENCES "p3"(a))}
86}
87
88
89finish_test
90