xref: /sqlite-3.40.0/test/recover.test (revision c59b7b1f)
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#
12# Test the shell tool ".ar" command.
13#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17set testprefix recover
18
19ifcapable !vtab {
20  finish_test; return
21}
22set CLI [test_find_cli]
23
24proc compare_result {db1 db2 sql} {
25  set r1 [$db1 eval $sql]
26  set r2 [$db2 eval $sql]
27  if {$r1 != $r2} {
28  puts "r1: $r1"
29  puts "r2: $r2"
30    error "mismatch for $sql"
31  }
32  return ""
33}
34
35proc compare_dbs {db1 db2} {
36  compare_result $db1 $db2 "SELECT sql FROM sqlite_master ORDER BY 1"
37  foreach tbl [$db1 eval {SELECT name FROM sqlite_master WHERE type='table'}] {
38    compare_result $db1 $db2 "SELECT * FROM $tbl"
39  }
40}
41
42proc do_recover_test {tn {tsql {}} {res {}}} {
43  set fd [open "|$::CLI test.db .recover"]
44  fconfigure $fd -encoding binary
45  fconfigure $fd -translation binary
46  set sql [read $fd]
47  close $fd
48
49  forcedelete test.db2
50  sqlite3 db2 test.db2
51  execsql $sql db2
52  if {$tsql==""} {
53    uplevel [list do_test $tn [list compare_dbs db db2] {}]
54  } else {
55    uplevel [list do_execsql_test -db db2 $tn $tsql $res]
56  }
57  db2 close
58}
59
60set doc {
61  hello
62  world
63}
64do_execsql_test 1.1.1 {
65  CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
66  INSERT INTO t1 VALUES(1, 4, X'1234567800');
67  INSERT INTO t1 VALUES(2, 'test', 8.1);
68  INSERT INTO t1 VALUES(3, $doc, 8.4);
69}
70do_recover_test 1.1.2
71
72do_execsql_test 1.2.1 "
73  DELETE FROM t1;
74  INSERT INTO t1 VALUES(13, 'hello\r\nworld', 13);
75"
76do_recover_test 1.2.2
77
78do_execsql_test 1.3.1 "
79  CREATE TABLE t2(i INTEGER PRIMARY KEY AUTOINCREMENT, b, c);
80  INSERT INTO t2 VALUES(NULL, 1, 2);
81  INSERT INTO t2 VALUES(NULL, 3, 4);
82  INSERT INTO t2 VALUES(NULL, 5, 6);
83  CREATE TABLE t3(i INTEGER PRIMARY KEY AUTOINCREMENT, b, c);
84  INSERT INTO t3 VALUES(NULL, 1, 2);
85  INSERT INTO t3 VALUES(NULL, 3, 4);
86  INSERT INTO t3 VALUES(NULL, 5, 6);
87  DELETE FROM t2;
88"
89do_recover_test 1.3.2
90
91#-------------------------------------------------------------------------
92reset_db
93do_execsql_test 2.1.0 {
94  PRAGMA auto_vacuum = 0;
95  CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c)) WITHOUT ROWID;
96  INSERT INTO t1 VALUES(1, 2, 3);
97  INSERT INTO t1 VALUES(4, 5, 6);
98  INSERT INTO t1 VALUES(7, 8, 9);
99}
100
101do_recover_test 2.1.1
102
103do_execsql_test 2.2.0 {
104  PRAGMA writable_schema = 1;
105  DELETE FROM sqlite_master WHERE name='t1';
106}
107do_recover_test 2.2.1 {
108  SELECT name FROM sqlite_master
109} {lost_and_found}
110
111do_execsql_test 2.3.0 {
112  CREATE TABLE lost_and_found(a, b, c);
113}
114do_recover_test 2.3.1 {
115  SELECT name FROM sqlite_master
116} {lost_and_found lost_and_found_0}
117
118do_execsql_test 2.4.0 {
119  CREATE TABLE lost_and_found_0(a, b, c);
120}
121do_recover_test 2.4.1 {
122  SELECT name FROM sqlite_master;
123  SELECT * FROM lost_and_found_1;
124} {lost_and_found lost_and_found_0 lost_and_found_1
125  2 2 3 {} 2 3 1
126  2 2 3 {} 5 6 4
127  2 2 3 {} 8 9 7
128}
129
130#-------------------------------------------------------------------------
131reset_db
132do_recover_test 3.0
133
134finish_test
135