interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / recover.test

182 lines · 4349 bytes Raw Blame History
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
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18set testprefix recover
19
20ifcapable !vtab {
21 finish_test; return
22}
23set CLI [test_find_cli]
24
25proc 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
36proc 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
43proc 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
59proc 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
71set doc {
72 hello
73 world
74}
75do_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}
81do_recover_test 1.1.2
82
83do_execsql_test 1.2.1 "
84 DELETE FROM t1;
85 INSERT INTO t1 VALUES(13, 'hello\r\nworld', 13);
86"
87do_recover_test 1.2.2
88
89do_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"
100do_recover_test 1.3.2
101
102#-------------------------------------------------------------------------
103reset_db
104do_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
112do_recover_test 2.1.1
113
114do_execsql_test 2.2.0 {
115 PRAGMA writable_schema = 1;
116 DELETE FROM sqlite_master WHERE name='t1';
117}
118do_recover_test 2.2.1 {
119 SELECT name FROM sqlite_master
120} {lost_and_found}
121
122do_execsql_test 2.3.0 {
123 CREATE TABLE lost_and_found(a, b, c);
124}
125do_recover_test 2.3.1 {
126 SELECT name FROM sqlite_master
127} {lost_and_found lost_and_found_0}
128
129do_execsql_test 2.4.0 {
130 CREATE TABLE lost_and_found_0(a, b, c);
131}
132do_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#-------------------------------------------------------------------------
142reset_db
143do_recover_test 3.0
144
145#-------------------------------------------------------------------------
146reset_db
147execsql { PRAGMA secure_delete = 0 }
148execsql { PRAGMA auto_vacuum = 0 }
149do_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
162recover_with_opts ""
163sqlite3 db2 test.db2
164do_execsql_test -db db2 4.1.1 {
165 SELECT name FROM sqlite_schema
166} {t3 lost_and_found}
167do_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}
170db2 close
171
172recover_with_opts -ignore-freelist
173sqlite3 db2 test.db2
174do_execsql_test -db db2 4.2.1 {
175 SELECT name FROM sqlite_schema
176} {t3}
177do_execsql_test -db db2 4.2.2 {
178 SELECT * FROM t3
179} {one two three}
180db2 close
181
182finish_test