sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | # 2019 April 23 |
| 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 | # TESTRUNNER: shell |
| 12 | # |
| 13 | # Test the shell tool ".ar" command. |
| 14 | # |
| 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | set testprefix recover |
| 19 | |
| 20 | ifcapable !vtab { |
| 21 | finish_test; return |
| 22 | } |
| 23 | set CLI [test_find_cli] |
| 24 | |
| 25 | proc compare_result {db1 db2 sql} { |
| 26 | set r1 [$db1 eval $sql] |
| 27 | set r2 [$db2 eval $sql] |
| 28 | if {$r1 != $r2} { |
| 29 | puts "r1: $r1" |
| 30 | puts "r2: $r2" |
| 31 | error "mismatch for $sql" |
| 32 | } |
| 33 | return "" |
| 34 | } |
| 35 | |
| 36 | proc compare_dbs {db1 db2} { |
| 37 | compare_result $db1 $db2 "SELECT sql FROM sqlite_master ORDER BY 1" |
| 38 | foreach tbl [$db1 eval {SELECT name FROM sqlite_master WHERE type='table'}] { |
| 39 | compare_result $db1 $db2 "SELECT * FROM $tbl" |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | proc recover_with_opts {opts} { |
| 44 | set cmd ".recover $opts" |
| 45 | set fd [open [list |$::CLI -noinit test.db $cmd]] |
| 46 | fconfigure $fd -translation binary |
| 47 | set sql [read $fd] |
| 48 | close $fd |
| 49 | |
| 50 | # Remove the ".dbconfig defensive off" and ".safe" lines |
| 51 | set sql [string map {".dbconfig defensive off" "" ".safe" ""} $sql] |
| 52 | |
| 53 | forcedelete test.db2 |
| 54 | sqlite3 db2 test.db2 |
| 55 | execsql $sql db2 |
| 56 | db2 close |
| 57 | } |
| 58 | |
| 59 | proc do_recover_test {tn {tsql {}} {res {}}} { |
| 60 | recover_with_opts "" |
| 61 | |
| 62 | sqlite3 db2 test.db2 |
| 63 | if {$tsql==""} { |
| 64 | uplevel [list do_test $tn [list compare_dbs db db2] {}] |
| 65 | } else { |
| 66 | uplevel [list do_execsql_test -db db2 $tn $tsql $res] |
| 67 | } |
| 68 | db2 close |
| 69 | } |
| 70 | |
| 71 | set doc { |
| 72 | hello |
| 73 | world |
| 74 | } |
| 75 | do_execsql_test 1.1.1 { |
| 76 | CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); |
| 77 | INSERT INTO t1 VALUES(1, 4, X'1234567800'); |
| 78 | INSERT INTO t1 VALUES(2, 'test', 8.1); |
| 79 | INSERT INTO t1 VALUES(3, $doc, 8.4); |
| 80 | } |
| 81 | do_recover_test 1.1.2 |
| 82 | |
| 83 | do_execsql_test 1.2.1 " |
| 84 | DELETE FROM t1; |
| 85 | INSERT INTO t1 VALUES(13, 'hello\r\nworld', 13); |
| 86 | " |
| 87 | do_recover_test 1.2.2 |
| 88 | |
| 89 | do_execsql_test 1.3.1 " |
| 90 | CREATE TABLE t2(i INTEGER PRIMARY KEY AUTOINCREMENT, b, c); |
| 91 | INSERT INTO t2 VALUES(NULL, 1, 2); |
| 92 | INSERT INTO t2 VALUES(NULL, 3, 4); |
| 93 | INSERT INTO t2 VALUES(NULL, 5, 6); |
| 94 | CREATE TABLE t3(i INTEGER PRIMARY KEY AUTOINCREMENT, b, c); |
| 95 | INSERT INTO t3 VALUES(NULL, 1, 2); |
| 96 | INSERT INTO t3 VALUES(NULL, 3, 4); |
| 97 | INSERT INTO t3 VALUES(NULL, 5, 6); |
| 98 | DELETE FROM t2; |
| 99 | " |
| 100 | do_recover_test 1.3.2 |
| 101 | |
| 102 | #------------------------------------------------------------------------- |
| 103 | reset_db |
| 104 | do_execsql_test 2.1.0 { |
| 105 | PRAGMA auto_vacuum = 0; |
| 106 | CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c)) WITHOUT ROWID; |
| 107 | INSERT INTO t1 VALUES(1, 2, 3); |
| 108 | INSERT INTO t1 VALUES(4, 5, 6); |
| 109 | INSERT INTO t1 VALUES(7, 8, 9); |
| 110 | } |
| 111 | |
| 112 | do_recover_test 2.1.1 |
| 113 | |
| 114 | do_execsql_test 2.2.0 { |
| 115 | PRAGMA writable_schema = 1; |
| 116 | DELETE FROM sqlite_master WHERE name='t1'; |
| 117 | } |
| 118 | do_recover_test 2.2.1 { |
| 119 | SELECT name FROM sqlite_master |
| 120 | } {lost_and_found} |
| 121 | |
| 122 | do_execsql_test 2.3.0 { |
| 123 | CREATE TABLE lost_and_found(a, b, c); |
| 124 | } |
| 125 | do_recover_test 2.3.1 { |
| 126 | SELECT name FROM sqlite_master |
| 127 | } {lost_and_found lost_and_found_0} |
| 128 | |
| 129 | do_execsql_test 2.4.0 { |
| 130 | CREATE TABLE lost_and_found_0(a, b, c); |
| 131 | } |
| 132 | do_recover_test 2.4.1 { |
| 133 | SELECT name FROM sqlite_master; |
| 134 | SELECT * FROM lost_and_found_1; |
| 135 | } {lost_and_found lost_and_found_0 lost_and_found_1 |
| 136 | 2 2 3 {} 2 3 1 |
| 137 | 2 2 3 {} 5 6 4 |
| 138 | 2 2 3 {} 8 9 7 |
| 139 | } |
| 140 | |
| 141 | #------------------------------------------------------------------------- |
| 142 | reset_db |
| 143 | do_recover_test 3.0 |
| 144 | |
| 145 | #------------------------------------------------------------------------- |
| 146 | reset_db |
| 147 | execsql { PRAGMA secure_delete = 0 } |
| 148 | execsql { PRAGMA auto_vacuum = 0 } |
| 149 | do_execsql_test 4.0 { |
| 150 | CREATE TABLE t1(a, b, c); |
| 151 | CREATE TABLE t2(d, e, f); |
| 152 | CREATE TABLE t3(g, h, i); |
| 153 | |
| 154 | INSERT INTO t2 VALUES(1, 2, 3); |
| 155 | INSERT INTO t2 VALUES('a', 'b', 'c'); |
| 156 | |
| 157 | INSERT INTO t3 VALUES('one', 'two', 'three'); |
| 158 | DROP TABLE t1; |
| 159 | DROP TABLE t2; |
| 160 | } |
| 161 | |
| 162 | recover_with_opts "" |
| 163 | sqlite3 db2 test.db2 |
| 164 | do_execsql_test -db db2 4.1.1 { |
| 165 | SELECT name FROM sqlite_schema |
| 166 | } {t3 lost_and_found} |
| 167 | do_execsql_test -db db2 4.1.2 { |
| 168 | SELECT id, c0, c1, c2 FROM lost_and_found |
| 169 | } {1 1 2 3 2 a b c} |
| 170 | db2 close |
| 171 | |
| 172 | recover_with_opts -ignore-freelist |
| 173 | sqlite3 db2 test.db2 |
| 174 | do_execsql_test -db db2 4.2.1 { |
| 175 | SELECT name FROM sqlite_schema |
| 176 | } {t3} |
| 177 | do_execsql_test -db db2 4.2.2 { |
| 178 | SELECT * FROM t3 |
| 179 | } {one two three} |
| 180 | db2 close |
| 181 | |
| 182 | finish_test |