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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
# 2018 March 14
#
# 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.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
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 sessionrebase
set ::lConflict [list]
proc xConflict {args} {
set res [lindex $::lConflict 0]
set ::lConflict [lrange $::lConflict 1 end]
return $res
}
#-------------------------------------------------------------------------
# The following test cases - 1.* - test that the rebase blobs output by
# sqlite3_changeset_apply_v2 look correct in some simple cases. The blob
# is itself a changeset, containing records determined as follows:
#
# * For each conflict resolved with REPLACE, the rebase blob contains
# a DELETE record. All fields other than the PK fields are undefined.
#
# * For each conflict resolved with OMIT, the rebase blob contains an
# INSERT record. For an INSERT or UPDATE operation, the indirect flag
# is clear and all updated fields are defined. For a DELETE operation,
# the indirect flag is set and all non-PK fields left undefined.
#
proc do_apply_v2_test {tn sql modsql conflict_handler res} {
execsql BEGIN
sqlite3session S db main
S attach *
execsql $sql
set changeset [S changeset]
S delete
execsql ROLLBACK
execsql BEGIN
execsql $modsql
set ::lConflict $conflict_handler
set blob [sqlite3changeset_apply_v2 db $changeset xConflict]
execsql ROLLBACK
uplevel [list do_test $tn [list changeset_to_list $blob] [list {*}$res]]
}
set ::lConflict [list]
proc xConflict {args} {
set res [lindex $::lConflict 0]
set ::lConflict [lrange $::lConflict 1 end]
return $res
}
# Take a copy of database test.db in file test.db2. Execute $sql1
# against test.db and $sql2 against test.db2. Capture a changeset
# for each. Then send the test.db2 changeset to test.db and apply
# it with the conflict handlers in $conflict_handler. Patch the
# test.db changeset and then execute it against test.db2. Test that
# the two databases come out the same.
#
proc do_rebase_test {tn sql1 sql2 conflict_handler {testsql ""} {testres ""}} {
for {set i 1} {$i <= 2} {incr i} {
forcedelete test.db2 test.db2-journal test.db2-wal
forcecopy test.db test.db2
sqlite3 db2 test.db2
db eval BEGIN
sqlite3session S1 db main
S1 object_config rowid 1
S1 attach *
execsql $sql1 db
set c1 [S1 changeset]
S1 delete
if {$i==1} {
sqlite3session S2 db2 main
S2 object_config rowid 1
S2 attach *
execsql $sql2 db2
set c2 [S2 changeset]
S2 delete
} else {
set c2 [list]
foreach sql [split $sql2 ";"] {
if {[string is space $sql]} continue
sqlite3session S2 db2 main
S2 object_config rowid 1
S2 attach *
execsql $sql db2
lappend c2 [S2 changeset]
S2 delete
}
}
set ::lConflict $conflict_handler
set rebase [list]
if {$i==1} {
lappend rebase [sqlite3changeset_apply_v2 db $c2 xConflict]
} else {
foreach c $c2 {
#puts "apply_v2: [changeset_to_list $c]"
lappend rebase [sqlite3changeset_apply_v2 db $c xConflict]
}
#puts "llength: [llength $rebase]"
}
#if {$tn=="2.1.4"} { puts [changeset_to_list $rebase] ; breakpoint }
#puts [changeset_to_list [lindex $rebase 0]] ; breakpoint
#puts [llength $rebase]
sqlite3rebaser_create R
foreach r $rebase {
#puts [changeset_to_list $r]
R configure $r
}
set c1r [R rebase $c1]
R delete
#if {$tn=="2.1.4"} { puts [changeset_to_list $c1r] }
sqlite3changeset_apply_v2 db2 $c1r xConflictAbort
if {[string range $tn end end]!="*"} {
uplevel [list do_test $tn.$i.1 [list compare_db db db2] {}]
}
db2 close
if {$testsql!=""} {
uplevel [list do_execsql_test $tn.$i.2 $testsql $testres]
}
db eval ROLLBACK
}
}
do_execsql_test 1.0 {
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
INSERT INTO t1 VALUES(1, 'value A');
}
do_apply_v2_test 1.1.1 {
UPDATE t1 SET b = 'value B' WHERE a=1;
} {
UPDATE t1 SET b = 'value C' WHERE a=1;
} {
OMIT
} {
{INSERT t1 0 X. {} {i 1 t {value B}}}
}
do_apply_v2_test 1.1.2 {
UPDATE t1 SET b = 'value B' WHERE a=1;
} {
UPDATE t1 SET b = 'value C' WHERE a=1;
} {
REPLACE
} {
{INSERT t1 1 X. {} {i 1 t {value B}}}
}
do_apply_v2_test 1.2.1 {
INSERT INTO t1 VALUES(2, 'first');
} {
INSERT INTO t1 VALUES(2, 'second');
} {
OMIT
} {
{INSERT t1 0 X. {} {i 2 t first}}
}
do_apply_v2_test 1.2.2 {
INSERT INTO t1 VALUES(2, 'first');
} {
INSERT INTO t1 VALUES(2, 'second');
} {
REPLACE
} {
{INSERT t1 1 X. {} {i 2 t first}}
}
do_apply_v2_test 1.3.1 {
DELETE FROM t1 WHERE a=1;
} {
UPDATE t1 SET b='value D' WHERE a=1;
} {
OMIT
} {
{DELETE t1 0 X. {i 1 t {value A}} {}}
}
do_apply_v2_test 1.3.2 {
DELETE FROM t1 WHERE a=1;
} {
UPDATE t1 SET b='value D' WHERE a=1;
} {
REPLACE
} {
{DELETE t1 1 X. {i 1 t {value A}} {}}
}
#-------------------------------------------------------------------------
# Test cases 2.* - simple tests of rebasing actual changesets.
#
# 2.1.1 - 1u2u1r
# 2.1.2 - 1u2u2r
# 2.1.3 - 1d2d
# 2.1.4 - 1d2u1r
# 2.1.5 - 1d2u2r !!
# 2.1.6 - 1u2d1r
# 2.1.7 - 1u2d2r
#
# 2.1.8 - 1i2i2r
# 2.1.9 - 1i2i1r
#
proc xConflictAbort {args} {
return "ABORT"
}
reset_db
do_execsql_test 2.1.0 {
CREATE TABLE t1 (a INTEGER PRIMARY KEY, b TEXT);
INSERT INTO t1 VALUES(1, 'one');
INSERT INTO t1 VALUES(2, 'two');
INSERT INTO t1 VALUES(3, 'three');
}
do_rebase_test 2.1.1 {
UPDATE t1 SET b = 'two.1' WHERE a=2
} {
UPDATE t1 SET b = 'two.2' WHERE a=2;
} {
OMIT
} { SELECT * FROM t1 } {1 one 2 two.1 3 three}
do_rebase_test 2.1.2 {
UPDATE t1 SET b = 'two.1' WHERE a=2
} {
UPDATE t1 SET b = 'two.2' WHERE a=2;
} {
REPLACE
} { SELECT * FROM t1 } {1 one 2 two.2 3 three}
do_rebase_test 2.1.3 {
DELETE FROM t1 WHERE a=3
} {
DELETE FROM t1 WHERE a=3;
} {
OMIT
} { SELECT * FROM t1 } {1 one 2 two}
do_rebase_test 2.1.4 {
DELETE FROM t1 WHERE a=1
} {
UPDATE t1 SET b='one.2' WHERE a=1
} {
OMIT
} { SELECT * FROM t1 } {2 two 3 three}
#do_rebase_test 2.1.5 {
# DELETE FROM t1 WHERE a=1;
#} {
# UPDATE t1 SET b='one.2' WHERE a=1
#} {
# REPLACE
#} { SELECT * FROM t1 } {2 two 3 three}
do_rebase_test 2.1.6 {
UPDATE t1 SET b='three.1' WHERE a=3
} {
DELETE FROM t1 WHERE a=3;
} {
OMIT
} { SELECT * FROM t1 } {1 one 2 two 3 three.1}
do_rebase_test 2.1.7 {
UPDATE t1 SET b='three.1' WHERE a=3
} {
DELETE FROM t1 WHERE a=3;
} {
REPLACE
} { SELECT * FROM t1 } {1 one 2 two}
do_rebase_test 2.1.8 {
INSERT INTO t1 VALUES(4, 'four.1')
} {
INSERT INTO t1 VALUES(4, 'four.2');
} {
REPLACE
} { SELECT * FROM t1 } {1 one 2 two 3 three 4 four.2}
do_rebase_test 2.1.9 {
INSERT INTO t1 VALUES(4, 'four.1')
} {
INSERT INTO t1 VALUES(4, 'four.2');
} {
OMIT
} { SELECT * FROM t1 } {1 one 2 two 3 three 4 four.1}
do_execsql_test 2.2.0 {
CREATE TABLE t2(x, y, z PRIMARY KEY);
INSERT INTO t2 VALUES('i', 'a', 'A');
INSERT INTO t2 VALUES('ii', 'b', 'B');
INSERT INTO t2 VALUES('iii', 'c', 'C');
CREATE TABLE t3(a INTEGER PRIMARY KEY, b, c);
INSERT INTO t3 VALUES(-1, 'z', 'Z');
INSERT INTO t3 VALUES(-2, 'y', 'Y');
}
do_rebase_test 2.2.1 {
UPDATE t2 SET x=1 WHERE z='A'
} {
UPDATE t2 SET y='one' WHERE z='A';
} {
} { SELECT * FROM t2 WHERE z='A' } { 1 one A }
do_rebase_test 2.2.2 {
UPDATE t2 SET x=1, y='one' WHERE z='B'
} {
UPDATE t2 SET y='two' WHERE z='B';
} {
REPLACE
} { SELECT * FROM t2 WHERE z='B' } { 1 two B }
do_rebase_test 2.2.3 {
UPDATE t2 SET x=1, y='one' WHERE z='B'
} {
UPDATE t2 SET y='two' WHERE z='B';
} {
OMIT
} { SELECT * FROM t2 WHERE z='B' } { 1 one B }
reset_db
do_execsql_test 2.3.0 {
CREATE TABLE t1 (b TEXT);
INSERT INTO t1(rowid, b) VALUES(1, 'one');
INSERT INTO t1(rowid, b) VALUES(2, 'two');
INSERT INTO t1(rowid, b) VALUES(3, 'three');
}
do_rebase_test 2.3.1 {
UPDATE t1 SET b = 'two.1' WHERE rowid=2
} {
UPDATE t1 SET b = 'two.2' WHERE rowid=2;
} {
OMIT
} { SELECT rowid, * FROM t1 } {1 one 2 two.1 3 three}
do_rebase_test 2.3.2 {
UPDATE t1 SET b = 'two.1' WHERE rowid=2
} {
UPDATE t1 SET b = 'two.2' WHERE rowid=2;
} {
REPLACE
} { SELECT rowid, * FROM t1 } {1 one 2 two.2 3 three}
do_rebase_test 2.3.3 {
DELETE FROM t1 WHERE rowid=3
} {
DELETE FROM t1 WHERE rowid=3;
} {
OMIT
} { SELECT rowid, * FROM t1 } {1 one 2 two}
do_rebase_test 2.3.4 {
DELETE FROM t1 WHERE rowid=1
} {
UPDATE t1 SET b='one.2' WHERE rowid=1
} {
OMIT
} { SELECT rowid, * FROM t1 } {2 two 3 three}
do_rebase_test 2.3.6 {
UPDATE t1 SET b='three.1' WHERE rowid=3
} {
DELETE FROM t1 WHERE rowid=3;
} {
OMIT
} { SELECT rowid, * FROM t1 } {1 one 2 two 3 three.1}
do_rebase_test 2.3.7 {
UPDATE t1 SET b='three.1' WHERE rowid=3
} {
DELETE FROM t1 WHERE rowid=3;
} {
REPLACE
} { SELECT rowid, * FROM t1 } {1 one 2 two}
do_rebase_test 2.3.8 {
INSERT INTO t1(rowid, b) VALUES(4, 'four.1')
} {
INSERT INTO t1(rowid, b) VALUES(4, 'four.2');
} {
REPLACE
} { SELECT rowid, * FROM t1 } {1 one 2 two 3 three 4 four.2}
do_rebase_test 2.3.9 {
INSERT INTO t1(rowid, b) VALUES(4, 'four.1')
} {
INSERT INTO t1(rowid, b) VALUES(4, 'four.2');
} {
OMIT
} { SELECT rowid, * FROM t1 } {1 one 2 two 3 three 4 four.1}
#-------------------------------------------------------------------------
reset_db
do_execsql_test 3.0 {
CREATE TABLE t3(a, b, c, PRIMARY KEY(b, c));
CREATE TABLE abcdefghijkl(x PRIMARY KEY, y, z);
INSERT INTO t3 VALUES(1, 2, 3);
INSERT INTO t3 VALUES(4, 2, 5);
INSERT INTO t3 VALUES(7, 2, 9);
INSERT INTO abcdefghijkl VALUES('a', 'b', 'c');
INSERT INTO abcdefghijkl VALUES('d', 'e', 'f');
INSERT INTO abcdefghijkl VALUES('g', 'h', 'i');
}
breakpoint
# do_rebase_test 3.6.tn {
# UPDATE abcdefghijkl SET z='X', y='X' WHERE x='d';
# } {
# UPDATE abcdefghijkl SET y=1 WHERE x='d';
# UPDATE abcdefghijkl SET z=1 WHERE x='d';
# } [list REPLACE REPLACE REPLACE]
foreach {tn p} {
1 OMIT 2 REPLACE
} {
do_rebase_test 3.1.$tn {
INSERT INTO t3 VALUES(1, 1, 1);
UPDATE abcdefghijkl SET y=2;
} {
INSERT INTO t3 VALUES(4, 1, 1);
DELETE FROM abcdefghijkl;
} [list $p $p $p $p $p $p $p $p]
do_rebase_test 3.2.$tn {
INSERT INTO abcdefghijkl SELECT * FROM t3;
UPDATE t3 SET b=b+1;
} {
INSERT INTO t3 VALUES(3, 3, 3);
INSERT INTO abcdefghijkl SELECT * FROM t3;
} [list $p $p $p $p $p $p $p $p]
do_rebase_test 3.3.$tn {
INSERT INTO abcdefghijkl VALUES(22, 23, 24);
} {
INSERT INTO abcdefghijkl VALUES(22, 25, 26);
UPDATE abcdefghijkl SET y=400 WHERE x=22;
} [list $p $p $p $p $p $p $p $p]
do_rebase_test 3.4.$tn {
INSERT INTO abcdefghijkl VALUES(22, 23, 24);
} {
INSERT INTO abcdefghijkl VALUES(22, 25, 26);
UPDATE abcdefghijkl SET y=400 WHERE x=22;
} [list REPLACE $p]
do_rebase_test 3.5.$tn* {
UPDATE abcdefghijkl SET y='X' WHERE x='d';
} {
DELETE FROM abcdefghijkl WHERE x='d';
INSERT INTO abcdefghijkl VALUES('d', NULL, NULL);
} [list $p $p $p]
do_rebase_test 3.5.$tn {
UPDATE abcdefghijkl SET y='X' WHERE x='d';
} {
DELETE FROM abcdefghijkl WHERE x='d';
INSERT INTO abcdefghijkl VALUES('d', NULL, NULL);
} [list REPLACE $p $p]
do_rebase_test 3.6.$tn {
UPDATE abcdefghijkl SET z='X', y='X' WHERE x='d';
} {
UPDATE abcdefghijkl SET y=1 WHERE x='d';
UPDATE abcdefghijkl SET z=1 WHERE x='d';
} [list REPLACE $p $p]
}
#-------------------------------------------------------------------------
# Check that apply_v2() does not create a rebase buffer for a patchset.
# And that it is not possible to rebase a patchset.
#
do_execsql_test 4.0 {
CREATE TABLE t5(o PRIMARY KEY, p, q);
INSERT INTO t5 VALUES(1, 2, 3);
INSERT INTO t5 VALUES(4, 5, 6);
}
foreach {tn cmd rebasable} {
1 patchset 0
2 changeset 1
} {
proc xConflict {args} { return "OMIT" }
do_test 4.1.$tn {
execsql {
BEGIN;
DELETE FROM t5 WHERE o=4;
}
sqlite3session S db main
S attach *
execsql {
INSERT INTO t5 VALUES(4, 'five', 'six');
}
set P [S $cmd]
S delete
execsql ROLLBACK;
set ::rebase [sqlite3changeset_apply_v2 db $P xConflict]
expr [llength $::rebase]>0
} $rebasable
}
foreach {tn cmd rebasable} {
1 patchset 0
2 changeset 1
} {
do_test 4.2.$tn {
sqlite3session S db main
S attach *
execsql {
INSERT INTO t5 VALUES(5+$tn, 'five', 'six');
}
set P [S $cmd]
S delete
sqlite3rebaser_create R
R configure $::rebase
expr [catch {R rebase $P}]==0
} $rebasable
catch { R delete }
}
finish_test
|