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 #***********************************************************************
11 # This file focuses on the sqlite3session_diff() function.
14 if {![info exists testdir]} {
15 set testdir [file join [file dirname [info script]] .. .. test]
17 source [file join [file dirname [info script]] session_common.tcl]
18 source $testdir/tester.tcl
19 ifcapable !session {finish_test; return}
21 set testprefix sessionD
23 proc scksum {db dbname} {
25 if {$dbname=="temp"} {
26 set master sqlite_temp_master
28 set master $dbname.sqlite_master
31 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
32 set txt [$db eval "SELECT * FROM $master ORDER BY type,name,sql"]
35 db eval "PRAGMA $dbname.table_info = $tab" x {
36 lappend cols "quote($x(name))"
38 set cols [join $cols ,]
39 append txt [db eval "SELECT $cols FROM $tab ORDER BY $cols"]
44 # Ensure that the diff produced by comparing the current contents of [db]
45 # with itself is empty.
46 proc do_empty_diff_test {tn} {
48 forcecopy test.db test.db2
50 execsql { ATTACH 'test.db2' AS aux }
51 sqlite3session S db main
52 foreach tbl [db eval {SELECT name FROM sqlite_master WHERE type='table'}] {
60 uplevel [list do_test $tn {string length $::C} 0]
66 CREATE TABLE t2(a PRIMARY KEY, b);
67 INSERT INTO t2 VALUES(1, 'one');
68 INSERT INTO t2 VALUES(2, 'two');
70 ATTACH 'test.db2' AS aux;
71 CREATE TABLE aux.t2(a PRIMARY KEY, b);
75 sqlite3session S db main
84 sqlite3changeset_apply db2 $C ""
86 db eval { SELECT * FROM aux.t2 }
90 CREATE TABLE aux.t1(x, y, PRIMARY KEY(y));
91 CREATE TABLE t1(x, y, PRIMARY KEY(y));
93 INSERT INTO t1 VALUES(1, 2);
94 INSERT INTO t1 VALUES(NULL, 'xyz');
95 INSERT INTO t1 VALUES(4.5, 5.5);
99 CREATE TABLE aux.t1(x, y, PRIMARY KEY(y));
100 CREATE TABLE t1(x, y, PRIMARY KEY(y));
102 INSERT INTO aux.t1 VALUES(1, 2);
103 INSERT INTO aux.t1 VALUES(NULL, 'xyz');
104 INSERT INTO aux.t1 VALUES(4.5, 5.5);
108 CREATE TABLE aux.t1(a PRIMARY KEY, b TEXT);
109 CREATE TABLE t1(a PRIMARY KEY, b TEXT);
111 INSERT INTO aux.t1 VALUES(1, 'one');
112 INSERT INTO aux.t1 VALUES(2, 'two');
113 INSERT INTO aux.t1 VALUES(3, 'three');
115 INSERT INTO t1 VALUES(1, 'I');
116 INSERT INTO t1 VALUES(2, 'two');
117 INSERT INTO t1 VALUES(3, 'III');
121 CREATE TABLE aux.t1(a, b, c, d, PRIMARY KEY(c, b, a));
122 CREATE TABLE t1(a, b, c, d, PRIMARY KEY(c, b, a));
124 INSERT INTO t1 VALUES('hvkzyipambwdqlvwv','',-458331.50,X'DA51ED5E84');
125 INSERT INTO t1 VALUES(X'C5C6B5DD','jjxrath',40917,830244);
126 INSERT INTO t1 VALUES(-204877.54,X'1704C253D5F3AFA8',155120.88,NULL);
128 VALUES('ckmqmzoeuvxisxqy',X'EB5A5D3A1DD22FD1','tidhjcbvbppdt',-642987.37);
129 INSERT INTO t1 VALUES(-851726,-161992,-469943,-159541);
130 INSERT INTO t1 VALUES(X'4A6A667F858938',185083,X'7A',NULL);
132 INSERT INTO aux.t1 VALUES(415075.74,'auawczkb',X'',X'57B4FAAF2595');
133 INSERT INTO aux.t1 VALUES(727637,711560,-181340,'hphuo');
135 VALUES(-921322.81,662959,'lvlgwdgxaurr','ajjrzrbhqflsutnymgc');
136 INSERT INTO aux.t1 VALUES(-146061,-377892,X'4E','gepvpvvuhszpxabbb');
137 INSERT INTO aux.t1 VALUES(-851726,-161992,-469943,-159541);
138 INSERT INTO aux.t1 VALUES(X'4A6A667F858938',185083,X'7A',NULL);
139 INSERT INTO aux.t1 VALUES(-204877.54,X'1704C253D5F3AFA8',155120.88, 4);
141 VALUES('ckmqmzoeuvxisxqy',X'EB5A5D3A1DD22FD1','tidgtsplhjcbvbppdt',-642987.3);
145 do_execsql_test 3.0 {
146 CREATE TABLE t1(a, b, c, PRIMARY KEY(a));
147 INSERT INTO t1 VALUES(1, 2, 3);
148 INSERT INTO t1 VALUES(4, 5, 6);
149 INSERT INTO t1 VALUES(7, 8, 9);
151 CREATE TABLE t2(a, b, c, PRIMARY KEY(a, b));
152 INSERT INTO t2 VALUES(1, 2, 3);
153 INSERT INTO t2 VALUES(4, 5, 6);
154 INSERT INTO t2 VALUES(7, 8, 9);
156 CREATE TABLE t3(a, b, c, PRIMARY KEY(a, b, c));
157 INSERT INTO t3 VALUES(1, 2, 3);
158 INSERT INTO t3 VALUES(4, 5, 6);
159 INSERT INTO t3 VALUES(7, 8, 9);
161 do_empty_diff_test 3.1
164 #-------------------------------------------------------------------------
165 # Test some error cases:
167 # 1) schema mismatches between the two dbs, and
168 # 2) tables with no primary keys. This is not actually an error, but
169 # should not add any changes to the session object.
173 do_execsql_test 4.0 {
174 ATTACH 'test.db2' AS ixua;
175 CREATE TABLE ixua.t1(a, b, c);
176 CREATE TABLE main.t1(a, b, c);
177 INSERT INTO main.t1 VALUES(1, 2, 3);
179 CREATE TABLE ixua.t2(a PRIMARY KEY, b, c);
180 CREATE TABLE main.t2(a PRIMARY KEY, b, x);
184 sqlite3session S db main
186 list [catch { S diff ixua t1 } msg] $msg
189 string length [S changeset]
194 sqlite3session S db main
196 list [catch { S diff ixua t2 } msg] $msg
197 } {1 {SQLITE_SCHEMA - table schemas do not match}}
201 sqlite3session S db main
203 execsql { CREATE TABLE t4(i PRIMARY KEY, b) }
204 list [catch { S diff ixua t4 } msg] $msg
205 } {1 {SQLITE_SCHEMA - table schemas do not match}}
207 do_catchsql_test 4.3.2 {
208 SELECT * FROM ixua.t4;
209 } {1 {no such table: ixua.t4}}
212 sqlite3session S db main
213 S attach sqlite_stat1
215 execsql { DROP TABLE ixua.sqlite_stat1 }
216 list [catch { S diff ixua sqlite_stat1 } msg] $msg
217 } {1 {SQLITE_SCHEMA - table schemas do not match}}
219 do_catchsql_test 4.4.2 {
220 SELECT * FROM ixua.sqlite_stat1;
221 } {1 {no such table: ixua.sqlite_stat1}}
224 sqlite3session S db main
226 list [catch { S diff ixua t8 } msg] $msg
229 do_catchsql_test 4.5.2 {
230 SELECT * FROM ixua.i8;
231 } {1 {no such table: ixua.i8}}