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/recoverrowid.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 / recoverrowid.test` 150 lines ยท 4183 bytes Raw Blame History 1 # 2022 September 07 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 # Tests for the SQLITE_RECOVER_ROWIDS option. 13 # 14 15 source [file join [file dirname [info script]] recover_common.tcl] 16 set testprefix recoverrowid 17 18 proc recover {db bRowids output} { 19 forcedelete $output 20 21 set R [sqlite3_recover_init db main test.db2] 22 $R config rowids $bRowids 23 $R run 24 $R finish 25 } 26 27 do_execsql_test 1.0 { 28 CREATE TABLE t1(a, b); 29 INSERT INTO t1 VALUES(1, 1), (2, 2), (3, 3), (4, 4); 30 DELETE FROM t1 WHERE a IN (1, 3); 31 } 32 33 do_test 1.1 { 34 recover db 0 test.db2 35 sqlite3 db2 test.db2 36 execsql { SELECT rowid, a, b FROM t1 ORDER BY rowid} db2 37 } {1 2 2 2 4 4} 38 39 do_test 1.2 { 40 db2 close 41 recover db 1 test.db2 42 sqlite3 db2 test.db2 43 execsql { SELECT rowid, a, b FROM t1 ORDER BY rowid} db2 44 } {2 2 2 4 4 4} 45 db2 close 46 47 #------------------------------------------------------------------------- 48 # Bug 2026-10-20T02:57:07Z: A column declared "INTEGER PRIMARY KEY DESC" 49 # is not an alias for the rowid, and so must not be recovered as one. 50 # 51 reset_db 52 do_execsql_test 2.0 { 53 CREATE TABLE invoice(id INTEGER PRIMARY KEY DESC, customer TEXT); 54 INSERT INTO invoice VALUES(1001, 'alice'), (1002, 'bob'), (2001, 'carol'); 55 CREATE TABLE w(v, id INTEGER PRIMARY KEY DESC); 56 INSERT INTO w VALUES('x', 42), ('y', 43); 57 DELETE FROM w WHERE v='x'; 58 59 -- These are all rowid aliases, or are not rowid tables 60 CREATE TABLE a1(id INTEGER PRIMARY KEY, c); 61 INSERT INTO a1 VALUES(77, 'ipk'); 62 CREATE TABLE a2(id INTEGER, c, PRIMARY KEY(id DESC)); 63 INSERT INTO a2 VALUES(88, 'tblpk'); 64 CREATE TABLE a3(id INTEGER PRIMARY KEY DESC, c) WITHOUT ROWID; 65 INSERT INTO a3 VALUES(99, 'wor'); 66 } 67 68 foreach {tn bRowids wres} { 69 1 0 {1 y 43} 70 2 1 {2 y 43} 71 } { 72 do_test 2.$tn { 73 recover db $bRowids test.db2 74 sqlite3 db2 test.db2 75 set res [execsql { 76 SELECT rowid, id, customer FROM invoice ORDER BY rowid; 77 SELECT rowid, v, id FROM w; 78 SELECT rowid, id, c FROM a1; 79 SELECT rowid, id, c FROM a2; 80 SELECT id, c FROM a3; 81 } db2] 82 db2 close 83 set res 84 } [list 1 1001 alice 2 1002 bob 3 2001 carol {*}$wres \ 85 77 77 ipk 88 88 tblpk 99 wor] 86 } 87 88 #------------------------------------------------------------------------- 89 # Bug 2026-10-20T02:57:35Z: If a table has a column named "_rowid_", 90 # the recovered rowid must be written using some other name. If the 91 # names "_rowid_", "rowid" and "oid" are all used by columns, the rowid 92 # is not preserved. 93 # 94 reset_db 95 do_execsql_test 3.0 { 96 CREATE TABLE t1(_rowid_ TEXT, v TEXT); 97 INSERT INTO t1(oid, _rowid_, v) VALUES(10, 'ext-7f3a', 'first'); 98 INSERT INTO t1(oid, _rowid_, v) VALUES(20, 'ext-91c2', 'second'); 99 CREATE TABLE t2(_ROWID_, Rowid); 100 INSERT INTO t2(oid, _rowid_, rowid) VALUES(7, 'u', 'r'); 101 CREATE TABLE t3(rowid, _rowid_, oid); 102 INSERT INTO t3 VALUES('r', 'u', 'o'); 103 CREATE TABLE t4(a, rowid, b); 104 INSERT INTO t4(oid, a, rowid, b) VALUES(5, 'a', 'r', 'b'); 105 } 106 107 proc rowidname_contents {db} { 108 execsql { 109 SELECT oid, _rowid_, v FROM t1 ORDER BY oid; 110 SELECT oid, _rowid_, rowid FROM t2; 111 SELECT rowid, _rowid_, oid FROM t3; 112 SELECT _rowid_, a, rowid, b FROM t4; 113 } $db 114 } 115 116 foreach {tn bRowids res} { 117 1 1 {10 ext-7f3a first 20 ext-91c2 second 7 u r r u o 5 a r b} 118 2 0 {1 ext-7f3a first 2 ext-91c2 second 1 u r r u o 1 a r b} 119 } { 120 do_test 3.$tn { 121 recover db $bRowids test.db2 122 sqlite3 db2 test.db2 123 set res [rowidname_contents db2] 124 db2 close 125 set res 126 } $res 127 } 128 129 proc my_sql_hook {sql} { 130 lappend ::sqlhook $sql 131 return 0 132 } 133 do_test 3.3 { 134 set ::sqlhook [list] 135 set R [sqlite3_recover_init_sql db main my_sql_hook] 136 $R config rowids 1 137 $R run 138 $R finish 139 forcedelete test.db2 140 sqlite3 db2 test.db2 141 db2 eval [join $::sqlhook ";"] 142 set res [rowidname_contents db2] 143 db2 close 144 set res 145 } {10 ext-7f3a first 20 ext-91c2 second 7 u r r u o 5 a r b} 146 147 148 149 150 finish_test