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: alter2.test,v 1.14 2009/04/07 14:14:22 danielk1977 Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # We have to have pragmas in order to do this test
23 ifcapable {!pragma} return
25 # Do not use a codec for tests in this file, as the database file is
26 # manipulated directly using tcl scripts. See proc [set_file_format].
30 # The file format change affects the way row-records stored in tables (but
31 # not indices) are interpreted. Before version 3.1.3, a row-record for a
32 # table with N columns was guaranteed to contain exactly N fields. As
33 # of version 3.1.3, the record may contain up to N fields. In this case
34 # the M fields that are present are the values for the left-most M
35 # columns. The (N-M) rightmost columns contain NULL.
37 # If any records in the database contain less fields than their table
38 # has columns, then the file-format meta value should be set to (at least) 2.
41 # This procedure sets the value of the file-format in file 'test.db'
42 # to $newval. Also, the schema cookie is incremented.
44 proc set_file_format {newval} {
45 hexio_write test.db 44 [hexio_render_int32 $newval]
46 set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
48 hexio_write test.db 40 [hexio_render_int32 $schemacookie]
52 # This procedure returns the value of the file-format in file 'test.db'.
54 proc get_file_format {{fname test.db}} {
55 return [hexio_get_int [hexio_read $fname 44 4]]
58 # This procedure sets the SQL statement stored for table $tbl in the
59 # sqlite_master table of file 'test.db' to $sql. Also set the file format
60 # to the supplied value. This is 2 if the added column has a default that is
61 # NULL, or 3 otherwise.
63 proc alter_table {tbl sql {file_format 2}} {
65 set s [string map {' ''} $sql]
66 set t [string map {' ''} $tbl]
67 sqlite3_db_config dbat DEFENSIVE 0
69 PRAGMA writable_schema = 1;
70 UPDATE sqlite_master SET sql = '$s' WHERE name = '$t' AND type = 'table';
71 PRAGMA writable_schema = 0;
77 # Create bogus application-defined functions for functions used
78 # internally by ALTER TABLE, to ensure that ALTER TABLE falls back
79 # to the built-in functions.
81 proc failing_app_func {args} {error "bad function"}
83 db func substr failing_app_func
84 db func like failing_app_func
85 db func sqlite_rename_table failing_app_func
86 db func sqlite_rename_trigger failing_app_func
87 db func sqlite_rename_parent failing_app_func
88 catchsql {SELECT substr('abcdefg',1,3)}
92 #-----------------------------------------------------------------------
93 # Some basic tests to make sure short rows are handled.
95 sqlite3_db_config db DEFENSIVE 0
98 CREATE TABLE abc(a, b);
99 INSERT INTO abc VALUES(1, 2);
100 INSERT INTO abc VALUES(3, 4);
101 INSERT INTO abc VALUES(5, 6);
105 # ALTER TABLE abc ADD COLUMN c;
106 alter_table abc {CREATE TABLE abc(a, b, c);}
112 } {1 2 {} 3 4 {} 5 6 {}}
115 UPDATE abc SET c = 10 WHERE a = 1;
118 } {1 2 10 3 4 {} 5 6 {}}
121 CREATE INDEX abc_i ON abc(c);
126 SELECT c FROM abc ORDER BY c;
131 SELECT * FROM abc WHERE c = 10;
136 SELECT sum(a), c FROM abc GROUP BY c;
140 # ALTER TABLE abc ADD COLUMN d;
141 alter_table abc {CREATE TABLE abc(a, b, c, d);}
142 if {[permutation] == "prepare"} { db cache flush }
143 execsql { SELECT * FROM abc; }
145 UPDATE abc SET d = 11 WHERE c IS NULL AND a<4;
148 } {1 2 10 {} 3 4 {} 11 5 6 {} {}}
149 do_test alter2-1.10 {
151 SELECT typeof(d) FROM abc;
153 } {null integer null}
154 do_test alter2-1.99 {
160 #-----------------------------------------------------------------------
161 # Test that views work when the underlying table structure is changed.
166 CREATE TABLE abc2(a, b, c);
167 INSERT INTO abc2 VALUES(1, 2, 10);
168 INSERT INTO abc2 VALUES(3, 4, NULL);
169 INSERT INTO abc2 VALUES(5, 6, NULL);
170 CREATE VIEW abc2_v AS SELECT * FROM abc2;
171 SELECT * FROM abc2_v;
173 } {1 2 10 3 4 {} 5 6 {}}
175 # ALTER TABLE abc ADD COLUMN d;
176 alter_table abc2 {CREATE TABLE abc2(a, b, c, d);}
178 SELECT * FROM abc2_v;
180 } {1 2 10 {} 3 4 {} {} 5 6 {} {}}
189 #-----------------------------------------------------------------------
190 # Test that triggers work when a short row is copied to the old.*
191 # trigger pseudo-table.
196 CREATE TABLE abc3(a, b);
197 CREATE TABLE blog(o, n);
198 CREATE TRIGGER abc3_t AFTER UPDATE OF b ON abc3 BEGIN
199 INSERT INTO blog VALUES(old.b, new.b);
205 INSERT INTO abc3 VALUES(1, 4);
206 UPDATE abc3 SET b = 2 WHERE b = 4;
212 INSERT INTO abc3 VALUES(3, 4);
213 INSERT INTO abc3 VALUES(5, 6);
215 alter_table abc3 {CREATE TABLE abc3(a, b, c);}
219 } {1 2 {} 3 4 {} 5 6 {}}
222 UPDATE abc3 SET b = b*2 WHERE a<4;
225 } {1 4 {} 3 8 {} 5 6 {}}
234 CREATE TABLE clog(o, n);
235 CREATE TRIGGER abc3_t2 AFTER UPDATE OF c ON abc3 BEGIN
236 INSERT INTO clog VALUES(old.c, new.c);
238 UPDATE abc3 SET c = a*2;
243 execsql { CREATE TABLE abc3(a, b); }
246 #---------------------------------------------------------------------
247 # Check that an error occurs if the database is upgraded to a file
248 # format that SQLite does not support (in this case 5). Note: The
249 # file format is checked each time the schema is read, so changing the
250 # file format requires incrementing the schema cookie.
255 catch { sqlite3 db test.db }
259 # We have to run two queries here because the Tcl interface uses
260 # sqlite3_prepare_v2(). In this case, the first query encounters an
261 # SQLITE_SCHEMA error. Then, when trying to recompile the statement, the
262 # "unsupported file format" error is encountered. So the error code
263 # returned is SQLITE_SCHEMA, not SQLITE_ERROR as required by the following
266 # When the query is attempted a second time, the same error message is
267 # returned but the error code is SQLITE_ERROR, because the unsupported
268 # file format was detected during a call to sqlite3_prepare(), not
271 catchsql { SELECT * FROM sqlite_master; }
272 catchsql { SELECT * FROM sqlite_master; }
273 } {1 {unsupported file format}}
278 set ::DB [sqlite3_connection_pointer db]
280 SELECT * FROM sqlite_master;
282 } {1 {unsupported file format}}
287 #---------------------------------------------------------------------
288 # Check that executing VACUUM on a file with file-format version 2
289 # resets the file format to 1.
291 set default_file_format [expr $SQLITE_DEFAULT_FILE_FORMAT==4 ? 4 : 1]
297 execsql {SELECT 1 FROM sqlite_master LIMIT 1;}
305 } $default_file_format
308 #---------------------------------------------------------------------
309 # Test that when a database with file-format 2 is opened, new
310 # databases are still created with file-format 1.
320 forcedelete test2.db-journal
323 ATTACH 'test2.db' AS aux;
324 CREATE TABLE aux.t1(a, b);
326 get_file_format test2.db
327 } $default_file_format
331 CREATE TABLE t1(a, b);
336 #---------------------------------------------------------------------
337 # Test that types and values for columns added with default values
338 # other than NULL work with SELECT statements.
344 INSERT INTO t1 VALUES(1);
345 INSERT INTO t1 VALUES(2);
346 INSERT INTO t1 VALUES(3);
347 INSERT INTO t1 VALUES(4);
352 set sql {CREATE TABLE t1(a, b DEFAULT '123', c INTEGER DEFAULT '123')}
353 alter_table t1 $sql 3
355 SELECT * FROM t1 LIMIT 1;
360 SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
362 } {1 integer 123 text 123 integer}
365 SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
367 } {1 integer 123 text 123 integer}
369 set sql {CREATE TABLE t1(a, b DEFAULT -123.0, c VARCHAR(10) default 5)}
370 alter_table t1 $sql 3
372 SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
374 } {1 integer -123 integer 5 text}
376 #-----------------------------------------------------------------------
377 # Test that UPDATE trigger tables work with default values, and that when
378 # a row is updated the default values are correctly transfered to the
382 db function set_val {set ::val}
385 CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
387 old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)||' '||
388 new.b||' '||typeof(new.b)||' '||new.c||' '||typeof(new.c)
397 UPDATE t1 SET c = 10 WHERE a = 1;
398 SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
400 } {1 integer -123 integer 10 text}
404 } {-123 integer 5 text -123 integer 10 text}
407 #-----------------------------------------------------------------------
408 # Test that DELETE trigger tables work with default values, and that when
409 # a row is updated the default values are correctly transfered to the
415 CREATE TRIGGER trig2 BEFORE DELETE ON t1 BEGIN
417 old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)
425 DELETE FROM t1 WHERE a = 2;
428 } {-123 integer 5 text}
431 #-----------------------------------------------------------------------
432 # Test creating an index on a column added with a default value.
435 do_test alter2-10.1 {
438 INSERT INTO t2 VALUES('a');
439 INSERT INTO t2 VALUES('b');
440 INSERT INTO t2 VALUES('c');
441 INSERT INTO t2 VALUES('d');
443 alter_table t2 {CREATE TABLE t2(a, b DEFAULT X'ABCD', c DEFAULT NULL);} 3
445 SELECT * FROM sqlite_master;
448 SELECT quote(a), quote(b), quote(c) FROM t2 LIMIT 1;
451 do_test alter2-10.2 {
453 CREATE INDEX i1 ON t2(b);
454 SELECT a FROM t2 WHERE b = X'ABCD';
457 do_test alter2-10.3 {
459 DELETE FROM t2 WHERE a = 'c';
460 SELECT a FROM t2 WHERE b = X'ABCD';
463 do_test alter2-10.4 {
465 SELECT count(b) FROM t2 WHERE b = X'ABCD';