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 / ext / rbu / rbu3.test

207 lines · 5150 bytes Raw Blame History
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
13source [file join [file dirname [info script]] rbu_common.tcl]
14if_no_rbu_support { finish_test ; return }
15set ::testprefix rbu3
16
17
18# Run the RBU in file $rbu on target database $target until completion.
19#
20proc run_rbu {target rbu} {
21 sqlite3rbu rbu $target $rbu
22 while { [rbu step]=="SQLITE_OK" } {}
23 rbu close
24}
25
26forcedelete test.db-oal rbu.db
27reset_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#
33do_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
38do_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
49do_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#
57forcedelete rbu.db
58reset_db
59
60do_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
65foreach {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#
92forcedelete rbu.db
93reset_db
94
95do_execsql_test 2.0 {
96 CREATE TABLE x1(a INTEGER PRIMARY KEY, b, c);
97 CREATE INDEX i1 ON x1(b, c);
98} {}
99
100do_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
110do_execsql_test 2.2 {
111 PRAGMA integrity_check;
112} {ok}
113
114# Also extra columns.
115#
116do_execsql_test 2.3 {
117 CREATE TABLE x2(a INTEGER PRIMARY KEY, b, c);
118 CREATE INDEX i2 ON x2(b, c);
119} {}
120
121do_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
132do_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#
144do_test 3.1 {
145 list [catch {sqlite3rbu_create_vfs xyz nosuchparent} msg] $msg
146} {1 SQLITE_NOTFOUND}
147
148do_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#
158do_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}
163do_test 4.2 {
164 list [catch { rbu close } msg] $msg
165} {1 {SQLITE_ERROR - rbu vfs not found}}
166do_test 4.3 {
167 sqlite3rbu_vacuum rbu file:test.db?vfs=tvfs
168 list [catch { rbu step } msg] $msg
169} {0 SQLITE_ERROR}
170do_test 4.4 {
171 list [catch { rbu close } msg] $msg
172} {1 {SQLITE_ERROR - rbu vfs not found}}
173tvfs delete
174
175#-------------------------------------------------------------------------
176# Test a large rbu update to ensure that wal_autocheckpoint does not get
177# in the way.
178#
179forcedelete rbu.db
180reset_db
181do_execsql_test 5.1 {
182 CREATE TABLE x1(a, b, c, PRIMARY KEY(a)) WITHOUT ROWID;
183 CREATE INDEX i1 ON x1(a);
184
185 ATTACH 'rbu.db' AS rbu;
186 CREATE TABLE rbu.data_x1(a, b, c, rbu_control);
187 WITH s(a, b, c) AS (
188 SELECT randomblob(300), randomblob(300), 1
189 UNION ALL
190 SELECT randomblob(300), randomblob(300), c+1 FROM s WHERE c<2000
191 )
192 INSERT INTO data_x1 SELECT a, b, c, 0 FROM s;
193}
194
195do_test 5.2 {
196 sqlite3rbu rbu test.db rbu.db
197 while {[rbu step]=="SQLITE_OK" && [file exists test.db-wal]==0} {}
198 rbu close
199} {SQLITE_OK}
200
201do_test 5.3 {
202 expr {[file size test.db-wal] > (1024 * 1200)}
203} 1
204
205do_test 6.1 { sqlite3rbu_internal_test } {}
206
207finish_test