Compute the correct column name even if the column identifier is the
[sqlite.git] / ext / rbu / rbu3.test
blob995b3123fd0eec51b59842797d74efd128907732
1 # 2014 August 30
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 if {![info exists testdir]} {
14   set testdir [file join [file dirname [info script]] .. .. test]
16 source $testdir/tester.tcl
17 set ::testprefix rbu3
20 # Run the RBU in file $rbu on target database $target until completion.
22 proc run_rbu {target rbu} {
23   sqlite3rbu rbu $target $rbu
24   while { [rbu step]=="SQLITE_OK" } {}
25   rbu close
28 forcedelete test.db-oal rbu.db
29 db close
30 sqlite3_shutdown
31 sqlite3_config_uri 1
32 reset_db
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.
38 do_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 } {}
43 do_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}
54 do_execsql_test 1.2 {
55   PRAGMA integrity_check;
56 } {ok}
58 #--------------------------------------------------------------------
59 # Test that NULL values may not be inserted into INTEGER PRIMARY KEY
60 # columns.
62 forcedelete rbu.db
63 reset_db
65 do_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 } {}
70 foreach {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   }
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}}
89   do_execsql_test 2.1.2 {
90     PRAGMA integrity_check;
91   } {ok}
94 #--------------------------------------------------------------------
95 # Test that missing columns are detected.
97 forcedelete rbu.db
98 reset_db
100 do_execsql_test 2.0 {
101   CREATE TABLE x1(a INTEGER PRIMARY KEY, b, c);
102   CREATE INDEX i1 ON x1(b, c);
103 } {}
105 do_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}}
115 do_execsql_test 2.2 {
116   PRAGMA integrity_check;
117 } {ok}
119 # Also extra columns.
121 do_execsql_test 2.3 {
122   CREATE TABLE x2(a INTEGER PRIMARY KEY, b, c);
123   CREATE INDEX i2 ON x2(b, c);
124 } {}
126 do_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}
137 do_execsql_test 2.5 {
138   PRAGMA integrity_check;
139 } {ok}
142 #-------------------------------------------------------------------------
143 # Test that sqlite3rbu_create_vfs() returns an error if the requested 
144 # parent VFS is unknown.
146 # And that nothing disasterous happens if a VFS name passed to
147 # sqlite3rbu_destroy_vfs() is unknown or not an RBU vfs.
149 do_test 3.1 {
150   list [catch {sqlite3rbu_create_vfs xyz nosuchparent} msg] $msg
151 } {1 SQLITE_NOTFOUND}
153 do_test 3.2 {
154   sqlite3rbu_destroy_vfs nosuchvfs
155   sqlite3rbu_destroy_vfs unix
156   sqlite3rbu_destroy_vfs win32
157 } {}
159 #-------------------------------------------------------------------------
160 # Test that it is an error to specify an explicit VFS that does not 
161 # include rbu VFS functionality.
163 do_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}
168 do_test 4.2 {
169   list [catch { rbu close } msg] $msg
170 } {1 {SQLITE_ERROR - rbu vfs not found}}
171 tvfs delete
173 #-------------------------------------------------------------------------
174 # Test a large rbu update to ensure that wal_autocheckpoint does not get
175 # in the way.
177 forcedelete rbu.db
178 reset_db
179 do_execsql_test 5.1 {
180   CREATE TABLE x1(a, b, c, PRIMARY KEY(a)) WITHOUT ROWID;
181   CREATE INDEX i1 ON x1(a);
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;
193 do_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}
199 do_test 5.3 {
200   expr {[file size test.db-wal] > (1024 * 1200)}
201 } 1
203 do_test 6.1 { sqlite3rbu_internal_test } {}
205 finish_test