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