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}}
148 alter table t1 add column d default (-5+1);
150 } {1 {Cannot add a column with non-constant default}}
151 do_test alter4-2.99 {
159 CREATE TEMP TABLE t1(a, b);
160 INSERT INTO t1 VALUES(1, 100);
161 INSERT INTO t1 VALUES(2, 300);
167 PRAGMA schema_version = 10;
172 ALTER TABLE t1 ADD c;
175 } {1 100 {} 2 300 {}}
176 ifcapable schema_version {
179 PRAGMA schema_version;
187 set ::DB [sqlite3 db test.db]
189 CREATE TEMP TABLE t1(a, b);
190 INSERT INTO t1 VALUES(1, 100);
191 INSERT INTO t1 VALUES(2, 300);
197 PRAGMA schema_version = 20;
202 ALTER TABLE t1 ADD c DEFAULT 'hello world';
205 } {1 100 {hello world} 2 300 {hello world}}
206 ifcapable schema_version {
209 PRAGMA schema_version;
213 do_test alter4-4.99 {
222 forcedelete test2.db-journal
224 CREATE TEMP TABLE t1(a, b);
225 INSERT INTO t1 VALUES(1, 'one');
226 INSERT INTO t1 VALUES(2, 'two');
227 ATTACH 'test2.db' AS aux;
228 CREATE TABLE aux.t1 AS SELECT * FROM t1;
229 PRAGMA aux.schema_version = 30;
230 SELECT sql FROM aux.sqlite_master;
232 } {{CREATE TABLE t1(a,b)}}
235 ALTER TABLE aux.t1 ADD COLUMN c VARCHAR(128);
236 SELECT sql FROM aux.sqlite_master;
238 } {{CREATE TABLE t1(a,b, c VARCHAR(128))}}
241 SELECT * FROM aux.t1;
243 } {1 one {} 2 two {}}
244 ifcapable schema_version {
247 PRAGMA aux.schema_version;
253 ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000;
254 SELECT sql FROM aux.sqlite_master;
256 } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}}
259 SELECT * FROM aux.t1;
261 } {1 one {} 1000 2 two {} 1000}
262 ifcapable schema_version {
265 PRAGMA aux.schema_version;
274 do_test alter4-5.99 {
282 #----------------------------------------------------------------
283 # Test that the table schema is correctly reloaded when a column
284 # is added to a table.
286 ifcapable trigger&&tempdb {
289 CREATE TEMP TABLE t1(a, b);
290 CREATE TEMP TABLE log(trig, a, b);
292 CREATE TRIGGER t1_a AFTER INSERT ON t1 BEGIN
293 INSERT INTO log VALUES('a', new.a, new.b);
295 CREATE TEMP TRIGGER t1_b AFTER INSERT ON t1 BEGIN
296 INSERT INTO log VALUES('b', new.a, new.b);
299 INSERT INTO t1 VALUES(1, 2);
305 ALTER TABLE t1 ADD COLUMN c DEFAULT 'c';
306 INSERT INTO t1(a, b) VALUES(3, 4);
309 } {b 1 2 a 1 2 b 3 4 a 3 4}
312 # Ticket #1183 - Make sure adding columns to large tables does not cause
313 # memory corruption (as was the case before this bug was fixed).
316 CREATE TEMP TABLE t4(c1);
322 for {set i 2} {$i < 100} {incr i} {
324 ALTER TABLE t4 ADD c$i
328 set ::sql "CREATE TABLE t4([join $cols {, }])"
333 SELECT sql FROM sqlite_temp_master WHERE name = 't4';
338 # Test that a default value equal to -1 multipied by the smallest possible
339 # 64-bit integer is correctly converted to a real.
340 do_execsql_test alter4-9.1 {
342 a INTEGER DEFAULT -9223372036854775808,
343 b INTEGER DEFAULT (-(-9223372036854775808))
345 INSERT INTO t5 DEFAULT VALUES;
348 do_execsql_test alter4-9.2 { SELECT typeof(a), a, typeof(b), b FROM t5; } {
349 integer -9223372036854775808
350 real 9.22337203685478e+18
353 do_execsql_test alter4-9.3 {
354 ALTER TABLE t5 ADD COLUMN c INTEGER DEFAULT (-(-9223372036854775808));
355 SELECT typeof(c), c FROM t5;
356 } {real 9.22337203685478e+18}