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 ... ADD COLUMN".
16 # $Id: alter4.test,v 1.1 2009/02/02 18:03:22 drh Exp $
19 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
24 ifcapable !altertable {
33 # alter4-1.*: Test that ALTER TABLE correctly modifies the CREATE TABLE sql.
34 # alter4-2.*: Test error messages.
35 # alter4-3.*: Test adding columns with default value NULL.
36 # alter4-4.*: Test adding columns with default values other than NULL.
37 # alter4-5.*: Test adding columns to tables in ATTACHed databases.
38 # alter4-6.*: Test that temp triggers are not accidentally dropped.
39 # alter4-7.*: Test that VACUUM resets the file-format.
44 CREATE TEMP TABLE abc(a, b, c);
45 SELECT sql FROM sqlite_temp_master;
47 } {{CREATE TABLE abc(a, b, c)}}
49 execsql {ALTER TABLE abc ADD d INTEGER;}
51 SELECT sql FROM sqlite_temp_master;
53 } {{CREATE TABLE abc(a, b, c, d INTEGER)}}
55 execsql {ALTER TABLE abc ADD e}
57 SELECT sql FROM sqlite_temp_master;
59 } {{CREATE TABLE abc(a, b, c, d INTEGER, e)}}
62 CREATE TABLE temp.t1(a, b);
64 SELECT sql FROM sqlite_temp_master WHERE tbl_name = 't1';
66 } {{CREATE TABLE t1(a, b, c)}}
69 ALTER TABLE t1 ADD d CHECK (a>d);
70 SELECT sql FROM sqlite_temp_master WHERE tbl_name = 't1';
72 } {{CREATE TABLE t1(a, b, c, d CHECK (a>d))}}
73 ifcapable foreignkey {
76 CREATE TEMP TABLE t2(a, b, UNIQUE(a, b));
77 ALTER TABLE t2 ADD c REFERENCES t1(c) ;
78 SELECT sql FROM sqlite_temp_master
79 WHERE tbl_name = 't2' AND type = 'table';
81 } {{CREATE TABLE t2(a, b, c REFERENCES t1(c), UNIQUE(a, b))}}
85 CREATE TEMPORARY TABLE t3(a, b, UNIQUE(a, b));
86 ALTER TABLE t3 ADD COLUMN c VARCHAR(10, 20);
87 SELECT sql FROM sqlite_temp_master
88 WHERE tbl_name = 't3' AND type = 'table';
90 } {{CREATE TABLE t3(a, b, c VARCHAR(10, 20), UNIQUE(a, b))}}
93 # May not exist if foriegn-keys are omitted at compile time.
105 CREATE TABLE temp.t1(a, b);
108 ALTER TABLE t1 ADD c PRIMARY KEY;
110 } {1 {Cannot add a PRIMARY KEY column}}
113 ALTER TABLE t1 ADD c UNIQUE
115 } {1 {Cannot add a UNIQUE column}}
118 ALTER TABLE t1 ADD b VARCHAR(10)
120 } {1 {duplicate column name: b}}
123 ALTER TABLE t1 ADD c NOT NULL;
125 } {1 {Cannot add a NOT NULL column with default value NULL}}
128 ALTER TABLE t1 ADD c NOT NULL DEFAULT 10;
134 CREATE TEMPORARY VIEW v1 AS SELECT * FROM t1;
137 alter table v1 add column d;
139 } {1 {Cannot add a column to a view}}
143 alter table t1 add column d DEFAULT CURRENT_TIME;
145 } {1 {Cannot add a column with non-constant default}}
146 do_test alter4-2.99 {
154 CREATE TEMP TABLE t1(a, b);
155 INSERT INTO t1 VALUES(1, 100);
156 INSERT INTO t1 VALUES(2, 300);
162 PRAGMA schema_version = 10;
167 ALTER TABLE t1 ADD c;
170 } {1 100 {} 2 300 {}}
171 ifcapable schema_version {
174 PRAGMA schema_version;
181 file delete -force test.db
182 set ::DB [sqlite3 db test.db]
184 CREATE TEMP TABLE t1(a, b);
185 INSERT INTO t1 VALUES(1, 100);
186 INSERT INTO t1 VALUES(2, 300);
192 PRAGMA schema_version = 20;
197 ALTER TABLE t1 ADD c DEFAULT 'hello world';
200 } {1 100 {hello world} 2 300 {hello world}}
201 ifcapable schema_version {
204 PRAGMA schema_version;
208 do_test alter4-4.99 {
216 file delete -force test2.db
217 file delete -force test2.db-journal
219 CREATE TEMP TABLE t1(a, b);
220 INSERT INTO t1 VALUES(1, 'one');
221 INSERT INTO t1 VALUES(2, 'two');
222 ATTACH 'test2.db' AS aux;
223 CREATE TABLE aux.t1 AS SELECT * FROM t1;
224 PRAGMA aux.schema_version = 30;
225 SELECT sql FROM aux.sqlite_master;
227 } {{CREATE TABLE t1(a,b)}}
230 ALTER TABLE aux.t1 ADD COLUMN c VARCHAR(128);
231 SELECT sql FROM aux.sqlite_master;
233 } {{CREATE TABLE t1(a,b, c VARCHAR(128))}}
236 SELECT * FROM aux.t1;
238 } {1 one {} 2 two {}}
239 ifcapable schema_version {
242 PRAGMA aux.schema_version;
248 ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000;
249 SELECT sql FROM aux.sqlite_master;
251 } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}}
254 SELECT * FROM aux.t1;
256 } {1 one {} 1000 2 two {} 1000}
257 ifcapable schema_version {
260 PRAGMA aux.schema_version;
269 do_test alter4-5.99 {
277 #----------------------------------------------------------------
278 # Test that the table schema is correctly reloaded when a column
279 # is added to a table.
281 ifcapable trigger&&tempdb {
284 CREATE TEMP TABLE t1(a, b);
285 CREATE TEMP TABLE log(trig, a, b);
287 CREATE TRIGGER t1_a AFTER INSERT ON t1 BEGIN
288 INSERT INTO log VALUES('a', new.a, new.b);
290 CREATE TEMP TRIGGER t1_b AFTER INSERT ON t1 BEGIN
291 INSERT INTO log VALUES('b', new.a, new.b);
294 INSERT INTO t1 VALUES(1, 2);
300 ALTER TABLE t1 ADD COLUMN c DEFAULT 'c';
301 INSERT INTO t1(a, b) VALUES(3, 4);
304 } {b 1 2 a 1 2 b 3 4 a 3 4}
307 # Ticket #1183 - Make sure adding columns to large tables does not cause
308 # memory corruption (as was the case before this bug was fixed).
311 CREATE TEMP TABLE t4(c1);
317 for {set i 2} {$i < 100} {incr i} {
319 ALTER TABLE t4 ADD c$i
323 set ::sql "CREATE TABLE t4([join $cols {, }])"
328 SELECT sql FROM sqlite_temp_master WHERE name = 't4';