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: alter3.test,v 1.11 2008/03/19 00:21:31 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 {
29 # Determine if there is a codec available on this test.
31 if {[catch {sqlite3 -has-codec} r] || $r} {
41 # alter3-1.*: Test that ALTER TABLE correctly modifies the CREATE TABLE sql.
42 # alter3-2.*: Test error messages.
43 # alter3-3.*: Test adding columns with default value NULL.
44 # alter3-4.*: Test adding columns with default values other than NULL.
45 # alter3-5.*: Test adding columns to tables in ATTACHed databases.
46 # alter3-6.*: Test that temp triggers are not accidentally dropped.
47 # alter3-7.*: Test that VACUUM resets the file-format.
50 # This procedure returns the value of the file-format in file 'test.db'.
52 proc get_file_format {{fname test.db}} {
53 return [hexio_get_int [hexio_read $fname 44 4]]
57 sqlite3_db_config db LEGACY_FILE_FORMAT 1
59 CREATE TABLE abc(a, b, c);
60 SELECT sql FROM sqlite_master;
62 } {{CREATE TABLE abc(a, b, c)}}
64 execsql {ALTER TABLE abc ADD d INTEGER;}
66 SELECT sql FROM sqlite_master;
68 } {{CREATE TABLE abc(a, b, c, d INTEGER)}}
70 execsql {ALTER TABLE abc ADD e}
72 SELECT sql FROM sqlite_master;
74 } {{CREATE TABLE abc(a, b, c, d INTEGER, e)}}
77 CREATE TABLE main.t1(a, b);
79 SELECT sql FROM sqlite_master WHERE tbl_name = 't1';
81 } {{CREATE TABLE t1(a, b, c)}}
84 ALTER TABLE t1 ADD d CHECK (a>d);
85 SELECT sql FROM sqlite_master WHERE tbl_name = 't1';
87 } {{CREATE TABLE t1(a, b, c, d CHECK (a>d))}}
88 ifcapable foreignkey {
91 CREATE TABLE t2(a, b, UNIQUE(a, b));
92 ALTER TABLE t2 ADD c REFERENCES t1(c) ;
93 SELECT sql FROM sqlite_master WHERE tbl_name = 't2' AND type = 'table';
95 } {{CREATE TABLE t2(a, b, c REFERENCES t1(c), UNIQUE(a, b))}}
99 CREATE TABLE t3(a, b, UNIQUE(a, b));
100 ALTER TABLE t3 ADD COLUMN c VARCHAR(10, 20);
101 SELECT sql FROM sqlite_master WHERE tbl_name = 't3' AND type = 'table';
103 } {{CREATE TABLE t3(a, b, c VARCHAR(10, 20), UNIQUE(a, b))}}
104 do_test alter3-1.99 {
106 # May not exist if foriegn-keys are omitted at compile time.
118 CREATE TABLE t1(a, b);
121 ALTER TABLE t1 ADD c PRIMARY KEY;
123 } {1 {Cannot add a PRIMARY KEY column}}
126 ALTER TABLE t1 ADD c UNIQUE
128 } {1 {Cannot add a UNIQUE column}}
131 ALTER TABLE t1 ADD b VARCHAR(10)
133 } {1 {duplicate column name: b}}
136 ALTER TABLE t1 ADD c NOT NULL;
138 } {1 {Cannot add a NOT NULL column with default value NULL}}
141 ALTER TABLE t1 ADD c NOT NULL DEFAULT 10;
147 CREATE VIEW v1 AS SELECT * FROM t1;
150 alter table v1 add column d;
152 } {1 {Cannot add a column to a view}}
156 alter table t1 add column d DEFAULT CURRENT_TIME;
158 } {1 {Cannot add a column with non-constant default}}
159 do_test alter3-2.99 {
167 CREATE TABLE t1(a, b);
168 INSERT INTO t1 VALUES(1, 100);
169 INSERT INTO t1 VALUES(2, 300);
175 PRAGMA schema_version = 10;
180 ALTER TABLE t1 ADD c;
183 } {1 100 {} 2 300 {}}
189 ifcapable schema_version {
192 PRAGMA schema_version;
200 set ::DB [sqlite3 db test.db]
201 sqlite3_db_config db LEGACY_FILE_FORMAT 1
203 CREATE TABLE t1(a, b);
204 INSERT INTO t1 VALUES(1, 100);
205 INSERT INTO t1 VALUES(2, 300);
211 PRAGMA schema_version = 20;
216 ALTER TABLE t1 ADD c DEFAULT 'hello world';
219 } {1 100 {hello world} 2 300 {hello world}}
225 ifcapable schema_version {
228 PRAGMA schema_version;
232 do_test alter3-4.99 {
241 forcedelete test2.db-journal
243 CREATE TABLE t1(a, b);
244 INSERT INTO t1 VALUES(1, 'one');
245 INSERT INTO t1 VALUES(2, 'two');
246 ATTACH 'test2.db' AS aux;
247 CREATE TABLE aux.t1 AS SELECT * FROM t1;
248 PRAGMA aux.schema_version = 30;
249 SELECT sql FROM aux.sqlite_master;
251 } {{CREATE TABLE t1(a,b)}}
254 ALTER TABLE aux.t1 ADD COLUMN c VARCHAR(128);
255 SELECT sql FROM aux.sqlite_master;
257 } {{CREATE TABLE t1(a,b, c VARCHAR(128))}}
260 SELECT * FROM aux.t1;
262 } {1 one {} 2 two {}}
263 ifcapable schema_version {
266 PRAGMA aux.schema_version;
272 list [get_file_format test2.db] [get_file_format]
277 ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000;
278 SELECT sql FROM aux.sqlite_master;
280 } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}}
283 SELECT * FROM aux.t1;
285 } {1 one {} 1000 2 two {} 1000}
286 ifcapable schema_version {
289 PRAGMA aux.schema_version;
298 do_test alter3-5.99 {
306 #----------------------------------------------------------------
307 # Test that the table schema is correctly reloaded when a column
308 # is added to a table.
310 ifcapable trigger&&tempdb {
313 CREATE TABLE t1(a, b);
314 CREATE TABLE log(trig, a, b);
316 CREATE TRIGGER t1_a AFTER INSERT ON t1 BEGIN
317 INSERT INTO log VALUES('a', new.a, new.b);
319 CREATE TEMP TRIGGER t1_b AFTER INSERT ON t1 BEGIN
320 INSERT INTO log VALUES('b', new.a, new.b);
323 INSERT INTO t1 VALUES(1, 2);
329 ALTER TABLE t1 ADD COLUMN c DEFAULT 'c';
330 INSERT INTO t1(a, b) VALUES(3, 4);
333 } {b 1 2 a 1 2 b 3 4 a 3 4}
346 CREATE TABLE abc(a, b, c);
347 ALTER TABLE abc ADD d DEFAULT NULL;
353 ALTER TABLE abc ADD e DEFAULT 10;
359 ALTER TABLE abc ADD f DEFAULT NULL;
372 # Ticket #1183 - Make sure adding columns to large tables does not cause
373 # memory corruption (as was the case before this bug was fixed).
382 for {set i 2} {$i < 100} {incr i} {
384 ALTER TABLE t4 ADD c$i
388 set ::sql "CREATE TABLE t4([join $cols {, }])"
393 SELECT sql FROM sqlite_master WHERE name = 't4';