xref: /sqlite-3.40.0/test/schema3.test (revision 37f3ac8f)
1# 2010 Jun 28
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# This file implements regression tests for SQLite library.
12#
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16source $testdir/malloc_common.tcl
17source $testdir/lock_common.tcl
18
19# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
20ifcapable !altertable {
21  finish_test
22  return
23}
24
25# This block tests that if one client modifies the database schema, a
26# second client updates its internal cache of the database schema before
27# executing any queries. Specifically, it does not return a "no such column"
28# or "no such table" error if the table or column in question does exist
29# but was added after the second client loaded its cache of the database
30# schema.
31#
32# Types of schema modifications:
33#
34#   1. Adding a database table.
35#   2. Adding a database view.
36#   3. Adding a database index.
37#   4. Adding a database trigger.
38#   5. Adding a column to an existing table (ALTER TABLE).
39#
40do_multiclient_test tn {
41
42  # Have connections [db1] and [db2] load the current database schema.
43  #
44  sql1 { SELECT * FROM sqlite_master }
45  sql2 { SELECT * FROM sqlite_master }
46
47  foreach {tn2 c1 c2} {
48    1  { CREATE TABLE t1(a, b) }       { SELECT * FROM t1            }
49    2  { CREATE TABLE t2(a, b) }       { UPDATE t2 SET a = b         }
50    3  { CREATE TABLE t3(a, b) }       { DELETE FROM t3              }
51    4  { CREATE TABLE t4(a, b) }       { INSERT INTO t4 VALUES(1, 2) }
52    5  { CREATE TABLE t5(a, b) }       { DROP TABLE t5 }
53    6  { CREATE TABLE t6(a, b) }       { CREATE INDEX i1 ON t6(a) }
54
55    7  { ALTER TABLE t1 ADD COLUMN c } { SELECT a, b, c FROM t1 }
56    8  { ALTER TABLE t2 ADD COLUMN c } { UPDATE t2 SET a = c }
57    9  { ALTER TABLE t2 ADD COLUMN d } { UPDATE t2 SET d = c }
58    10 { ALTER TABLE t3 ADD COLUMN c } { DELETE FROM t3 WHERE c>10 }
59    11 { ALTER TABLE t4 ADD COLUMN c } { INSERT INTO t4(a,b,c) VALUES(1,2,3) }
60    12 { ALTER TABLE t6 ADD COLUMN c } { CREATE INDEX i2 ON t6(c) }
61    13 { ALTER TABLE t6 ADD COLUMN d } {
62         CREATE TRIGGER tr1 AFTER UPDATE OF d ON t6 BEGIN
63           SELECT 1, 2, 3;
64         END;
65    }
66
67    14 { CREATE INDEX i3 ON t1(a) }    { DROP INDEX i3 }
68    15 { CREATE INDEX i4 ON t2(a) }    {
69         SELECT * FROM t2 INDEXED BY i4 ORDER BY a
70    }
71
72    16 { CREATE TRIGGER tr2 AFTER INSERT ON t3 BEGIN SELECT 1 ; END } {
73         DROP TRIGGER tr2
74    }
75
76    17  { CREATE VIEW v1 AS SELECT * FROM t1 } { SELECT a,b,c   FROM v1 }
77    18  { ALTER TABLE t1 ADD COLUMN d        } { SELECT a,b,c,d FROM v1 }
78
79    19  { CREATE TABLE t7(a, b) } {
80          DROP TABLE IF EXISTS t7; CREATE TABLE t7(c, d);
81    }
82    20  { CREATE INDEX i5 ON t7(c, d) } {
83          DROP INDEX IF EXISTS i5; CREATE INDEX i5 ON t7(c)
84    }
85    21  { CREATE TRIGGER tr3 BEFORE DELETE ON t7 BEGIN SELECT 1, 2, 3 ; END } {
86          DROP TRIGGER IF EXISTS tr3;
87          CREATE TRIGGER tr3 AFTER INSERT ON t7 BEGIN SELECT 1, 2, 3 ; END
88    }
89
90    22  { CREATE TABLE t8(a, b) }       {
91         CREATE TRIGGER tr4 AFTER UPDATE OF a ON t8 BEGIN
92           SELECT 1, 2, 3;
93         END;
94    }
95  } {
96    do_test schema3-1.$tn.$tn2 {
97      sql1 $c1
98      sql2 $c2
99    } {}
100  }
101}
102
103finish_test
104