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/ext/recover/recover1.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 / ext / recover / recover1.test` 494 lines ยท 12862 bytes Raw Blame History 1 # 2022 August 28 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 # 12 13 source [file join [file dirname [info script]] recover_common.tcl] 14 set testprefix recover1 15 16 proc compare_result {db1 db2 sql} { 17 set r1 [$db1 eval $sql] 18 set r2 [$db2 eval $sql] 19 if {$r1 != $r2} { 20 puts "r1: $r1" 21 puts "r2: $r2" 22 error "mismatch for $sql" 23 } 24 return "" 25 } 26 27 proc compare_dbs {db1 db2} { 28 compare_result $db1 $db2 "SELECT sql FROM sqlite_master ORDER BY 1" 29 foreach tbl [$db1 eval {SELECT name FROM sqlite_master WHERE type='table'}] { 30 compare_result $db1 $db2 "SELECT * FROM $tbl" 31 } 32 33 compare_result $db1 $db2 "PRAGMA page_size" 34 compare_result $db1 $db2 "PRAGMA auto_vacuum" 35 compare_result $db1 $db2 "PRAGMA encoding" 36 compare_result $db1 $db2 "PRAGMA user_version" 37 compare_result $db1 $db2 "PRAGMA application_id" 38 } 39 40 proc do_recover_test {tn} { 41 forcedelete test.db2 42 forcedelete rstate.db 43 44 uplevel [list do_test $tn.1 { 45 set R [sqlite3_recover_init db main test.db2] 46 $R config testdb rstate.db 47 $R run 48 $R finish 49 } {}] 50 51 sqlite3 db2 test.db2 52 uplevel [list do_test $tn.2 [list compare_dbs db db2] {}] 53 db2 close 54 55 forcedelete test.db2 56 forcedelete rstate.db 57 58 uplevel [list do_test $tn.3 { 59 set ::sqlhook [list] 60 set R [sqlite3_recover_init_sql db main my_sql_hook] 61 $R config testdb rstate.db 62 $R config rowids 1 63 $R run 64 $R finish 65 } {}] 66 67 sqlite3 db2 test.db2 68 execsql [join $::sqlhook ";"] db2 69 db2 close 70 sqlite3 db2 test.db2 71 uplevel [list do_test $tn.4 [list compare_dbs db db2] {}] 72 db2 close 73 } 74 75 proc my_sql_hook {sql} { 76 lappend ::sqlhook $sql 77 return 0 78 } 79 80 do_execsql_test 1.0 { 81 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); 82 CREATE TABLE t2(a INTEGER PRIMARY KEY, b) WITHOUT ROWID; 83 WITH s(i) AS ( 84 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10 85 ) 86 INSERT INTO t1 SELECT i*2, hex(randomblob(250)) FROM s; 87 INSERT INTO t2 SELECT * FROM t1; 88 } 89 90 do_recover_test 1 91 92 do_execsql_test 2.0 { 93 ALTER TABLE t1 ADD COLUMN c DEFAULT 'xyz' 94 } 95 do_recover_test 2 96 97 do_execsql_test 3.0 { 98 CREATE INDEX i1 ON t1(c); 99 } 100 do_recover_test 3 101 102 do_execsql_test 4.0 { 103 CREATE VIEW v1 AS SELECT * FROM t2; 104 } 105 do_recover_test 4 106 107 do_execsql_test 5.0 { 108 CREATE UNIQUE INDEX i2 ON t1(c, b); 109 } 110 do_recover_test 5 111 112 #-------------------------------------------------------------------------- 113 # 114 reset_db 115 do_execsql_test 6.0 { 116 CREATE TABLE t1( 117 a INTEGER PRIMARY KEY, 118 b INT, 119 c TEXT, 120 d INT GENERATED ALWAYS AS (a*abs(b)) VIRTUAL, 121 e TEXT GENERATED ALWAYS AS (substr(c,b,b+1)) STORED, 122 f TEXT GENERATED ALWAYS AS (substr(c,b,b+1)) STORED 123 ); 124 125 INSERT INTO t1 VALUES(1, 2, 'hello world'); 126 } 127 do_recover_test 6 128 129 do_execsql_test 7.0 { 130 CREATE TABLE t2(i, j GENERATED ALWAYS AS (i+1) STORED, k); 131 INSERT INTO t2 VALUES(10, 'ten'); 132 } 133 do_execsql_test 7.1 { 134 SELECT * FROM t2 135 } {10 11 ten} 136 137 do_recover_test 7.2 138 139 #-------------------------------------------------------------------------- 140 # 141 reset_db 142 do_execsql_test 8.0 { 143 CREATE TABLE x1(a INTEGER PRIMARY KEY AUTOINCREMENT, b, c); 144 WITH s(i) AS ( 145 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<2 146 ) 147 INSERT INTO x1(b, c) SELECT hex(randomblob(100)), hex(randomblob(100)) FROM s; 148 149 CREATE INDEX x1b ON x1(b); 150 CREATE INDEX x1cb ON x1(c, b); 151 DELETE FROM x1 WHERE a>50; 152 153 ANALYZE; 154 } 155 156 do_recover_test 8 157 158 #------------------------------------------------------------------------- 159 reset_db 160 ifcapable fts5 { 161 do_execsql_test 9.1 { 162 CREATE VIRTUAL TABLE ft5 USING fts5(a, b); 163 INSERT INTO ft5 VALUES('hello', 'world'); 164 } 165 do_recover_test 9 166 } 167 168 #------------------------------------------------------------------------- 169 reset_db 170 do_execsql_test 10.1 { 171 CREATE TABLE x1(a PRIMARY KEY, str TEXT) WITHOUT ROWID; 172 INSERT INTO x1 VALUES(1, ' 173 \nhello\012world(\n0)(\n1) 174 '); 175 INSERT INTO x1 VALUES(2, ' 176 \nhello 177 '); 178 } 179 do_execsql_test 10.2 " 180 INSERT INTO x1 VALUES(3, '\012hello there\015world'); 181 INSERT INTO x1 VALUES(4, '\015hello there\015world'); 182 " 183 do_recover_test 10 184 185 #------------------------------------------------------------------------- 186 reset_db 187 do_execsql_test 11.1 { 188 PRAGMA page_size = 4096; 189 PRAGMA encoding='utf16'; 190 PRAGMA auto_vacuum = 2; 191 PRAGMA user_version = 45; 192 PRAGMA application_id = 22; 193 194 CREATE TABLE u1(u, v); 195 INSERT INTO u1 VALUES('edvin marton', 'bond'); 196 INSERT INTO u1 VALUES(1, 4.0); 197 } 198 do_execsql_test 11.1a { 199 PRAGMA auto_vacuum; 200 } {2} 201 202 do_recover_test 11 203 204 do_test 12.1 { 205 set R [sqlite3_recover_init db "" test.db2] 206 $R config lostandfound "" 207 $R config invalid xyz 208 } {12} 209 do_test 12.2 { 210 $R run 211 $R run 212 } {0} 213 214 do_test 12.3 { 215 $R finish 216 } {} 217 218 219 220 #------------------------------------------------------------------------- 221 reset_db 222 file_control_reservebytes db 16 223 do_execsql_test 12.1 { 224 PRAGMA auto_vacuum = 2; 225 PRAGMA user_version = 45; 226 PRAGMA application_id = 22; 227 228 CREATE TABLE u1(u, v); 229 CREATE UNIQUE INDEX i1 ON u1(u, v); 230 INSERT INTO u1 VALUES(1, 2), (3, 4); 231 232 CREATE TABLE u2(u, v); 233 CREATE UNIQUE INDEX i2 ON u1(u, v); 234 INSERT INTO u2 VALUES(hex(randomblob(500)), hex(randomblob(1000))); 235 INSERT INTO u2 VALUES(hex(randomblob(500)), hex(randomblob(1000))); 236 INSERT INTO u2 VALUES(hex(randomblob(500)), hex(randomblob(1000))); 237 INSERT INTO u2 VALUES(hex(randomblob(50000)), hex(randomblob(20000))); 238 } 239 240 do_recover_test 12 241 242 #------------------------------------------------------------------------- 243 reset_db 244 sqlite3 db "" 245 do_recover_test 13 246 247 do_execsql_test 14.1 { 248 PRAGMA auto_vacuum = 2; 249 PRAGMA user_version = 45; 250 PRAGMA application_id = 22; 251 252 CREATE TABLE u1(u, v); 253 CREATE UNIQUE INDEX i1 ON u1(u, v); 254 INSERT INTO u1 VALUES(1, 2), (3, 4); 255 256 CREATE TABLE u2(u, v); 257 CREATE UNIQUE INDEX i2 ON u1(u, v); 258 INSERT INTO u2 VALUES(hex(randomblob(500)), hex(randomblob(1000))); 259 INSERT INTO u2 VALUES(hex(randomblob(500)), hex(randomblob(1000))); 260 INSERT INTO u2 VALUES(hex(randomblob(500)), hex(randomblob(1000))); 261 INSERT INTO u2 VALUES(hex(randomblob(50000)), hex(randomblob(20000))); 262 } 263 do_recover_test 14 264 265 #------------------------------------------------------------------------- 266 reset_db 267 execsql { 268 PRAGMA journal_mode=OFF; 269 PRAGMA mmap_size=10; 270 } 271 do_execsql_test 15.1 { 272 CREATE TABLE t1(x); 273 } {} 274 do_recover_test 15 275 276 #------------------------------------------------------------------------- 277 reset_db 278 do_test 16.1 { 279 execsql { PRAGMA journal_mode = wal } 280 execsql { 281 CREATE TABLE t1(x); 282 INSERT INTO t1 VALUES(1), (2), (3); 283 } 284 } {} 285 do_test 16.2 { 286 set R [sqlite3_recover_init db main test.db2] 287 $R run 288 $R finish 289 } {} 290 do_execsql_test 16.3 { 291 SELECT * FROM t1; 292 } {1 2 3} 293 294 do_execsql_test 16.4 { 295 BEGIN; 296 SELECT * FROM t1; 297 } {1 2 3} 298 do_test 16.5 { 299 set R [sqlite3_recover_init db main test.db2] 300 $R run 301 list [catch { $R finish } msg] $msg 302 } {1 {cannot start a transaction within a transaction}} 303 do_execsql_test 16.6 { 304 SELECT * FROM t1; 305 } {1 2 3} 306 do_execsql_test 16.7 { 307 INSERT INTO t1 VALUES(4); 308 } 309 do_test 16.8 { 310 set R [sqlite3_recover_init db main test.db2] 311 $R run 312 list [catch { $R finish } msg] $msg 313 } {1 {cannot start a transaction within a transaction}} 314 do_execsql_test 16.9 { 315 SELECT * FROM t1; 316 COMMIT; 317 } {1 2 3 4} 318 319 #------------------------------------------------------------------------- 320 reset_db 321 do_execsql_test 17.1 { 322 CREATE TABLE t(a, PRIMARY KEY(a, a COLLATE NOCASE)) WITHOUT ROWID; 323 INSERT INTO t VALUES('abc'); 324 INSERT INTO t VALUES('def'); 325 } 326 do_test 17.2 { 327 set R [sqlite3_recover_init db main test.db2] 328 $R run 329 list [catch { $R finish } msg] $msg 330 } {0 {}} 331 332 #------------------------------------------------------------------------- 333 foreach enc {utf8 utf16 utf16le utf16be} { 334 reset_db 335 do_execsql_test 18.$enc.1 { 336 PRAGMA auto_vacuum = 0; 337 PRAGMA encoding='utf16'; 338 CREATE TABLE t1(a, b); 339 CREATE TABLE t2(a, b); 340 INSERT INTO t1 VALUES('abc', 'def'); 341 PRAGMA writable_schema = 1; 342 DELETE FROM sqlite_schema WHERE name='t1'; 343 } 344 345 proc my_sql_hook2 {sql} { 346 if {[string match "INSERT INTO lostandfound*" $sql]} { 347 lappend ::script $sql 348 } 349 return 0 350 } 351 do_test 18.$enc.2 { 352 set ::script [list] 353 set R [sqlite3_recover_init_sql db main my_sql_hook2] 354 $R config lostandfound lostandfound 355 $R run 356 $R finish 357 set ::script 358 } {{INSERT INTO lostandfound VALUES(2, 2, 2, 1, 'abc', 'def')}} 359 } 360 361 #------------------------------------------------------------------------- 362 reset_db 363 do_execsql_test 19.0 { 364 CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT); 365 INSERT INTO t1 VALUES(1, 'one'); 366 INSERT INTO t1 VALUES(2, 'two'); 367 368 ALTER TABLE t1 ADD COLUMN c NOT NULL DEFAULT 13; 369 INSERT INTO t1 VALUES(3, 'three', 'hello world'); 370 } 371 372 do_recover_test 19.1 373 374 #------------------------------------------------------------------------- 375 # Bug 2026-10-18T20:41:57Z 376 # 377 # In a UTF-16 database, TEXT values that begin with U+FEFF or U+FFFE 378 # must be recovered unchanged. The leading character is part of the 379 # value, not a byte-order mark. Values 4 and 5 are large enough to 380 # spill onto overflow pages. 381 # 382 foreach enc {utf16le utf16be} { 383 reset_db 384 do_execsql_test 20.$enc.0 " 385 PRAGMA encoding = '$enc'; 386 CREATE TABLE t1(id INTEGER PRIMARY KEY, a TEXT); 387 INSERT INTO t1 VALUES 388 (1, char(0xfeff)||'abc'), 389 (2, char(0xfffe)||'xyz'), 390 (3, 'plain'), 391 (4, char(0xfeff)||printf('%.*c', 5000, 'a')), 392 (5, char(0xfffe)||printf('%.*c', 5000, 'b')); 393 " 394 395 set sql { 396 SELECT id, length(a), unicode(a), hex(substr(a,1,3)), hex(substr(a,-2)) 397 FROM t1 ORDER BY id 398 } 399 set expect [db eval $sql] 400 401 # Recover directly into a new database. 402 do_test 20.$enc.1 { 403 forcedelete test.db2 404 set R [sqlite3_recover_init db main test.db2] 405 $R run 406 $R finish 407 sqlite3 db2 test.db2 408 set res [db2 eval $sql] 409 db2 close 410 set res 411 } $expect 412 413 # Recover as SQL text (the path used by the CLI ".recover" command). 414 do_test 20.$enc.2 { 415 forcedelete test.db2 416 set ::sqlhook [list] 417 set R [sqlite3_recover_init_sql db main my_sql_hook] 418 $R run 419 $R finish 420 sqlite3 db2 test.db2 421 db2 eval [join $::sqlhook ";"] 422 set res [db2 eval $sql] 423 db2 close 424 set res 425 } $expect 426 } 427 428 #------------------------------------------------------------------------- 429 # https://sqlite.org/bugs/info/`03b1348dfac769e0` 430 # 431 # Ordinary columns that follow a VIRTUAL generated column. A VIRTUAL 432 # column has no field in the record, so a record has fewer fields than 433 # the table has columns. This used to cause the last ordinary column(s) 434 # to be silently dropped for rowid tables, and an SQLITE_RANGE error 435 # for tables with an INTEGER PRIMARY KEY and WITHOUT ROWID tables. 436 # 437 reset_db 438 do_execsql_test 21.0 { 439 CREATE TABLE t1(a TEXT, v AS (upper(a)), b TEXT, c INT); 440 INSERT INTO t1(a, b, c) VALUES('x', 'keep me', 42), ('y', 'also', 7); 441 } 442 do_recover_test 21.1 443 444 reset_db 445 do_execsql_test 21.2 { 446 CREATE TABLE t2(id INTEGER PRIMARY KEY, a TEXT, v AS (upper(a)), b TEXT); 447 INSERT INTO t2(a, b) VALUES('x', 'keep me'); 448 CREATE TABLE t3(z); 449 INSERT INTO t3 VALUES(1); 450 } 451 do_recover_test 21.3 452 453 reset_db 454 do_execsql_test 21.4 { 455 CREATE TABLE t4(a TEXT, v AS (upper(b)), b TEXT PRIMARY KEY, c INT) 456 WITHOUT ROWID; 457 INSERT INTO t4(a, b, c) VALUES('x', 'k1', 42), ('y', 'k2', 7); 458 } 459 do_recover_test 21.5 460 461 # Several VIRTUAL and STORED columns, plus short records that predate an 462 # ALTER TABLE ADD COLUMN. 463 reset_db 464 do_execsql_test 21.6 { 465 CREATE TABLE t5(a, v1 AS (a*2), b, v2 AS (b||'!') STORED, c, v3 AS (c+1)); 466 INSERT INTO t5(a, b, c) VALUES(1, 'b', 3); 467 ALTER TABLE t5 ADD COLUMN d DEFAULT 'dflt'; 468 INSERT INTO t5(a, b, c, d) VALUES(4, 'e', 6, 'z'); 469 CREATE TABLE t6(id INTEGER PRIMARY KEY, v AS (id*10), a); 470 INSERT INTO t6(id, a) VALUES(5, 'five'), (6, 'six'); 471 } 472 do_recover_test 21.7 473 474 #------------------------------------------------------------------------- 475 # A WITHOUT ROWID table with a column that appears more than once in 476 # the PRIMARY KEY has more fields in each record than it has columns. 477 # Such tables used to be recovered with NULL or missing values (see also 478 # test 17 above). 479 # 480 reset_db 481 do_execsql_test 22.0 { 482 CREATE TABLE t1(a, PRIMARY KEY(a, a COLLATE NOCASE)) WITHOUT ROWID; 483 INSERT INTO t1 VALUES('abc'), ('def'); 484 CREATE TABLE t2(a, b, PRIMARY KEY(a, a COLLATE NOCASE)) WITHOUT ROWID; 485 INSERT INTO t2 VALUES('abc', 1), ('def', 2); 486 CREATE TABLE t3( 487 a, s AS (a||'s') STORED, v AS (a||'v'), b, c, 488 PRIMARY KEY(b, a, b COLLATE nocase) 489 ) WITHOUT ROWID; 490 INSERT INTO t3(a, b, c) VALUES(1, 'B', 3), (4, 'b2', 6); 491 } 492 do_recover_test 22.1 493 494 finish_test