Debian Codegraph @

ext/rbu/rbu3.test

raw
1 # 2014 August 30
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
13 source [file join [file dirname [info script]] rbu_common.tcl]
14 if_no_rbu_support { finish_test ; return }
15 set ::testprefix rbu3
16
17
18 # Run the RBU in file $rbu on target database $target until completion.
19 #
20 proc run_rbu {target rbu} {
21 sqlite3rbu rbu $target $rbu
22 while { [rbu step]=="SQLITE_OK" } {}
23 rbu close
24 }
25
26 forcedelete test.db-oal rbu.db
27 reset_db
28
29 #--------------------------------------------------------------------
30 # Test that for an RBU to be applied, no corruption results if the
31 # affinities on the source and target table do not match.
32 #
33 do_execsql_test 1.0 {
34 CREATE TABLE x1(a INTEGER PRIMARY KEY, b TEXT, c REAL);
35 CREATE INDEX i1 ON x1(b, c);
36 } {}
37
38 do_test 1.1 {
39 sqlite3 db2 rbu.db
40 db2 eval {
41 CREATE TABLE data_x1(a, b, c, rbu_control);
42 INSERT INTO data_x1 VALUES(1, '123', '123', 0);
43 INSERT INTO data_x1 VALUES(2, 123, 123, 0);
44 }
45 db2 close
46 run_rbu test.db rbu.db
47 } {SQLITE_DONE}
48
49 do_execsql_test 1.2 {
50 PRAGMA integrity_check;
51 } {ok}
52
53 #--------------------------------------------------------------------
54 # Test that NULL values may not be inserted into INTEGER PRIMARY KEY
55 # columns.
56 #
57 forcedelete rbu.db
58 reset_db
59
60 do_execsql_test 2.0 {
61 CREATE TABLE x1(a INTEGER PRIMARY KEY, b TEXT, c REAL);
62 CREATE INDEX i1 ON x1(b, c);
63 } {}
64
65 foreach {tn rbudb} {
66 1 {
67 CREATE TABLE data_x1(a, b, c, rbu_control);
68 INSERT INTO data_x1 VALUES(NULL, 'a', 'b', 0);
69 }
70
71 2 {
72 CREATE TABLE data_x1(c, b, a, rbu_control);
73 INSERT INTO data_x1 VALUES('b', 'a', NULL, 0);
74 }
75 } {
76 do_test 2.$tn.1 {
77 forcedelete rbu.db
78 sqlite3 db2 rbu.db
79 db2 eval $rbudb
80 db2 close
81 list [catch { run_rbu test.db rbu.db } msg] $msg
82 } {1 {SQLITE_MISMATCH - datatype mismatch}}
83
84 do_execsql_test 2.1.2 {
85 PRAGMA integrity_check;
86 } {ok}
87 }
88
89 #--------------------------------------------------------------------
90 # Test that missing columns are detected.
91 #
92 forcedelete rbu.db
93 reset_db
94
95 do_execsql_test 2.0 {
96 CREATE TABLE x1(a INTEGER PRIMARY KEY, b, c);
97 CREATE INDEX i1 ON x1(b, c);
98 } {}
99
100 do_test 2.1 {
101 sqlite3 db2 rbu.db
102 db2 eval {
103 CREATE TABLE data_x1(a, b, rbu_control);
104 INSERT INTO data_x1 VALUES(1, 'a', 0);
105 }
106 db2 close
107 list [catch { run_rbu test.db rbu.db } msg] $msg
108 } {1 {SQLITE_ERROR - column missing from data_x1: c}}
109
110 do_execsql_test 2.2 {
111 PRAGMA integrity_check;
112 } {ok}
113
114 # Also extra columns.
115 #
116 do_execsql_test 2.3 {
117 CREATE TABLE x2(a INTEGER PRIMARY KEY, b, c);
118 CREATE INDEX i2 ON x2(b, c);
119 } {}
120
121 do_test 2.4 {
122 forcedelete rbu.db
123 sqlite3 db2 rbu.db
124 db2 eval {
125 CREATE TABLE data_x2(a, b, c, d, rbu_control);
126 INSERT INTO data_x2 VALUES(1, 'a', 2, 3, 0);
127 }
128 db2 close
129 list [catch { run_rbu test.db rbu.db } msg] $msg
130 } {1 SQLITE_ERROR}
131
132 do_execsql_test 2.5 {
133 PRAGMA integrity_check;
134 } {ok}
135
136
137 #-------------------------------------------------------------------------
138 # Test that sqlite3rbu_create_vfs() returns an error if the requested
139 # parent VFS is unknown.
140 #
141 # And that nothing disasterous happens if a VFS name passed to
142 # sqlite3rbu_destroy_vfs() is unknown or not an RBU vfs.
143 #
144 do_test 3.1 {
145 list [catch {sqlite3rbu_create_vfs xyz nosuchparent} msg] $msg
146 } {1 SQLITE_NOTFOUND}
147
148 do_test 3.2 {
149 sqlite3rbu_destroy_vfs nosuchvfs
150 sqlite3rbu_destroy_vfs unix
151 sqlite3rbu_destroy_vfs win32
152 } {}
153
154 #-------------------------------------------------------------------------
155 # Test that it is an error to specify an explicit VFS that does not
156 # include rbu VFS functionality.
157 #
158 do_test 4.1 {
159 testvfs tvfs
160 sqlite3rbu rbu file:test.db?vfs=tvfs rbu.db
161 list [catch { rbu step } msg] $msg
162 } {0 SQLITE_ERROR}
163 do_test 4.2 {
164 list [catch { rbu close } msg] $msg
165 } {1 {SQLITE_ERROR - rbu vfs not found}}
166 tvfs delete
167
168 #-------------------------------------------------------------------------
169 # Test a large rbu update to ensure that wal_autocheckpoint does not get
170 # in the way.
171 #
172 forcedelete rbu.db
173 reset_db
174 do_execsql_test 5.1 {
175 CREATE TABLE x1(a, b, c, PRIMARY KEY(a)) WITHOUT ROWID;
176 CREATE INDEX i1 ON x1(a);
177
178 ATTACH 'rbu.db' AS rbu;
179 CREATE TABLE rbu.data_x1(a, b, c, rbu_control);
180 WITH s(a, b, c) AS (
181 SELECT randomblob(300), randomblob(300), 1
182 UNION ALL
183 SELECT randomblob(300), randomblob(300), c+1 FROM s WHERE c<2000
184 )
185 INSERT INTO data_x1 SELECT a, b, c, 0 FROM s;
186 }
187
188 do_test 5.2 {
189 sqlite3rbu rbu test.db rbu.db
190 while {[rbu step]=="SQLITE_OK" && [file exists test.db-wal]==0} {}
191 rbu close
192 } {SQLITE_OK}
193
194 do_test 5.3 {
195 expr {[file size test.db-wal] > (1024 * 1200)}
196 } 1
197
198 do_test 6.1 { sqlite3rbu_internal_test } {}
199
200 finish_test