Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/alter4.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / alter4.test` 488 lines ยท 11492 bytes Raw Blame History 1 # 2009 February 2 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: alter4.test,v 1.1 2009/02/02 18:03:22 drh Exp $ 17 # 18 19 set testdir [file dirname $argv0] 20 21 source $testdir/tester.tcl 22 set testprefix alter4 23 24 # If SQLITE_OMIT_ALTERTABLE is defined, omit this file. 25 ifcapable !altertable { 26 finish_test 27 return 28 } 29 30 31 # Test Organisation: 32 # ------------------ 33 # 34 # 1.*: Test that ALTER TABLE correctly modifies the CREATE TABLE sql. 35 # 2.*: Test error messages. 36 # 3.*: Test adding columns with default value NULL. 37 # 4.*: Test adding columns with default values other than NULL. 38 # 5.*: Test adding columns to tables in ATTACHed databases. 39 # 6.*: Test that temp triggers are not accidentally dropped. 40 # 7.*: Test that VACUUM resets the file-format. 41 # 42 43 do_test 1.1 { 44 execsql { 45 CREATE TEMP TABLE abc(a, b, c); 46 SELECT sql FROM sqlite_temp_master; 47 } 48 } {{CREATE TABLE abc(a, b, c)}} 49 do_test 1.1b { 50 execsql { 51 SELECT sql FROM temp.sqlite_master; 52 } 53 } {{CREATE TABLE abc(a, b, c)}} 54 do_test 1.2 { 55 execsql {ALTER TABLE abc ADD d INTEGER;} 56 execsql { 57 SELECT sql FROM sqlite_temp_master; 58 } 59 } {{CREATE TABLE abc(a, b, c, d INTEGER)}} 60 do_test 1.2b { 61 execsql { 62 SELECT sql FROM temp.sqlite_master; 63 } 64 } {{CREATE TABLE abc(a, b, c, d INTEGER)}} 65 do_test 1.3 { 66 execsql {ALTER TABLE abc ADD e} 67 execsql { 68 SELECT sql FROM sqlite_temp_master; 69 } 70 } {{CREATE TABLE abc(a, b, c, d INTEGER, e)}} 71 do_test 1.3b { 72 execsql { 73 SELECT sql FROM temp.sqlite_master; 74 } 75 } {{CREATE TABLE abc(a, b, c, d INTEGER, e)}} 76 do_test 1.4 { 77 execsql { 78 CREATE TABLE temp.t1(a, b); 79 ALTER TABLE t1 ADD c; 80 SELECT sql FROM sqlite_temp_master WHERE tbl_name = 't1'; 81 } 82 } {{CREATE TABLE t1(a, b, c)}} 83 do_test 1.4b { 84 execsql { 85 SELECT sql FROM temp.sqlite_master WHERE tbl_name = 't1'; 86 } 87 } {{CREATE TABLE t1(a, b, c)}} 88 do_test 1.5 { 89 execsql { 90 ALTER TABLE t1 ADD d CHECK (a>d); 91 SELECT sql FROM sqlite_temp_master WHERE tbl_name = 't1'; 92 } 93 } {{CREATE TABLE t1(a, b, c, d CHECK (a>d))}} 94 ifcapable foreignkey { 95 do_test 1.6 { 96 execsql { 97 CREATE TEMP TABLE t2(a, b, UNIQUE(a, b)); 98 ALTER TABLE t2 ADD c REFERENCES t1(c) ; 99 SELECT sql FROM sqlite_temp_master 100 WHERE tbl_name = 't2' AND type = 'table'; 101 } 102 } {{CREATE TABLE t2(a, b, c REFERENCES t1(c), UNIQUE(a, b))}} 103 } 104 do_test 1.7 { 105 execsql { 106 CREATE TEMPORARY TABLE t3(a, b, UNIQUE(a, b)); 107 ALTER TABLE t3 ADD COLUMN c VARCHAR(10, 20); 108 SELECT sql FROM sqlite_temp_master 109 WHERE tbl_name = 't3' AND type = 'table'; 110 } 111 } {{CREATE TABLE t3(a, b, c VARCHAR(10, 20), UNIQUE(a, b))}} 112 do_test 1.99 { 113 catchsql { 114 # May not exist if foreign-keys are omitted at compile time. 115 DROP TABLE t2; 116 } 117 execsql { 118 DROP TABLE abc; 119 DROP TABLE t1; 120 DROP TABLE t3; 121 } 122 } {} 123 124 do_test 2.1 { 125 execsql { 126 CREATE TABLE temp.t1(a, b); 127 INSERT INTO t1 VALUES(1,2); 128 } 129 catchsql { 130 ALTER TABLE t1 ADD c PRIMARY KEY; 131 } 132 } {1 {Cannot add a PRIMARY KEY column}} 133 do_test 2.2 { 134 catchsql { 135 ALTER TABLE t1 ADD c UNIQUE 136 } 137 } {1 {Cannot add a UNIQUE column}} 138 do_test 2.3 { 139 catchsql { 140 ALTER TABLE t1 ADD b VARCHAR(10) 141 } 142 } {1 {duplicate column name: b}} 143 do_test 2.3 { 144 catchsql { 145 ALTER TABLE t1 ADD c NOT NULL; 146 } 147 } {1 {Cannot add a NOT NULL column with default value NULL}} 148 do_test 2.4 { 149 catchsql { 150 ALTER TABLE t1 ADD c NOT NULL DEFAULT 10; 151 } 152 } {0 {}} 153 ifcapable view { 154 do_test 2.5 { 155 execsql { 156 CREATE TEMPORARY VIEW v1 AS SELECT * FROM t1; 157 } 158 catchsql { 159 alter table v1 add column d; 160 } 161 } {1 {Cannot add a column to a view}} 162 } 163 do_test 2.6 { 164 catchsql { 165 alter table t1 add column d DEFAULT CURRENT_TIME; 166 } 167 } {1 {Cannot add a column with non-constant default}} 168 do_test 2.7 { 169 catchsql { 170 alter table t1 add column d default (-5+1); 171 } 172 } {1 {Cannot add a column with non-constant default}} 173 do_test 2.99 { 174 execsql { 175 DROP TABLE t1; 176 } 177 } {} 178 179 do_test 3.1 { 180 execsql { 181 CREATE TEMP TABLE t1(a, b); 182 INSERT INTO t1 VALUES(1, 100); 183 INSERT INTO t1 VALUES(2, 300); 184 SELECT * FROM t1; 185 } 186 } {1 100 2 300} 187 do_test 3.1 { 188 execsql { 189 PRAGMA schema_version = 10; 190 } 191 } {} 192 do_test 3.2 { 193 execsql { 194 ALTER TABLE t1 ADD c; 195 SELECT * FROM t1; 196 } 197 } {1 100 {} 2 300 {}} 198 ifcapable schema_version { 199 do_test 3.4 { 200 execsql { 201 PRAGMA schema_version; 202 } 203 } {10} 204 } 205 206 do_test 4.1 { 207 db close 208 forcedelete test.db 209 set ::DB [sqlite3 db test.db] 210 execsql { 211 CREATE TEMP TABLE t1(a, b); 212 INSERT INTO t1 VALUES(1, 100); 213 INSERT INTO t1 VALUES(2, 300); 214 SELECT * FROM t1; 215 } 216 } {1 100 2 300} 217 do_test 4.1 { 218 execsql { 219 PRAGMA schema_version = 20; 220 } 221 } {} 222 do_test 4.2 { 223 execsql { 224 ALTER TABLE t1 ADD c DEFAULT 'hello world'; 225 SELECT * FROM t1; 226 } 227 } {1 100 {hello world} 2 300 {hello world}} 228 ifcapable schema_version { 229 do_test 4.4 { 230 execsql { 231 PRAGMA schema_version; 232 } 233 } {20} 234 } 235 do_test 4.99 { 236 execsql { 237 DROP TABLE t1; 238 } 239 } {} 240 241 ifcapable attach { 242 do_test 5.1 { 243 forcedelete test2.db 244 forcedelete test2.db-journal 245 execsql { 246 CREATE TEMP TABLE t1(a, b); 247 INSERT INTO t1 VALUES(1, 'one'); 248 INSERT INTO t1 VALUES(2, 'two'); 249 ATTACH 'test2.db' AS aux; 250 CREATE TABLE aux.t1 AS SELECT * FROM t1; 251 PRAGMA aux.schema_version = 30; 252 SELECT sql FROM aux.sqlite_master; 253 } 254 } {{CREATE TABLE t1(a,b)}} 255 do_test 5.2 { 256 execsql { 257 ALTER TABLE aux.t1 ADD COLUMN c VARCHAR(128); 258 SELECT sql FROM aux.sqlite_master; 259 } 260 } {{CREATE TABLE t1(a,b, c VARCHAR(128))}} 261 do_test 5.3 { 262 execsql { 263 SELECT * FROM aux.t1; 264 } 265 } {1 one {} 2 two {}} 266 ifcapable schema_version { 267 do_test 5.4 { 268 execsql { 269 PRAGMA aux.schema_version; 270 } 271 } {31} 272 } 273 do_test 5.6 { 274 execsql { 275 ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000; 276 SELECT sql FROM aux.sqlite_master; 277 } 278 } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}} 279 do_test 5.7 { 280 execsql { 281 SELECT * FROM aux.t1; 282 } 283 } {1 one {} 1000 2 two {} 1000} 284 ifcapable schema_version { 285 do_test 5.8 { 286 execsql { 287 PRAGMA aux.schema_version; 288 } 289 } {32} 290 } 291 do_test 5.9 { 292 execsql { 293 SELECT * FROM t1; 294 } 295 } {1 one 2 two} 296 do_test 5.99 { 297 execsql { 298 DROP TABLE aux.t1; 299 DROP TABLE t1; 300 } 301 } {} 302 } 303 304 #---------------------------------------------------------------- 305 # Test that the table schema is correctly reloaded when a column 306 # is added to a table. 307 # 308 ifcapable trigger&&tempdb { 309 do_test 6.1 { 310 execsql { 311 CREATE TEMP TABLE t1(a, b); 312 CREATE TEMP TABLE log(trig, a, b); 313 314 CREATE TRIGGER t1_a AFTER INSERT ON t1 BEGIN 315 INSERT INTO log VALUES('a', new.a, new.b); 316 END; 317 CREATE TEMP TRIGGER t1_b AFTER INSERT ON t1 BEGIN 318 INSERT INTO log VALUES('b', new.a, new.b); 319 END; 320 321 INSERT INTO t1 VALUES(1, 2); 322 SELECT * FROM log ORDER BY trig, a, b; 323 } 324 } {a 1 2 b 1 2} 325 do_test 6.2 { 326 execsql { 327 ALTER TABLE t1 ADD COLUMN c DEFAULT 'c'; 328 INSERT INTO t1(a, b) VALUES(3, 4); 329 SELECT * FROM log ORDER BY trig, a, b; 330 } 331 } {a 1 2 a 3 4 b 1 2 b 3 4} 332 } 333 334 # Ticket #1183 - Make sure adding columns to large tables does not cause 335 # memory corruption (as was the case before this bug was fixed). 336 do_test 8.1 { 337 execsql { 338 CREATE TEMP TABLE t4(c1); 339 } 340 } {} 341 set ::sql "" 342 do_test 8.2 { 343 set cols c1 344 for {set i 2} {$i < 100} {incr i} { 345 execsql " 346 ALTER TABLE t4 ADD c$i 347 " 348 lappend cols c$i 349 } 350 set ::sql "CREATE TABLE t4([join $cols {, }])" 351 list 352 } {} 353 do_test 8.2 { 354 execsql { 355 SELECT sql FROM sqlite_temp_master WHERE name = 't4'; 356 } 357 } [list $::sql] 358 359 360 # Test that a default value equal to -1 multipied by the smallest possible 361 # 64-bit integer is correctly converted to a real. 362 do_execsql_test 9.1 { 363 CREATE TABLE t5( 364 a INTEGER DEFAULT -`9223372036854775808`, 365 b INTEGER DEFAULT (-(-`9223372036854775808`)) 366 ); 367 INSERT INTO t5 DEFAULT VALUES; 368 } 369 370 do_execsql_test 9.2 { SELECT typeof(a), a, typeof(b), b FROM t5; } { 371 integer -`9223372036854775808` 372 real 9.`22337203685478e`+18 373 } 374 375 do_execsql_test 9.3 { 376 ALTER TABLE t5 ADD COLUMN c INTEGER DEFAULT (-(-`9223372036854775808`)); 377 SELECT typeof(c), c FROM t5; 378 } {real 9.`22337203685478e`+18} 379 380 # Confirm that doing an ALTER TABLE on a legacy format database 381 # does not corrupt DESC indexes. 382 # 383 # Ticket https://sqlite.org/src/tktview/`f68bf68513a1c` 384 # 385 do_test 10.1 { 386 db close 387 sqlite3 db :memory: 388 sqlite3_db_config db LEGACY_FILE_FORMAT 1 389 db eval { 390 CREATE TABLE t1(a,b,c); 391 CREATE INDEX t1a ON t1(a DESC); 392 INSERT INTO t1 VALUES(1,2,3); 393 INSERT INTO t1 VALUES(2,3,4); 394 ALTER TABLE t1 ADD COLUMN d; 395 PRAGMA integrity_check; 396 } 397 } {ok} 398 399 reset_db 400 do_execsql_test 11.0 { 401 CREATE TABLE t1(c INTEGER PRIMARY KEY, d); 402 INSERT INTO t1(c,d) VALUES(1,2); 403 PRAGMA foreign_keys = on; 404 ALTER TABLE t1 ADD COLUMN e; 405 } 406 407 do_execsql_test 11.1 { 408 ALTER TABLE t1 ADD COLUMN f REFERENCES t1; 409 } 410 411 do_catchsql_test 11.2 { 412 ALTER TABLE t1 ADD COLUMN g REFERENCES t1 DEFAULT 4; 413 } {1 {Cannot add a REFERENCES column with non-NULL default value}} 414 415 do_catchsql_test 11.3 { 416 ALTER TABLE t2 ADD COLUMN g; 417 } {1 {no such table: t2}} 418 419 ifcapable fts5 { 420 do_execsql_test 11.4 { 421 CREATE VIRTUAL TABLE fff USING fts5(f); 422 } 423 do_catchsql_test 11.2 { 424 ALTER TABLE fff ADD COLUMN g; 425 } {1 {virtual tables may not be altered}} 426 } 427 428 # Bug 2026-10-20T03:23:05Z 429 # 430 reset_db 431 do_execsql_test 12.0 { 432 CREATE TABLE inv(sku TEXT PRIMARY KEY, qty INTEGER); 433 CREATE TABLE incoming(sku TEXT, qty INTEGER); 434 CREATE TRIGGER add_stock AFTER INSERT ON incoming BEGIN 435 INSERT INTO inv(sku, qty) VALUES (new.sku, new.qty) 436 ON CONFLICT(sku) DO UPDATE SET qty=qty+excluded.qty WHERE excluded.qty>0; 437 END; 438 INSERT INTO inv VALUES('A',100),('B',7); 439 BEGIN; 440 INSERT INTO incoming VALUES ('A', 5); 441 SELECT sku, qty FROM inv; 442 ROLLBACK; 443 } {A 105 B 7} 444 do_execsql_test 12.1 { 445 ALTER TABLE inv RENAME TO stock; 446 SELECT sql GLOB '*SET qty=qty+excluded.qty WHERE*' 447 FROM sqlite_schema WHERE name = 'add_stock'; 448 } 1 449 do_execsql_test 12.2 { 450 INSERT INTO incoming VALUES ('A', 5), ('B', -3); 451 SELECT sku, qty FROM stock; 452 } {A 105 B 7} 453 454 #------------------------------------------------------------------------- 455 reset_db 456 457 do_execsql_test 12.1 { 458 CREATE TABLE t1(x); 459 INSERT INTO t1(x) VALUES(1), (2); 460 ALTER TABLE t1 ADD COLUMN y TEXT DEFAULT 1e2; 461 INSERT INTO t1(x) VALUES(3), (4); 462 } 463 464 do_execsql_test 12.2 { 465 SELECT DISTINCT y FROM t1 466 } {100.0} 467 468 do_execsql_test 12.3 { 469 SELECT x, quote(y) FROM t1 470 } {1 '100.0' 2 '100.0' 3 '100.0' 4 '100.0'} 471 472 do_execsql_test 13.1 { 473 CREATE TABLE t2(x); 474 INSERT INTO t2(x) VALUES(1), (2); 475 ALTER TABLE t2 ADD COLUMN y TEXT DEFAULT 0x64; 476 INSERT INTO t2(x) VALUES(3), (4); 477 } 478 479 do_execsql_test 13.2 { 480 SELECT DISTINCT y FROM t2 481 } {100} 482 483 do_execsql_test 13.3 { 484 SELECT x, quote(y) FROM t2 485 } {1 '100' 2 '100' 3 '100' 4 '100'} 486 487 finish_test 488