1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# 2014 August 16
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
#
if {![info exists testdir]} {
set testdir [file join [file dirname [info script]] .. .. test]
}
source [file join [file dirname [info script]] session_common.tcl]
source $testdir/tester.tcl
ifcapable !session {finish_test; return}
set testprefix sessionC
#-------------------------------------------------------------------------
# Test the outcome of a DELETE operation made as part of applying a
# changeset failing with SQLITE_CONSTRAINT. This may happen if an
# ON DELETE RESTRICT foreign key action is triggered, or if a trigger
# program raises a constraint somehow.
#
# UPDATE: The above is no longer true, as "PRAGMA defer_foreign_keys"
# now disables "RESTRICT" processing. The test below has been rewritten
# to use a trigger instead of a foreign key to test this case.
#
do_execsql_test 1.0 {
PRAGMA foreign_keys = 1;
CREATE TABLE p(a PRIMARY KEY, b, c);
CREATE TABLE c(d PRIMARY KEY, e /* REFERENCES p ON DELETE RESTRICT */);
CREATE TRIGGER restrict_trig BEFORE DELETE ON p BEGIN
SELECT raise(ABORT, 'error!') FROM c WHERE e=old.a;
END;
INSERT INTO p VALUES('one', 1, 1);
INSERT INTO p VALUES('two', 2, 2);
INSERT INTO p VALUES('three', 3, 3);
INSERT INTO c VALUES(1, 'one');
INSERT INTO c VALUES(3, 'three');
}
do_test 1.1 {
execsql BEGIN
set C [changeset_from_sql {
INSERT INTO c VALUES(4, 'one');
DELETE FROM p WHERE a='two';
}]
execsql ROLLBACK
execsql {
INSERT INTO c VALUES(2, 'two');
}
} {}
do_test 1.2.1 {
proc xConflict {args} { return "ABORT" }
catch { sqlite3changeset_apply db $C xConflict } msg
set msg
} {SQLITE_ABORT}
do_execsql_test 1.2.2 { SELECT * FROM c } {1 one 3 three 2 two}
do_test 1.3.1 {
proc xConflict {args} { return "OMIT" }
catch { sqlite3changeset_apply db $C xConflict } msg
set msg
} {}
do_execsql_test 1.3.2 { SELECT * FROM c } {1 one 3 three 2 two 4 one}
do_execsql_test 1.3.3 {
SELECT * FROM p;
} {one 1 1 two 2 2 three 3 3}
#-------------------------------------------------------------------------
# Test that concatenating a changeset with a patchset does not work.
# Any attempt to do so returns SQLITE_ERROR.
#
reset_db
do_execsql_test 2.0 {
CREATE TABLE x1(t, v PRIMARY KEY);
INSERT INTO x1 VALUES(12, 55);
INSERT INTO x1 VALUES(55, 14);
}
do_test 2.1 {
execsql BEGIN
sqlite3session S1 db main
S1 attach *
execsql {
UPDATE x1 SET t=13 WHERE v=55;
INSERT INTO x1 VALUES(99, 123);
}
set patchset [S1 patchset]
S1 delete
sqlite3session S1 db main
S1 attach *
execsql {
UPDATE x1 SET t=56 WHERE v=14;
INSERT INTO x1 VALUES(22, 998);
}
set changeset [S1 changeset]
S1 delete
execsql ROLLBACK
} {}
do_test 2.2 {
set rc [catch { sqlite3changeset_concat $patchset $changeset } msg]
list $rc $msg
} {1 SQLITE_ERROR}
do_test 2.3 {
set rc [catch { sqlite3changeset_concat $changeset $patchset } msg]
list $rc $msg
} {1 SQLITE_ERROR}
do_test 2.4 {
set rc [catch { sqlite3changeset_concat {} $patchset } msg]
list $rc $msg
} [list 0 $patchset]
do_test 2.5 {
set rc [catch { sqlite3changeset_concat $patchset {} } msg]
list $rc $msg
} [list 0 $patchset]
do_test 2.6 {
set rc [catch { sqlite3changeset_concat {} $changeset } msg]
list $rc $msg
} [list 0 $changeset]
do_test 2.7 {
set rc [catch { sqlite3changeset_concat $changeset {} } msg]
list $rc $msg
} [list 0 $changeset]
do_test 2.8 {
set rc [catch { sqlite3changeset_concat {} {} } msg]
list $rc $msg
} [list 0 {}]
#-------------------------------------------------------------------------
# Test that the xFilter argument to sqlite3changeset_apply() works.
#
reset_db
do_execsql_test 3.0 {
CREATE TABLE t1(a PRIMARY KEY, b);
CREATE TABLE t2(a PRIMARY KEY, b);
CREATE TABLE t3(a PRIMARY KEY, b);
}
do_test 3.1 {
execsql BEGIN
set changeset [changeset_from_sql {
INSERT INTO t1 VALUES(1, 1);
INSERT INTO t2 VALUES(2, 2);
INSERT INTO t3 VALUES(3, 3);
}]
execsql ROLLBACK
} {}
do_test 3.2 {
proc xFilter {zName} {
if {$zName == "t1"} { return 1 }
return 0
}
sqlite3changeset_apply db $changeset noop xFilter
execsql {
SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
}
} {1 1}
do_test 3.3 {
proc xFilter {zName} {
if {$zName == "t3"} { return 1 }
return 0
}
sqlite3changeset_apply db $changeset noop xFilter
execsql {
SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
}
} {1 1 3 3}
finish_test
|