3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
13 source [file join [file dirname [info script]] recover_common.tcl]
14 set testprefix recover1
16 proc compare_result {db1 db2 sql} {
17 set r1 [$db1 eval $sql]
18 set r2 [$db2 eval $sql]
22 error "mismatch for $sql"
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"
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"
40 proc do_recover_test {tn} {
44 uplevel [list do_test $tn.1 {
45 set R [sqlite3_recover_init db main test.db2]
46 $R config testdb rstate.db
52 uplevel [list do_test $tn.2 [list compare_dbs db db2] {}]
58 uplevel [list do_test $tn.3 {
60 set R [sqlite3_recover_init_sql db main my_sql_hook]
61 $R config testdb rstate.db
68 execsql [join $::sqlhook ";"] db2
71 uplevel [list do_test $tn.4 [list compare_dbs db db2] {}]
75 proc my_sql_hook {sql} {
76 lappend ::sqlhook $sql
81 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
82 CREATE TABLE t2(a INTEGER PRIMARY KEY, b) WITHOUT ROWID;
84 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10
86 INSERT INTO t1 SELECT i*2, hex(randomblob(250)) FROM s;
87 INSERT INTO t2 SELECT * FROM t1;
93 ALTER TABLE t1 ADD COLUMN c DEFAULT 'xyz'
98 CREATE INDEX i1 ON t1(c);
102 do_execsql_test 4.0 {
103 CREATE VIEW v1 AS SELECT * FROM t2;
107 do_execsql_test 5.0 {
108 CREATE UNIQUE INDEX i2 ON t1(c, b);
112 #--------------------------------------------------------------------------
115 do_execsql_test 6.0 {
117 a INTEGER PRIMARY KEY,
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
125 INSERT INTO t1 VALUES(1, 2, 'hello world');
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');
133 do_execsql_test 7.1 {
139 #--------------------------------------------------------------------------
142 do_execsql_test 8.0 {
143 CREATE TABLE x1(a INTEGER PRIMARY KEY AUTOINCREMENT, b, c);
145 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<2
147 INSERT INTO x1(b, c) SELECT hex(randomblob(100)), hex(randomblob(100)) FROM s;
149 CREATE INDEX x1b ON x1(b);
150 CREATE INDEX x1cb ON x1(c, b);
151 DELETE FROM x1 WHERE a>50;
158 #-------------------------------------------------------------------------
161 do_execsql_test 9.1 {
162 CREATE VIRTUAL TABLE ft5 USING fts5(a, b);
163 INSERT INTO ft5 VALUES('hello', 'world');
168 #-------------------------------------------------------------------------
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)
175 INSERT INTO x1 VALUES(2, '
179 do_execsql_test 10.2 "
180 INSERT INTO x1 VALUES(3, '\012hello there\015world');
181 INSERT INTO x1 VALUES(4, '\015hello there\015world');
185 #-------------------------------------------------------------------------
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;
194 CREATE TABLE u1(u, v);
195 INSERT INTO u1 VALUES('edvin marton', 'bond');
196 INSERT INTO u1 VALUES(1, 4.0);
198 do_execsql_test 11.1a {
205 set R [sqlite3_recover_init db "" test.db2]
206 $R config lostandfound ""
207 $R config invalid xyz
220 #-------------------------------------------------------------------------
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;
228 CREATE TABLE u1(u, v);
229 CREATE UNIQUE INDEX i1 ON u1(u, v);
230 INSERT INTO u1 VALUES(1, 2), (3, 4);
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)));
242 #-------------------------------------------------------------------------
247 do_execsql_test 14.1 {
248 PRAGMA auto_vacuum = 2;
249 PRAGMA user_version = 45;
250 PRAGMA application_id = 22;
252 CREATE TABLE u1(u, v);
253 CREATE UNIQUE INDEX i1 ON u1(u, v);
254 INSERT INTO u1 VALUES(1, 2), (3, 4);
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)));
265 #-------------------------------------------------------------------------
268 PRAGMA journal_mode=OFF;
271 do_execsql_test 15.1 {
276 #-------------------------------------------------------------------------
279 execsql { PRAGMA journal_mode = wal }
282 INSERT INTO t1 VALUES(1), (2), (3);
286 set R [sqlite3_recover_init db main test.db2]
290 do_execsql_test 16.3 {
294 do_execsql_test 16.4 {
299 set R [sqlite3_recover_init db main test.db2]
301 list [catch { $R finish } msg] $msg
302 } {1 {cannot start a transaction within a transaction}}
303 do_execsql_test 16.6 {
306 do_execsql_test 16.7 {
307 INSERT INTO t1 VALUES(4);
310 set R [sqlite3_recover_init db main test.db2]
312 list [catch { $R finish } msg] $msg
313 } {1 {cannot start a transaction within a transaction}}
314 do_execsql_test 16.9 {
319 #-------------------------------------------------------------------------
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');
327 set R [sqlite3_recover_init db main test.db2]
329 list [catch { $R finish } msg] $msg
332 #-------------------------------------------------------------------------
333 foreach enc {utf8 utf16 utf16le utf16be} {
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';
345 proc my_sql_hook2 {sql} {
346 if {[string match "INSERT INTO lostandfound*" $sql]} {
347 lappend ::script $sql
353 set R [sqlite3_recover_init_sql db main my_sql_hook2]
354 $R config lostandfound lostandfound
358 } {{INSERT INTO lostandfound VALUES(2, 2, 2, 1, 'abc', 'def')}}
361 #-------------------------------------------------------------------------
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');
368 ALTER TABLE t1 ADD COLUMN c NOT NULL DEFAULT 13;
369 INSERT INTO t1 VALUES(3, 'three', 'hello world');