rbudiff.test 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # 2015-07-31
  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. # Tests for the [sqldiff --rbu] command.
  13. #
  14. #
  15. if {![info exists testdir]} {
  16. set testdir [file join [file dirname [info script]] .. .. test]
  17. }
  18. source $testdir/tester.tcl
  19. set testprefix rbudiff
  20. set PROG [test_find_sqldiff]
  21. db close
  22. proc get_rbudiff_sql {db1 db2} {
  23. exec $::PROG --rbu $db1 $db2
  24. }
  25. proc get_vtab_rbudiff_sql {db1 db2} {
  26. exec $::PROG --vtab --rbu $db1 $db2
  27. }
  28. proc step_rbu {target rbu} {
  29. while 1 {
  30. sqlite3rbu rbu $target $rbu
  31. set rc [rbu step]
  32. rbu close
  33. if {$rc != "SQLITE_OK"} break
  34. }
  35. set rc
  36. }
  37. proc apply_rbudiff {sql target} {
  38. test_rbucount $sql
  39. forcedelete rbu.db
  40. sqlite3 rbudb rbu.db
  41. rbudb eval $sql
  42. rbudb close
  43. step_rbu $target rbu.db
  44. }
  45. proc sqlesc {id} {
  46. set ret "'[string map {' ''} $id]'"
  47. set ret
  48. }
  49. # The only argument is the output of an [sqldiff -rbu] run. This command
  50. # tests that the contents of the rbu_count table is correct. An exception
  51. # is thrown if it is not.
  52. #
  53. proc test_rbucount {sql} {
  54. sqlite3 tmpdb ""
  55. tmpdb eval $sql
  56. tmpdb eval {
  57. SELECT name FROM sqlite_master WHERE name LIKE 'data%' AND type='table'
  58. } {
  59. set a [tmpdb eval "SELECT count(*) FROM [sqlesc $name]"]
  60. set b [tmpdb eval {SELECT cnt FROM rbu_count WHERE tbl = $name}]
  61. if {$a != $b} {
  62. tmpdb close
  63. error "rbu_count error - tbl = $name"
  64. }
  65. }
  66. tmpdb close
  67. return ""
  68. }
  69. proc rbudiff_cksum {db1} {
  70. set txt ""
  71. sqlite3 dbtmp $db1
  72. foreach tbl [dbtmp eval {SELECT name FROM sqlite_master WHERE type='table'}] {
  73. set cols [list]
  74. dbtmp eval "PRAGMA table_info = [sqlesc $tbl]" {
  75. lappend cols "quote( $name )"
  76. }
  77. append txt [dbtmp eval \
  78. "SELECT [join $cols {||'.'||}] FROM [sqlesc $tbl] ORDER BY 1"
  79. ]
  80. }
  81. dbtmp close
  82. md5 $txt
  83. }
  84. foreach {tn init mod} {
  85. 1 {
  86. CREATE TABLE t1(a PRIMARY KEY, b, c);
  87. INSERT INTO t1 VALUES(1, 2, 3);
  88. INSERT INTO t1 VALUES(4, 5, 6);
  89. CREATE TABLE t2(a, b, c, PRIMARY KEY(b, c));
  90. INSERT INTO t2 VALUES(1, 2, 3);
  91. INSERT INTO t2 VALUES(4, 5, 6);
  92. } {
  93. INSERT INTO t1 VALUES(7, 8, 9);
  94. DELETE FROM t1 WHERE a=4;
  95. UPDATE t1 SET c = 11 WHERE a = 1;
  96. INSERT INTO t2 VALUES(7, 8, 9);
  97. DELETE FROM t2 WHERE a=4;
  98. UPDATE t2 SET c = 11 WHERE a = 1;
  99. }
  100. 2 {
  101. CREATE TABLE t1(a, b, c, PRIMARY KEY(a, b, c));
  102. INSERT INTO t1 VALUES('u', 'v', 'w');
  103. INSERT INTO t1 VALUES('x', 'y', 'z');
  104. } {
  105. DELETE FROM t1 WHERE a='u';
  106. INSERT INTO t1 VALUES('a', 'b', 'c');
  107. }
  108. 3 {
  109. CREATE TABLE t1(i INTEGER PRIMARY KEY, x);
  110. INSERT INTO t1 VALUES(1,
  111. X'0000000000000000111111111111111122222222222222223333333333333333'
  112. );
  113. CREATE TABLE t2(y INTEGER PRIMARY KEY, x);
  114. INSERT INTO t2 VALUES(1,
  115. X'0000000000000000111111111111111122222222222222223333333333333333'
  116. );
  117. } {
  118. DELETE FROM t1;
  119. INSERT INTO t1 VALUES(1,
  120. X'0000000000000000111111111111111122222555555552223333333333333333'
  121. );
  122. DELETE FROM t2;
  123. INSERT INTO t2 VALUES(1,
  124. X'0000000000000000111111111111111122222222222222223333333FFF333333'
  125. );
  126. }
  127. 4 {
  128. CREATE TABLE x1(a, b, c, PRIMARY KEY(a, b, c));
  129. INSERT INTO x1 VALUES('u', 'v', NULL);
  130. INSERT INTO x1 VALUES('x', 'y', 'z');
  131. INSERT INTO x1 VALUES('a', NULL, 'b');
  132. } {
  133. INSERT INTO x1 VALUES('a', 'b', 'c');
  134. }
  135. 5 {
  136. CREATE TABLE t1(a PRIMARY KEY, b);
  137. INSERT INTO t1 VALUES(1, NULL);
  138. INSERT INTO t1 VALUES(2, X'');
  139. } {
  140. UPDATE t1 SET b = X'' WHERE a=1;
  141. UPDATE t1 SET b = NULL WHERE a=2;
  142. }
  143. } {
  144. catch { db close }
  145. forcedelete test.db test.db2
  146. sqlite3 db test.db
  147. db eval "$init"
  148. sqlite3 db test.db2
  149. db eval "$init ; $mod"
  150. db close
  151. do_test 1.$tn.2 {
  152. set sql [get_rbudiff_sql test.db test.db2]
  153. apply_rbudiff $sql test.db
  154. } {SQLITE_DONE}
  155. do_test 1.$tn.3 { rbudiff_cksum test.db } [rbudiff_cksum test.db2]
  156. forcedelete test.db test.db2
  157. sqlite3 db test.db
  158. db eval "$init ; $mod"
  159. sqlite3 db test.db2
  160. db eval "$init"
  161. db close
  162. do_test 1.$tn.4 {
  163. set sql [get_rbudiff_sql test.db test.db2]
  164. apply_rbudiff $sql test.db
  165. } {SQLITE_DONE}
  166. do_test 1.$tn.5 { rbudiff_cksum test.db } [rbudiff_cksum test.db2]
  167. }
  168. #-------------------------------------------------------------------------
  169. # Test that if the --vtab switch is present, [sqldiff] handles virtual
  170. # table types fts[345] and rtree correctly.
  171. #
  172. ifcapable fts3&&fts5&&rtree {
  173. foreach {tn init mod} {
  174. 1 {
  175. CREATE VIRTUAL TABLE t1 USING fts5(c);
  176. INSERT INTO t1 VALUES('a b c');
  177. INSERT INTO t1 VALUES('a b c');
  178. } {
  179. DELETE FROM t1 WHERE rowid = 1;
  180. INSERT INTO t1 VALUES('a b c');
  181. }
  182. 2 {
  183. CREATE VIRTUAL TABLE "x y" USING 'rtree'(id, x1, x2);
  184. INSERT INTO "x y" VALUES(1, 2, 3);
  185. INSERT INTO "x y" VALUES(2, 4, 6);
  186. } {
  187. DELETE FROM "x y" WHERE rowid = 1;
  188. INSERT INTO "x y" VALUES(3, 6, 9);
  189. }
  190. 3 {
  191. CREATE VIRTUAL TABLE 'x''y' USING fts3;
  192. INSERT INTO 'x''y' VALUES('one two three');
  193. INSERT INTO 'x''y' VALUES('four five six');
  194. } {
  195. DELETE FROM 'x''y' WHERE rowid = 1;
  196. INSERT INTO 'x''y' VALUES('one two three');
  197. }
  198. } {
  199. forcedelete test.db test.db2
  200. sqlite3 db test.db
  201. db eval "$init"
  202. sqlite3 db test.db2
  203. db eval "$init ; $mod"
  204. db close
  205. do_test 2.$tn.1 {
  206. set sql [get_vtab_rbudiff_sql test.db test.db2]
  207. apply_rbudiff $sql test.db
  208. } {SQLITE_DONE}
  209. do_test 2.$tn.2 { rbudiff_cksum test.db } [rbudiff_cksum test.db2]
  210. }
  211. }
  212. ifcapable fts5 {
  213. foreach {tn init mod} {
  214. 1 {
  215. CREATE VIRTUAL TABLE t1 USING fts5(c);
  216. INSERT INTO t1 VALUES('a b c');
  217. INSERT INTO t1 VALUES('a b c');
  218. } {
  219. DELETE FROM t1 WHERE rowid = 1;
  220. INSERT INTO t1 VALUES('a b c');
  221. }
  222. 2 {
  223. CREATE VIRTUAL TABLE t1 USING FTs5(c);
  224. INSERT INTO t1 VALUES('a b c');
  225. INSERT INTO t1 VALUES('a b c');
  226. } {
  227. DELETE FROM t1 WHERE rowid = 1;
  228. INSERT INTO t1 VALUES('a b c');
  229. }
  230. 3 {
  231. creAte virTUal
  232. tablE t1 USING FTs5(c);
  233. INSERT INTO t1 VALUES('a b c');
  234. INSERT INTO t1 VALUES('a b c');
  235. } {
  236. DELETE FROM t1 WHERE rowid = 1;
  237. INSERT INTO t1 VALUES('a b c');
  238. }
  239. } {
  240. forcedelete test.db test.db2
  241. sqlite3 db test.db
  242. db eval "$init"
  243. sqlite3 db test.db2
  244. db eval "$init ; $mod"
  245. db eval { INSERT INTO t1(t1) VALUES('optimize') }
  246. db close
  247. do_test 3.$tn.1 {
  248. set sql [get_vtab_rbudiff_sql test.db test.db2]
  249. apply_rbudiff $sql test.db
  250. } {SQLITE_DONE}
  251. sqlite3 db test.db
  252. sqlite3 db2 test.db2
  253. do_test 3.$tn.2 {
  254. db2 eval { SELECT * FROM t1 ORDER BY rowid }
  255. } [db eval { SELECT * FROM t1 ORDER BY rowid }]
  256. do_test 3.$tn.3 {
  257. db2 eval { INSERT INTO t1(t1) VALUES('integrity-check') }
  258. } {}
  259. db close
  260. db2 close
  261. }
  262. }
  263. finish_test