| OLD | NEW |
| 1 # 2009 February 2 | 1 # 2009 February 2 |
| 2 # | 2 # |
| 3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
| 4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
| 5 # | 5 # |
| 6 # May you do good and not evil. | 6 # May you do good and not evil. |
| 7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
| 8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
| 9 # | 9 # |
| 10 #************************************************************************* | 10 #************************************************************************* |
| 11 # This file implements regression tests for SQLite library. The | 11 # This file implements regression tests for SQLite library. The |
| 12 # focus of this script is testing that SQLite can handle a subtle | 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 | 13 # file format change that may be used in the future to implement |
| 14 # "ALTER TABLE ... ADD COLUMN". | 14 # "ALTER TABLE ... ADD COLUMN". |
| 15 # | 15 # |
| 16 # $Id: alter4.test,v 1.1 2009/02/02 18:03:22 drh Exp $ | 16 # $Id: alter4.test,v 1.1 2009/02/02 18:03:22 drh Exp $ |
| 17 # | 17 # |
| 18 | 18 |
| 19 set testdir [file dirname $argv0] | 19 set testdir [file dirname $argv0] |
| 20 | 20 |
| 21 source $testdir/tester.tcl | 21 source $testdir/tester.tcl |
| 22 | 22 |
| 23 # If SQLITE_OMIT_ALTERTABLE is defined, omit this file. | 23 # If SQLITE_OMIT_ALTERTABLE is defined, omit this file. |
| 24 ifcapable !altertable { | 24 ifcapable !altertable { |
| 25 finish_test | 25 finish_test |
| 26 return | 26 return |
| 27 } | 27 } |
| 28 | 28 |
| 29 # Determine if there is a codec available on this test. | |
| 30 # | |
| 31 if {[catch {sqlite3 -has_codec} r] || $r} { | |
| 32 set has_codec 1 | |
| 33 } else { | |
| 34 set has_codec 0 | |
| 35 } | |
| 36 | |
| 37 | 29 |
| 38 # Test Organisation: | 30 # Test Organisation: |
| 39 # ------------------ | 31 # ------------------ |
| 40 # | 32 # |
| 41 # alter4-1.*: Test that ALTER TABLE correctly modifies the CREATE TABLE sql. | 33 # alter4-1.*: Test that ALTER TABLE correctly modifies the CREATE TABLE sql. |
| 42 # alter4-2.*: Test error messages. | 34 # alter4-2.*: Test error messages. |
| 43 # alter4-3.*: Test adding columns with default value NULL. | 35 # alter4-3.*: Test adding columns with default value NULL. |
| 44 # alter4-4.*: Test adding columns with default values other than NULL. | 36 # alter4-4.*: Test adding columns with default values other than NULL. |
| 45 # alter4-5.*: Test adding columns to tables in ATTACHed databases. | 37 # alter4-5.*: Test adding columns to tables in ATTACHed databases. |
| 46 # alter4-6.*: Test that temp triggers are not accidentally dropped. | 38 # alter4-6.*: Test that temp triggers are not accidentally dropped. |
| 47 # alter4-7.*: Test that VACUUM resets the file-format. | 39 # alter4-7.*: Test that VACUUM resets the file-format. |
| 48 # | 40 # |
| 49 | 41 |
| 50 # This procedure returns the value of the file-format in file 'test.db'. | |
| 51 # | |
| 52 proc get_file_format {{fname test.db}} { | |
| 53 return [hexio_get_int [hexio_read $fname 44 4]] | |
| 54 } | |
| 55 | |
| 56 do_test alter4-1.1 { | 42 do_test alter4-1.1 { |
| 57 execsql { | 43 execsql { |
| 58 CREATE TEMP TABLE abc(a, b, c); | 44 CREATE TEMP TABLE abc(a, b, c); |
| 59 SELECT sql FROM sqlite_temp_master; | 45 SELECT sql FROM sqlite_temp_master; |
| 60 } | 46 } |
| 61 } {{CREATE TABLE abc(a, b, c)}} | 47 } {{CREATE TABLE abc(a, b, c)}} |
| 62 do_test alter4-1.2 { | 48 do_test alter4-1.2 { |
| 63 execsql {ALTER TABLE abc ADD d INTEGER;} | 49 execsql {ALTER TABLE abc ADD d INTEGER;} |
| 64 execsql { | 50 execsql { |
| 65 SELECT sql FROM sqlite_temp_master; | 51 SELECT sql FROM sqlite_temp_master; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 execsql { | 161 execsql { |
| 176 PRAGMA schema_version = 10; | 162 PRAGMA schema_version = 10; |
| 177 } | 163 } |
| 178 } {} | 164 } {} |
| 179 do_test alter4-3.2 { | 165 do_test alter4-3.2 { |
| 180 execsql { | 166 execsql { |
| 181 ALTER TABLE t1 ADD c; | 167 ALTER TABLE t1 ADD c; |
| 182 SELECT * FROM t1; | 168 SELECT * FROM t1; |
| 183 } | 169 } |
| 184 } {1 100 {} 2 300 {}} | 170 } {1 100 {} 2 300 {}} |
| 185 if {!$has_codec} { | |
| 186 do_test alter4-3.3 { | |
| 187 get_file_format | |
| 188 } {3} | |
| 189 } | |
| 190 ifcapable schema_version { | 171 ifcapable schema_version { |
| 191 do_test alter4-3.4 { | 172 do_test alter4-3.4 { |
| 192 execsql { | 173 execsql { |
| 193 PRAGMA schema_version; | 174 PRAGMA schema_version; |
| 194 } | 175 } |
| 195 } {10} | 176 } {10} |
| 196 } | 177 } |
| 197 | 178 |
| 198 do_test alter4-4.1 { | 179 do_test alter4-4.1 { |
| 199 db close | 180 db close |
| (...skipping 10 matching lines...) Expand all Loading... |
| 210 execsql { | 191 execsql { |
| 211 PRAGMA schema_version = 20; | 192 PRAGMA schema_version = 20; |
| 212 } | 193 } |
| 213 } {} | 194 } {} |
| 214 do_test alter4-4.2 { | 195 do_test alter4-4.2 { |
| 215 execsql { | 196 execsql { |
| 216 ALTER TABLE t1 ADD c DEFAULT 'hello world'; | 197 ALTER TABLE t1 ADD c DEFAULT 'hello world'; |
| 217 SELECT * FROM t1; | 198 SELECT * FROM t1; |
| 218 } | 199 } |
| 219 } {1 100 {hello world} 2 300 {hello world}} | 200 } {1 100 {hello world} 2 300 {hello world}} |
| 220 if {!$has_codec} { | |
| 221 do_test alter4-4.3 { | |
| 222 get_file_format | |
| 223 } {3} | |
| 224 } | |
| 225 ifcapable schema_version { | 201 ifcapable schema_version { |
| 226 do_test alter4-4.4 { | 202 do_test alter4-4.4 { |
| 227 execsql { | 203 execsql { |
| 228 PRAGMA schema_version; | 204 PRAGMA schema_version; |
| 229 } | 205 } |
| 230 } {20} | 206 } {20} |
| 231 } | 207 } |
| 232 do_test alter4-4.99 { | 208 do_test alter4-4.99 { |
| 233 execsql { | 209 execsql { |
| 234 DROP TABLE t1; | 210 DROP TABLE t1; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 260 SELECT * FROM aux.t1; | 236 SELECT * FROM aux.t1; |
| 261 } | 237 } |
| 262 } {1 one {} 2 two {}} | 238 } {1 one {} 2 two {}} |
| 263 ifcapable schema_version { | 239 ifcapable schema_version { |
| 264 do_test alter4-5.4 { | 240 do_test alter4-5.4 { |
| 265 execsql { | 241 execsql { |
| 266 PRAGMA aux.schema_version; | 242 PRAGMA aux.schema_version; |
| 267 } | 243 } |
| 268 } {31} | 244 } {31} |
| 269 } | 245 } |
| 270 if {!$has_codec} { | |
| 271 do_test alter4-5.5 { | |
| 272 list [get_file_format test2.db] [get_file_format] | |
| 273 } {2 3} | |
| 274 } | |
| 275 do_test alter4-5.6 { | 246 do_test alter4-5.6 { |
| 276 execsql { | 247 execsql { |
| 277 ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000; | 248 ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000; |
| 278 SELECT sql FROM aux.sqlite_master; | 249 SELECT sql FROM aux.sqlite_master; |
| 279 } | 250 } |
| 280 } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}} | 251 } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}} |
| 281 do_test alter4-5.7 { | 252 do_test alter4-5.7 { |
| 282 execsql { | 253 execsql { |
| 283 SELECT * FROM aux.t1; | 254 SELECT * FROM aux.t1; |
| 284 } | 255 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 } {b 1 2 a 1 2} | 297 } {b 1 2 a 1 2} |
| 327 do_test alter4-6.2 { | 298 do_test alter4-6.2 { |
| 328 execsql { | 299 execsql { |
| 329 ALTER TABLE t1 ADD COLUMN c DEFAULT 'c'; | 300 ALTER TABLE t1 ADD COLUMN c DEFAULT 'c'; |
| 330 INSERT INTO t1(a, b) VALUES(3, 4); | 301 INSERT INTO t1(a, b) VALUES(3, 4); |
| 331 SELECT * FROM log; | 302 SELECT * FROM log; |
| 332 } | 303 } |
| 333 } {b 1 2 a 1 2 b 3 4 a 3 4} | 304 } {b 1 2 a 1 2 b 3 4 a 3 4} |
| 334 } | 305 } |
| 335 | 306 |
| 336 if {!$has_codec} { | |
| 337 ifcapable vacuum { | |
| 338 do_test alter4-7.1 { | |
| 339 execsql { | |
| 340 VACUUM; | |
| 341 } | |
| 342 get_file_format | |
| 343 } {1} | |
| 344 do_test alter4-7.2 { | |
| 345 execsql { | |
| 346 CREATE TEMP TABLE abc(a, b, c); | |
| 347 ALTER TABLE abc ADD d DEFAULT NULL; | |
| 348 } | |
| 349 get_file_format | |
| 350 } {2} | |
| 351 do_test alter4-7.3 { | |
| 352 execsql { | |
| 353 ALTER TABLE abc ADD e DEFAULT 10; | |
| 354 } | |
| 355 get_file_format | |
| 356 } {3} | |
| 357 do_test alter4-7.4 { | |
| 358 execsql { | |
| 359 ALTER TABLE abc ADD f DEFAULT NULL; | |
| 360 } | |
| 361 get_file_format | |
| 362 } {3} | |
| 363 do_test alter4-7.5 { | |
| 364 execsql { | |
| 365 VACUUM; | |
| 366 } | |
| 367 get_file_format | |
| 368 } {1} | |
| 369 } | |
| 370 } | |
| 371 | |
| 372 # Ticket #1183 - Make sure adding columns to large tables does not cause | 307 # Ticket #1183 - Make sure adding columns to large tables does not cause |
| 373 # memory corruption (as was the case before this bug was fixed). | 308 # memory corruption (as was the case before this bug was fixed). |
| 374 do_test alter4-8.1 { | 309 do_test alter4-8.1 { |
| 375 execsql { | 310 execsql { |
| 376 CREATE TEMP TABLE t4(c1); | 311 CREATE TEMP TABLE t4(c1); |
| 377 } | 312 } |
| 378 } {} | 313 } {} |
| 379 set ::sql "" | 314 set ::sql "" |
| 380 do_test alter4-8.2 { | 315 do_test alter4-8.2 { |
| 381 set cols c1 | 316 set cols c1 |
| 382 for {set i 2} {$i < 100} {incr i} { | 317 for {set i 2} {$i < 100} {incr i} { |
| 383 execsql " | 318 execsql " |
| 384 ALTER TABLE t4 ADD c$i | 319 ALTER TABLE t4 ADD c$i |
| 385 " | 320 " |
| 386 lappend cols c$i | 321 lappend cols c$i |
| 387 } | 322 } |
| 388 set ::sql "CREATE TABLE t4([join $cols {, }])" | 323 set ::sql "CREATE TABLE t4([join $cols {, }])" |
| 389 list | 324 list |
| 390 } {} | 325 } {} |
| 391 do_test alter4-8.2 { | 326 do_test alter4-8.2 { |
| 392 execsql { | 327 execsql { |
| 393 SELECT sql FROM sqlite_temp_master WHERE name = 't4'; | 328 SELECT sql FROM sqlite_temp_master WHERE name = 't4'; |
| 394 } | 329 } |
| 395 } [list $::sql] | 330 } [list $::sql] |
| 396 | 331 |
| 397 finish_test | 332 finish_test |
| OLD | NEW |