sessionB.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. # 2014 August 16
  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. # This file implements regression tests for sessions SQLite extension.
  13. # Specifically, this file contains tests for "patchset" changes.
  14. #
  15. if {![info exists testdir]} {
  16. set testdir [file join [file dirname [info script]] .. .. test]
  17. }
  18. source [file join [file dirname [info script]] session_common.tcl]
  19. source $testdir/tester.tcl
  20. ifcapable !session {finish_test; return}
  21. set testprefix sessionB
  22. #
  23. # 1.*: Test that the blobs returned by the session_patchset() API are
  24. # as expected. Also the sqlite3_changeset_iter functions.
  25. #
  26. # 2.*: Test that patchset blobs are handled by sqlite3changeset_apply().
  27. #
  28. # 3.*: Test that sqlite3changeset_invert() works with patchset blobs.
  29. # Correct behaviour is to return SQLITE_CORRUPT.
  30. proc do_sql2patchset_test {tn sql res} {
  31. sqlite3session S db main
  32. S attach *
  33. execsql $sql
  34. uplevel [list do_patchset_test $tn S $res]
  35. S delete
  36. }
  37. #-------------------------------------------------------------------------
  38. # Run simple tests of the _patchset() API.
  39. #
  40. do_execsql_test 1.0 {
  41. CREATE TABLE t1(a, b, c, d, PRIMARY KEY(d, a));
  42. INSERT INTO t1 VALUES(1, 2, 3, 4);
  43. INSERT INTO t1 VALUES(5, 6, 7, 8);
  44. INSERT INTO t1 VALUES(9, 10, 11, 12);
  45. }
  46. do_test 1.1 {
  47. sqlite3session S db main
  48. S attach t1
  49. execsql {
  50. INSERT INTO t1 VALUES('w', 'x', 'y', 'z');
  51. DELETE FROM t1 WHERE d=4;
  52. UPDATE t1 SET c = 14 WHERE a=5;
  53. }
  54. } {}
  55. do_patchset_test 1.2 S {
  56. {UPDATE t1 0 X..X {i 5 {} {} {} {} i 8} {{} {} {} {} i 14 {} {}}}
  57. {INSERT t1 0 X..X {} {t w t x t y t z}}
  58. {DELETE t1 0 X..X {i 1 {} {} {} {} i 4} {}}
  59. }
  60. do_test 1.3 {
  61. S delete
  62. } {}
  63. do_sql2patchset_test 1.4 {
  64. DELETE FROM t1;
  65. } {
  66. {DELETE t1 0 X..X {i 5 {} {} {} {} i 8} {}}
  67. {DELETE t1 0 X..X {t w {} {} {} {} t z} {}}
  68. {DELETE t1 0 X..X {i 9 {} {} {} {} i 12} {}}
  69. }
  70. do_sql2patchset_test 1.5 {
  71. INSERT INTO t1 VALUES(X'61626364', NULL, NULL, 4.2);
  72. INSERT INTO t1 VALUES(4.2, NULL, NULL, X'61626364');
  73. } {
  74. {INSERT t1 0 X..X {} {f 4.2 n {} n {} b abcd}}
  75. {INSERT t1 0 X..X {} {b abcd n {} n {} f 4.2}}
  76. }
  77. do_sql2patchset_test 1.6 {
  78. UPDATE t1 SET b=45 WHERE typeof(a)=='blob';
  79. UPDATE t1 SET c='zzzz' WHERE typeof(a)!='blob';
  80. } {
  81. {UPDATE t1 0 X..X {f 4.2 {} {} {} {} b abcd} {{} {} {} {} t zzzz {} {}}}
  82. {UPDATE t1 0 X..X {b abcd {} {} {} {} f 4.2} {{} {} i 45 {} {} {} {}}}
  83. }
  84. do_sql2patchset_test 1.7 {
  85. UPDATE t1 SET b='xyz' WHERE typeof(a)=='blob';
  86. UPDATE t1 SET c='xyz' WHERE typeof(a)!='blob';
  87. UPDATE t1 SET b=45 WHERE typeof(a)=='blob';
  88. UPDATE t1 SET c='zzzz' WHERE typeof(a)!='blob';
  89. } {
  90. }
  91. do_sql2patchset_test 1.8 {
  92. DELETE FROM t1;
  93. } {
  94. {DELETE t1 0 X..X {f 4.2 {} {} {} {} b abcd} {}}
  95. {DELETE t1 0 X..X {b abcd {} {} {} {} f 4.2} {}}
  96. }
  97. #-------------------------------------------------------------------------
  98. # Run simple tests of _apply() with patchset objects.
  99. #
  100. reset_db
  101. proc noop {args} { error $args }
  102. proc exec_rollback_replay {sql} {
  103. sqlite3session S db main
  104. S attach *
  105. execsql BEGIN
  106. execsql $sql
  107. set patchset [S patchset]
  108. S delete
  109. execsql ROLLBACK
  110. sqlite3changeset_apply db $patchset noop
  111. }
  112. do_execsql_test 2.0 {
  113. CREATE TABLE t2(a, b, c, d, PRIMARY KEY(b,c));
  114. CREATE TABLE t3(w, x, y, z, PRIMARY KEY(w));
  115. }
  116. do_test 2.1 {
  117. exec_rollback_replay {
  118. INSERT INTO t2 VALUES(1, 2, 3, 4);
  119. INSERT INTO t2 VALUES('w', 'x', 'y', 'z');
  120. }
  121. execsql { SELECT * FROM t2 }
  122. } {1 2 3 4 w x y z}
  123. do_test 2.2 {
  124. exec_rollback_replay {
  125. DELETE FROM t2 WHERE a=1;
  126. UPDATE t2 SET d = 'a';
  127. }
  128. execsql { SELECT * FROM t2 }
  129. } {w x y a}
  130. #-------------------------------------------------------------------------
  131. # sqlite3changeset_invert()
  132. #
  133. reset_db
  134. do_execsql_test 3.1 { CREATE TABLE t1(x PRIMARY KEY, y) }
  135. do_test 3.2 {
  136. sqlite3session S db main
  137. S attach *
  138. execsql { INSERT INTO t1 VALUES(1, 2) }
  139. set patchset [S patchset]
  140. S delete
  141. list [catch { sqlite3changeset_invert $patchset } msg] [set msg]
  142. } {1 SQLITE_CORRUPT}
  143. #-------------------------------------------------------------------------
  144. # sqlite3changeset_concat()
  145. #
  146. reset_db
  147. proc do_patchconcat_test {tn args} {
  148. set bRevert 0
  149. if {[lindex $args 0] == "-revert"} {
  150. set bRevert 1
  151. set args [lrange $args 1 end]
  152. }
  153. set nSql [expr [llength $args]-1]
  154. set res [lindex $args $nSql]
  155. set patchlist [list]
  156. execsql BEGIN
  157. if {$bRevert} { execsql { SAVEPOINT x } }
  158. foreach sql [lrange $args 0 end-1] {
  159. sqlite3session S db main
  160. S attach *
  161. execsql $sql
  162. lappend patchlist [S patchset]
  163. S delete
  164. if {$bRevert} { execsql { ROLLBACK TO x } }
  165. }
  166. execsql ROLLBACK
  167. set patch [lindex $patchlist 0]
  168. foreach p [lrange $patchlist 1 end] {
  169. set patch [sqlite3changeset_concat $patch $p]
  170. }
  171. set x [list]
  172. sqlite3session_foreach c $patch { lappend x $c }
  173. uplevel [list do_test $tn [list set {} $x] [list {*}$res]]
  174. }
  175. do_execsql_test 4.1.1 {
  176. CREATE TABLE t1(x PRIMARY KEY, y, z);
  177. }
  178. do_patchconcat_test 4.1.2 {
  179. INSERT INTO t1 VALUES(1, 2, 3);
  180. } {
  181. INSERT INTO t1 VALUES(4, 5, 6);
  182. } {
  183. {INSERT t1 0 X.. {} {i 1 i 2 i 3}}
  184. {INSERT t1 0 X.. {} {i 4 i 5 i 6}}
  185. }
  186. do_execsql_test 4.2.1 {
  187. INSERT INTO t1 VALUES(1, 2, 3);
  188. INSERT INTO t1 VALUES(4, 5, 6);
  189. }
  190. do_patchconcat_test 4.2.2 {
  191. UPDATE t1 SET z = 'abc' WHERE x=1
  192. } {
  193. UPDATE t1 SET z = 'def' WHERE x=4
  194. } {
  195. {UPDATE t1 0 X.. {i 1 {} {} {} {}} {{} {} {} {} t abc}}
  196. {UPDATE t1 0 X.. {i 4 {} {} {} {}} {{} {} {} {} t def}}
  197. }
  198. do_patchconcat_test 4.2.3 {
  199. DELETE FROM t1 WHERE x=1;
  200. } {
  201. DELETE FROM t1 WHERE x=4;
  202. } {
  203. {DELETE t1 0 X.. {i 1 {} {} {} {}} {}}
  204. {DELETE t1 0 X.. {i 4 {} {} {} {}} {}}
  205. }
  206. do_execsql_test 4.3.1 {
  207. CREATE TABLE t2(a, b, c, d, PRIMARY KEY(c, b));
  208. INSERT INTO t2 VALUES('.', 1, 1, '.');
  209. INSERT INTO t2 VALUES('.', 1, 2, '.');
  210. INSERT INTO t2 VALUES('.', 2, 1, '.');
  211. INSERT INTO t2 VALUES('.', 2, 2, '.');
  212. }
  213. # INSERT + INSERT
  214. do_patchconcat_test 4.3.2 -revert {
  215. INSERT INTO t2 VALUES('a', 'a', 'a', 'a');
  216. } {
  217. INSERT INTO t2 VALUES('b', 'a', 'a', 'b');
  218. } {
  219. {INSERT t2 0 .XX. {} {t a t a t a t a}}
  220. }
  221. # INSERT + DELETE
  222. do_patchconcat_test 4.3.3 {
  223. INSERT INTO t2 VALUES('a', 'a', 'a', 'a');
  224. } {
  225. DELETE FROM t2 WHERE c = 'a';
  226. } {}
  227. # INSERT + UPDATE
  228. do_patchconcat_test 4.3.4 {
  229. INSERT INTO t2 VALUES('a', 'a', 'a', 'a');
  230. } {
  231. UPDATE t2 SET d = 'b' WHERE c='a';
  232. } {
  233. {INSERT t2 0 .XX. {} {t a t a t a t b}}
  234. }
  235. # UPDATE + UPDATE
  236. do_patchconcat_test 4.3.5 {
  237. UPDATE t2 SET a = 'a' WHERE c=1 AND b=2;
  238. } {
  239. UPDATE t2 SET d = 'd' WHERE c=1 AND b=2;
  240. } {
  241. {UPDATE t2 0 .XX. {{} {} i 2 i 1 {} {}} {t a {} {} {} {} t d}}
  242. }
  243. # UPDATE + DELETE
  244. do_patchconcat_test 4.3.6 {
  245. UPDATE t2 SET a = 'a' WHERE c=1 AND b=2;
  246. } {
  247. DELETE FROM t2 WHERE c=1 AND b=2;
  248. } {
  249. {DELETE t2 0 .XX. {{} {} i 2 i 1 {} {}} {}}
  250. }
  251. # DELETE + INSERT
  252. do_patchconcat_test 4.3.7 {
  253. DELETE FROM t2 WHERE b=1;
  254. } {
  255. INSERT INTO t2 VALUES('x', 1, 2, '.');
  256. } {
  257. {DELETE t2 0 .XX. {{} {} i 1 i 1 {} {}} {}}
  258. {UPDATE t2 0 .XX. {{} {} i 1 i 2 {} {}} {t x {} {} {} {} t .}}
  259. }
  260. # DELETE + UPDATE
  261. do_patchconcat_test 4.3.8 -revert {
  262. DELETE FROM t2 WHERE b=1 AND c=2;
  263. } {
  264. UPDATE t2 SET a=5 WHERE b=1 AND c=2;
  265. } {
  266. {DELETE t2 0 .XX. {{} {} i 1 i 2 {} {}} {}}
  267. }
  268. # DELETE + UPDATE
  269. do_patchconcat_test 4.3.9 -revert {
  270. DELETE FROM t2 WHERE b=1 AND c=2;
  271. } {
  272. DELETE FROM t2 WHERE b=1;
  273. } {
  274. {DELETE t2 0 .XX. {{} {} i 1 i 1 {} {}} {}}
  275. {DELETE t2 0 .XX. {{} {} i 1 i 2 {} {}} {}}
  276. }
  277. #-------------------------------------------------------------------------
  278. # More rigorous testing of the _patchset(), _apply and _concat() APIs.
  279. #
  280. # The inputs to each test are a populate database and a list of DML
  281. # statements. This test determines that the final database is the same
  282. # if:
  283. #
  284. # 1) the statements are executed directly on the database.
  285. #
  286. # 2) a single patchset is collected while executing the statements and
  287. # then applied to a copy of the original database file.
  288. #
  289. # 3) individual patchsets are collected for statement while executing
  290. # them and concatenated together before being applied to a copy of
  291. # the original database. The concatenation is done in a couple of
  292. # different ways - linear, pairwise etc.
  293. #
  294. # All tests, as it happens, are run with both changesets and patchsets.
  295. # But the focus is on patchset capabilities.
  296. #
  297. # Return a checksum of the contents of the database file. Implicit IPK
  298. # columns are not included in the checksum - just modifying rowids does
  299. # not change the database checksum.
  300. #
  301. proc databasecksum {db} {
  302. set alltab [$db eval {SELECT name FROM sqlite_master WHERE type='table'}]
  303. foreach tab $alltab {
  304. $db eval "SELECT * FROM $tab LIMIT 1" res { }
  305. set slist [list]
  306. foreach col [lsort $res(*)] {
  307. lappend slist "quote($col)"
  308. }
  309. set sql "SELECT [join $slist ,] FROM $tab"
  310. append txt "[lsort [$db eval $sql]]\n"
  311. }
  312. return [md5 $txt]
  313. }
  314. proc do_patchset_test {tn tstcmd lSql} {
  315. if {$tstcmd != "patchset" && $tstcmd != "changeset"} {
  316. error "have $tstcmd: must be patchset or changeset"
  317. }
  318. foreach fname {test.db2 test.db3 test.db4 test.db5} {
  319. forcedelete $fname
  320. forcecopy test.db $fname
  321. }
  322. # Execute the SQL statements on [db]. Collect a patchset for each
  323. # individual statement, as well as a single patchset for the entire
  324. # operation.
  325. sqlite3session S db main
  326. S attach *
  327. foreach sql $lSql {
  328. sqlite3session T db main
  329. T attach *
  330. db eval $sql
  331. lappend lPatch [T $tstcmd]
  332. T delete
  333. }
  334. set patchset [S $tstcmd]
  335. S delete
  336. # Calculate a checksum for the final database.
  337. set cksum [databasecksum db]
  338. # 1. Apply the single large patchset to test.db2
  339. sqlite3 db2 test.db2
  340. sqlite3changeset_apply db2 $patchset noop
  341. uplevel [list do_test $tn.1 { databasecksum db2 } $cksum ]
  342. db2 close
  343. # 2. Apply each of the single-statement patchsets to test.db3
  344. sqlite3 db2 test.db3
  345. foreach p $lPatch {
  346. sqlite3changeset_apply db2 $p noop
  347. }
  348. uplevel [list do_test $tn.2 { databasecksum db2 } $cksum ]
  349. db2 close
  350. # 3. Concatenate all single-statement patchsets into a single large
  351. # patchset, then apply it to test.db4.
  352. #
  353. sqlite3 db2 test.db4
  354. set big ""
  355. foreach p $lPatch {
  356. set big [sqlite3changeset_concat $big $p]
  357. }
  358. sqlite3changeset_apply db2 $big noop
  359. uplevel [list do_test $tn.3 { databasecksum db2 } $cksum ]
  360. db2 close
  361. # 4. Concatenate all single-statement patchsets pairwise into a single
  362. # large patchset, then apply it to test.db5. Pairwise concatenation:
  363. #
  364. # a b c d e f g h i j k
  365. # -> {a b} {c d} {e f} {g h} {i j} k
  366. # -> {a b c d} {e f g h} {i j k}
  367. # -> {a b c d e f g h} {i j k}
  368. # -> {a b c d e f g h i j k}
  369. # -> APPLY!
  370. #
  371. sqlite3 db2 test.db5
  372. set L $lPatch
  373. while {[llength $L] > 1} {
  374. set O [list]
  375. for {set i 0} {$i < [llength $L]} {incr i 2} {
  376. if {$i==[llength $L]-1} {
  377. lappend O [lindex $L $i]
  378. } else {
  379. set i1 [expr $i+1]
  380. lappend O [sqlite3changeset_concat [lindex $L $i] [lindex $L $i1]]
  381. }
  382. }
  383. set L $O
  384. }
  385. sqlite3changeset_apply db2 [lindex $L 0] noop
  386. uplevel [list do_test $tn.4 { databasecksum db2 } $cksum ]
  387. db2 close
  388. }
  389. proc do_patchset_changeset_test {tn initsql args} {
  390. foreach tstcmd {patchset changeset} {
  391. reset_db
  392. execsql $initsql
  393. set x 0
  394. foreach sql $args {
  395. incr x
  396. set lSql [split $sql ";"]
  397. uplevel [list do_patchset_test $tn.$tstcmd.$x $tstcmd $lSql]
  398. }
  399. }
  400. }
  401. do_patchset_changeset_test 5.1 {
  402. CREATE TABLE t1(a PRIMARY KEY, b, c);
  403. INSERT INTO t1 VALUES(1, 2, 3);
  404. } {
  405. INSERT INTO t1 VALUES(4, 5, 6);
  406. DELETE FROM t1 WHERE a=1;
  407. } {
  408. INSERT INTO t1 VALUES(7, 8, 9);
  409. UPDATE t1 SET c = 5;
  410. INSERT INTO t1 VALUES(10, 11, 12);
  411. UPDATE t1 SET c = 6;
  412. INSERT INTO t1 VALUES(13, 14, 15);
  413. } {
  414. UPDATE t1 SET c=c+1;
  415. DELETE FROM t1 WHERE (a%2);
  416. }
  417. do_patchset_changeset_test 5.2 {
  418. CREATE TABLE t1(a PRIMARY KEY, b, c);
  419. CREATE TABLE t2(a, b, c, d, PRIMARY KEY(c, b));
  420. } {
  421. INSERT INTO t1 VALUES(x'00', 0, 'zero');
  422. INSERT INTO t1 VALUES(x'01', 1, 'one');
  423. INSERT INTO t1 VALUES(x'02', 4, 'four');
  424. INSERT INTO t1 VALUES(x'03', 9, 'nine');
  425. INSERT INTO t1 VALUES(x'04', 16, 'sixteen');
  426. INSERT INTO t1 VALUES(x'05', 25, 'twenty-five');
  427. } {
  428. UPDATE t1 SET a = b WHERE b<=4;
  429. INSERT INTO t2 SELECT NULL, * FROM t1;
  430. DELETE FROM t1 WHERE b=25;
  431. } {
  432. DELETE FROM t2;
  433. INSERT INTO t2 SELECT NULL, * FROM t1;
  434. DELETE FROM t1;
  435. INSERT INTO t1 SELECT b, c, d FROM t2;
  436. UPDATE t1 SET b = b+1;
  437. UPDATE t1 SET b = b+1;
  438. UPDATE t1 SET b = b+1;
  439. }
  440. set initsql { CREATE TABLE t1(a, b, c, PRIMARY KEY(c, b)); }
  441. for {set i 0} {$i < 1000} {incr i} {
  442. append insert "INSERT INTO t1 VALUES($i, $i, $i);"
  443. append delete "DELETE FROM t1 WHERE b=$i;"
  444. }
  445. do_patchset_changeset_test 5.3 \
  446. $initsql $insert $delete \
  447. $insert $delete \
  448. "$insert $delete" \
  449. $delete
  450. finish_test