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);
119 INSERT INTO t1 VALUES(1,2);
122 ALTER TABLE t1 ADD c PRIMARY KEY;
124 } {1 {Cannot add a PRIMARY KEY column}}
127 ALTER TABLE t1 ADD c UNIQUE
129 } {1 {Cannot add a UNIQUE column}}
132 ALTER TABLE t1 ADD b VARCHAR(10)
134 } {1 {duplicate column name: b}}
137 ALTER TABLE t1 ADD c NOT NULL;
139 } {1 {Cannot add a NOT NULL column with default value NULL}}
142 ALTER TABLE t1 ADD c NOT NULL DEFAULT 10;
148 CREATE VIEW v1 AS SELECT * FROM t1;
151 alter table v1 add column d;
153 } {1 {Cannot add a column to a view}}
157 alter table t1 add column d DEFAULT CURRENT_TIME;
159 } {1 {Cannot add a column with non-constant default}}
160 do_test alter3-2.99 {
168 CREATE TABLE t1(a, b);
169 INSERT INTO t1 VALUES(1, 100);
170 INSERT INTO t1 VALUES(2, 300);
176 PRAGMA schema_version = 10;
181 ALTER TABLE t1 ADD c;
184 } {1 100 {} 2 300 {}}
190 ifcapable schema_version {
193 PRAGMA schema_version;
201 set ::DB [sqlite3 db test.db]
202 sqlite3_db_config db LEGACY_FILE_FORMAT 1
204 CREATE TABLE t1(a, b);
205 INSERT INTO t1 VALUES(1, 100);
206 INSERT INTO t1 VALUES(2, 300);
212 PRAGMA schema_version = 20;
217 ALTER TABLE t1 ADD c DEFAULT 'hello world';
220 } {1 100 {hello world} 2 300 {hello world}}
226 ifcapable schema_version {
229 PRAGMA schema_version;
233 do_test alter3-4.99 {
242 forcedelete test2.db-journal
244 CREATE TABLE t1(a, b);
245 INSERT INTO t1 VALUES(1, 'one');
246 INSERT INTO t1 VALUES(2, 'two');
247 ATTACH 'test2.db' AS aux;
248 CREATE TABLE aux.t1 AS SELECT * FROM t1;
249 PRAGMA aux.schema_version = 30;
250 SELECT sql FROM aux.sqlite_master;
252 } {{CREATE TABLE t1(a,b)}}
255 ALTER TABLE aux.t1 ADD COLUMN c VARCHAR(128);
256 SELECT sql FROM aux.sqlite_master;
258 } {{CREATE TABLE t1(a,b, c VARCHAR(128))}}
261 SELECT * FROM aux.t1;
263 } {1 one {} 2 two {}}
264 ifcapable schema_version {
267 PRAGMA aux.schema_version;
273 list [get_file_format test2.db] [get_file_format]
278 ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000;
279 SELECT sql FROM aux.sqlite_master;
281 } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}}
284 SELECT * FROM aux.t1;
286 } {1 one {} 1000 2 two {} 1000}
287 ifcapable schema_version {
290 PRAGMA aux.schema_version;
299 do_test alter3-5.99 {
307 #----------------------------------------------------------------
308 # Test that the table schema is correctly reloaded when a column
309 # is added to a table.
311 ifcapable trigger&&tempdb {
314 CREATE TABLE t1(a, b);
315 CREATE TABLE log(trig, a, b);
317 CREATE TRIGGER t1_a AFTER INSERT ON t1 BEGIN
318 INSERT INTO log VALUES('a', new.a, new.b);
320 CREATE TEMP TRIGGER t1_b AFTER INSERT ON t1 BEGIN
321 INSERT INTO log VALUES('b', new.a, new.b);
324 INSERT INTO t1 VALUES(1, 2);
330 ALTER TABLE t1 ADD COLUMN c DEFAULT 'c';
331 INSERT INTO t1(a, b) VALUES(3, 4);
334 } {b 1 2 a 1 2 b 3 4 a 3 4}
347 CREATE TABLE abc(a, b, c);
348 ALTER TABLE abc ADD d DEFAULT NULL;
354 ALTER TABLE abc ADD e DEFAULT 10;
360 ALTER TABLE abc ADD f DEFAULT NULL;
373 # Ticket #1183 - Make sure adding columns to large tables does not cause
374 # memory corruption (as was the case before this bug was fixed).
383 for {set i 2} {$i < 100} {incr i} {
385 ALTER TABLE t4 ADD c$i
389 set ::sql "CREATE TABLE t4([join $cols {, }])"
394 SELECT sql FROM sqlite_master WHERE name = 't4';