1# 2005 February 18 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 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. 9# 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". 15# 16# $Id: alter2.test,v 1.14 2009/04/07 14:14:22 danielk1977 Exp $ 17# 18 19set testdir [file dirname $argv0] 20source $testdir/tester.tcl 21 22# We have to have pragmas in order to do this test 23ifcapable {!pragma} return 24 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]. 27# 28do_not_use_codec 29 30# These tests do not work if there is a codec. 31# 32#if {[catch {sqlite3 -has_codec} r] || $r} return 33# 34 35# The file format change affects the way row-records stored in tables (but 36# not indices) are interpreted. Before version 3.1.3, a row-record for a 37# table with N columns was guaranteed to contain exactly N fields. As 38# of version 3.1.3, the record may contain up to N fields. In this case 39# the M fields that are present are the values for the left-most M 40# columns. The (N-M) rightmost columns contain NULL. 41# 42# If any records in the database contain less fields than their table 43# has columns, then the file-format meta value should be set to (at least) 2. 44# 45 46# This procedure sets the value of the file-format in file 'test.db' 47# to $newval. Also, the schema cookie is incremented. 48# 49proc set_file_format {newval} { 50 hexio_write test.db 44 [hexio_render_int32 $newval] 51 set schemacookie [hexio_get_int [hexio_read test.db 40 4]] 52 incr schemacookie 53 hexio_write test.db 40 [hexio_render_int32 $schemacookie] 54 return {} 55} 56 57# This procedure returns the value of the file-format in file 'test.db'. 58# 59proc get_file_format {{fname test.db}} { 60 return [hexio_get_int [hexio_read $fname 44 4]] 61} 62 63# This procedure sets the SQL statement stored for table $tbl in the 64# sqlite_master table of file 'test.db' to $sql. Also set the file format 65# to the supplied value. This is 2 if the added column has a default that is 66# NULL, or 3 otherwise. 67# 68proc alter_table {tbl sql {file_format 2}} { 69 sqlite3 dbat test.db 70 set s [string map {' ''} $sql] 71 set t [string map {' ''} $tbl] 72 dbat eval [subst { 73 PRAGMA writable_schema = 1; 74 UPDATE sqlite_master SET sql = '$s' WHERE name = '$t' AND type = 'table'; 75 PRAGMA writable_schema = 0; 76 }] 77 dbat close 78 set_file_format 2 79} 80 81# Create bogus application-defined functions for functions used 82# internally by ALTER TABLE, to ensure that ALTER TABLE falls back 83# to the built-in functions. 84# 85proc failing_app_func {args} {error "bad function"} 86do_test alter2-1.0 { 87 db func substr failing_app_func 88 db func like failing_app_func 89 db func sqlite_rename_table failing_app_func 90 db func sqlite_rename_trigger failing_app_func 91 db func sqlite_rename_parent failing_app_func 92 catchsql {SELECT substr('abcdefg',1,3)} 93} {1 {bad function}} 94 95 96#----------------------------------------------------------------------- 97# Some basic tests to make sure short rows are handled. 98# 99do_test alter2-1.1 { 100 execsql { 101 CREATE TABLE abc(a, b); 102 INSERT INTO abc VALUES(1, 2); 103 INSERT INTO abc VALUES(3, 4); 104 INSERT INTO abc VALUES(5, 6); 105 } 106} {} 107do_test alter2-1.2 { 108 # ALTER TABLE abc ADD COLUMN c; 109 alter_table abc {CREATE TABLE abc(a, b, c);} 110} {} 111do_test alter2-1.3 { 112 execsql { 113 SELECT * FROM abc; 114 } 115} {1 2 {} 3 4 {} 5 6 {}} 116do_test alter2-1.4 { 117 execsql { 118 UPDATE abc SET c = 10 WHERE a = 1; 119 SELECT * FROM abc; 120 } 121} {1 2 10 3 4 {} 5 6 {}} 122do_test alter2-1.5 { 123 execsql { 124 CREATE INDEX abc_i ON abc(c); 125 } 126} {} 127do_test alter2-1.6 { 128 execsql { 129 SELECT c FROM abc ORDER BY c; 130 } 131} {{} {} 10} 132do_test alter2-1.7 { 133 execsql { 134 SELECT * FROM abc WHERE c = 10; 135 } 136} {1 2 10} 137do_test alter2-1.8 { 138 execsql { 139 SELECT sum(a), c FROM abc GROUP BY c; 140 } 141} {8 {} 1 10} 142do_test alter2-1.9 { 143 # ALTER TABLE abc ADD COLUMN d; 144 alter_table abc {CREATE TABLE abc(a, b, c, d);} 145 execsql { SELECT * FROM abc; } 146 execsql { 147 UPDATE abc SET d = 11 WHERE c IS NULL AND a<4; 148 SELECT * FROM abc; 149 } 150} {1 2 10 {} 3 4 {} 11 5 6 {} {}} 151do_test alter2-1.10 { 152 execsql { 153 SELECT typeof(d) FROM abc; 154 } 155} {null integer null} 156do_test alter2-1.99 { 157 execsql { 158 DROP TABLE abc; 159 } 160} {} 161 162#----------------------------------------------------------------------- 163# Test that views work when the underlying table structure is changed. 164# 165ifcapable view { 166 do_test alter2-2.1 { 167 execsql { 168 CREATE TABLE abc2(a, b, c); 169 INSERT INTO abc2 VALUES(1, 2, 10); 170 INSERT INTO abc2 VALUES(3, 4, NULL); 171 INSERT INTO abc2 VALUES(5, 6, NULL); 172 CREATE VIEW abc2_v AS SELECT * FROM abc2; 173 SELECT * FROM abc2_v; 174 } 175 } {1 2 10 3 4 {} 5 6 {}} 176 do_test alter2-2.2 { 177 # ALTER TABLE abc ADD COLUMN d; 178 alter_table abc2 {CREATE TABLE abc2(a, b, c, d);} 179 execsql { 180 SELECT * FROM abc2_v; 181 } 182 } {1 2 10 {} 3 4 {} {} 5 6 {} {}} 183 do_test alter2-2.3 { 184 execsql { 185 DROP TABLE abc2; 186 DROP VIEW abc2_v; 187 } 188 } {} 189} 190 191#----------------------------------------------------------------------- 192# Test that triggers work when a short row is copied to the old.* 193# trigger pseudo-table. 194# 195ifcapable trigger { 196 do_test alter2-3.1 { 197 execsql { 198 CREATE TABLE abc3(a, b); 199 CREATE TABLE blog(o, n); 200 CREATE TRIGGER abc3_t AFTER UPDATE OF b ON abc3 BEGIN 201 INSERT INTO blog VALUES(old.b, new.b); 202 END; 203 } 204 } {} 205 do_test alter2-3.2 { 206 execsql { 207 INSERT INTO abc3 VALUES(1, 4); 208 UPDATE abc3 SET b = 2 WHERE b = 4; 209 SELECT * FROM blog; 210 } 211 } {4 2} 212 do_test alter2-3.3 { 213 execsql { 214 INSERT INTO abc3 VALUES(3, 4); 215 INSERT INTO abc3 VALUES(5, 6); 216 } 217 alter_table abc3 {CREATE TABLE abc3(a, b, c);} 218 execsql { 219 SELECT * FROM abc3; 220 } 221 } {1 2 {} 3 4 {} 5 6 {}} 222 do_test alter2-3.4 { 223 execsql { 224 UPDATE abc3 SET b = b*2 WHERE a<4; 225 SELECT * FROM abc3; 226 } 227 } {1 4 {} 3 8 {} 5 6 {}} 228 do_test alter2-3.5 { 229 execsql { 230 SELECT * FROM blog; 231 } 232 } {4 2 2 4 4 8} 233 234 do_test alter2-3.6 { 235 execsql { 236 CREATE TABLE clog(o, n); 237 CREATE TRIGGER abc3_t2 AFTER UPDATE OF c ON abc3 BEGIN 238 INSERT INTO clog VALUES(old.c, new.c); 239 END; 240 UPDATE abc3 SET c = a*2; 241 SELECT * FROM clog; 242 } 243 } {{} 2 {} 6 {} 10} 244} else { 245 execsql { CREATE TABLE abc3(a, b); } 246} 247 248#--------------------------------------------------------------------- 249# Check that an error occurs if the database is upgraded to a file 250# format that SQLite does not support (in this case 5). Note: The 251# file format is checked each time the schema is read, so changing the 252# file format requires incrementing the schema cookie. 253# 254do_test alter2-4.1 { 255 db close 256 set_file_format 5 257 catch { sqlite3 db test.db } 258 set {} {} 259} {} 260do_test alter2-4.2 { 261 # We have to run two queries here because the Tcl interface uses 262 # sqlite3_prepare_v2(). In this case, the first query encounters an 263 # SQLITE_SCHEMA error. Then, when trying to recompile the statement, the 264 # "unsupported file format" error is encountered. So the error code 265 # returned is SQLITE_SCHEMA, not SQLITE_ERROR as required by the following 266 # test case. 267 # 268 # When the query is attempted a second time, the same error message is 269 # returned but the error code is SQLITE_ERROR, because the unsupported 270 # file format was detected during a call to sqlite3_prepare(), not 271 # sqlite3_step(). 272 # 273 catchsql { SELECT * FROM sqlite_master; } 274 catchsql { SELECT * FROM sqlite_master; } 275} {1 {unsupported file format}} 276do_test alter2-4.3 { 277 sqlite3_errcode db 278} {SQLITE_ERROR} 279do_test alter2-4.4 { 280 set ::DB [sqlite3_connection_pointer db] 281 catchsql { 282 SELECT * FROM sqlite_master; 283 } 284} {1 {unsupported file format}} 285do_test alter2-4.5 { 286 sqlite3_errcode db 287} {SQLITE_ERROR} 288 289#--------------------------------------------------------------------- 290# Check that executing VACUUM on a file with file-format version 2 291# resets the file format to 1. 292# 293set default_file_format [expr $SQLITE_DEFAULT_FILE_FORMAT==4 ? 4 : 1] 294ifcapable vacuum { 295 do_test alter2-5.1 { 296 set_file_format 2 297 db close 298 sqlite3 db test.db 299 execsql {SELECT 1 FROM sqlite_master LIMIT 1;} 300 get_file_format 301 } {2} 302 do_test alter2-5.2 { 303 execsql { VACUUM } 304 } {} 305 do_test alter2-5.3 { 306 get_file_format 307 } $default_file_format 308} 309 310#--------------------------------------------------------------------- 311# Test that when a database with file-format 2 is opened, new 312# databases are still created with file-format 1. 313# 314do_test alter2-6.1 { 315 db close 316 set_file_format 2 317 sqlite3 db test.db 318 get_file_format 319} {2} 320ifcapable attach { 321 do_test alter2-6.2 { 322 file delete -force test2.db-journal 323 file delete -force test2.db 324 execsql { 325 ATTACH 'test2.db' AS aux; 326 CREATE TABLE aux.t1(a, b); 327 } 328 get_file_format test2.db 329 } $default_file_format 330} 331do_test alter2-6.3 { 332 execsql { 333 CREATE TABLE t1(a, b); 334 } 335 get_file_format 336} {2} 337 338#--------------------------------------------------------------------- 339# Test that types and values for columns added with default values 340# other than NULL work with SELECT statements. 341# 342do_test alter2-7.1 { 343 execsql { 344 DROP TABLE t1; 345 CREATE TABLE t1(a); 346 INSERT INTO t1 VALUES(1); 347 INSERT INTO t1 VALUES(2); 348 INSERT INTO t1 VALUES(3); 349 INSERT INTO t1 VALUES(4); 350 SELECT * FROM t1; 351 } 352} {1 2 3 4} 353do_test alter2-7.2 { 354 set sql {CREATE TABLE t1(a, b DEFAULT '123', c INTEGER DEFAULT '123')} 355 alter_table t1 $sql 3 356 execsql { 357 SELECT * FROM t1 LIMIT 1; 358 } 359} {1 123 123} 360do_test alter2-7.3 { 361 execsql { 362 SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1; 363 } 364} {1 integer 123 text 123 integer} 365do_test alter2-7.4 { 366 execsql { 367 SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1; 368 } 369} {1 integer 123 text 123 integer} 370do_test alter2-7.5 { 371 set sql {CREATE TABLE t1(a, b DEFAULT -123.0, c VARCHAR(10) default 5)} 372 alter_table t1 $sql 3 373 execsql { 374 SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1; 375 } 376} {1 integer -123 integer 5 text} 377 378#----------------------------------------------------------------------- 379# Test that UPDATE trigger tables work with default values, and that when 380# a row is updated the default values are correctly transfered to the 381# new row. 382# 383ifcapable trigger { 384db function set_val {set ::val} 385 do_test alter2-8.1 { 386 execsql { 387 CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN 388 SELECT set_val( 389 old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)||' '|| 390 new.b||' '||typeof(new.b)||' '||new.c||' '||typeof(new.c) 391 ); 392 END; 393 } 394 list 395 } {} 396} 397do_test alter2-8.2 { 398 execsql { 399 UPDATE t1 SET c = 10 WHERE a = 1; 400 SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1; 401 } 402} {1 integer -123 integer 10 text} 403ifcapable trigger { 404 do_test alter2-8.3 { 405 set ::val 406 } {-123 integer 5 text -123 integer 10 text} 407} 408 409#----------------------------------------------------------------------- 410# Test that DELETE trigger tables work with default values, and that when 411# a row is updated the default values are correctly transfered to the 412# new row. 413# 414ifcapable trigger { 415 do_test alter2-9.1 { 416 execsql { 417 CREATE TRIGGER trig2 BEFORE DELETE ON t1 BEGIN 418 SELECT set_val( 419 old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c) 420 ); 421 END; 422 } 423 list 424 } {} 425 do_test alter2-9.2 { 426 execsql { 427 DELETE FROM t1 WHERE a = 2; 428 } 429 set ::val 430 } {-123 integer 5 text} 431} 432 433#----------------------------------------------------------------------- 434# Test creating an index on a column added with a default value. 435# 436ifcapable bloblit { 437 do_test alter2-10.1 { 438 execsql { 439 CREATE TABLE t2(a); 440 INSERT INTO t2 VALUES('a'); 441 INSERT INTO t2 VALUES('b'); 442 INSERT INTO t2 VALUES('c'); 443 INSERT INTO t2 VALUES('d'); 444 } 445 alter_table t2 {CREATE TABLE t2(a, b DEFAULT X'ABCD', c DEFAULT NULL);} 3 446 catchsql { 447 SELECT * FROM sqlite_master; 448 } 449 execsql { 450 SELECT quote(a), quote(b), quote(c) FROM t2 LIMIT 1; 451 } 452 } {'a' X'ABCD' NULL} 453 do_test alter2-10.2 { 454 execsql { 455 CREATE INDEX i1 ON t2(b); 456 SELECT a FROM t2 WHERE b = X'ABCD'; 457 } 458 } {a b c d} 459 do_test alter2-10.3 { 460 execsql { 461 DELETE FROM t2 WHERE a = 'c'; 462 SELECT a FROM t2 WHERE b = X'ABCD'; 463 } 464 } {a b d} 465 do_test alter2-10.4 { 466 execsql { 467 SELECT count(b) FROM t2 WHERE b = X'ABCD'; 468 } 469 } {3} 470} 471 472finish_test 473