rbuvacuum2.test 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # 2016 June 1
  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 contains tests for the RBU module. More specifically, it
  13. # contains tests to ensure that the sqlite3rbu_vacuum() API works as
  14. # expected.
  15. #
  16. source [file join [file dirname [info script]] rbu_common.tcl]
  17. if_no_rbu_support { finish_test ; return }
  18. foreach {step} {0 1} {
  19. foreach {ttt state} {
  20. s state.db t test.db-vacuum n {}
  21. } {
  22. set ::testprefix rbuvacuum2-$step$ttt
  23. #-------------------------------------------------------------------------
  24. # Test that a database that contains fts3 tables can be vacuumed.
  25. #
  26. ifcapable fts3 {
  27. reset_db
  28. do_execsql_test 1.1 {
  29. CREATE VIRTUAL TABLE t1 USING fts3(z, y);
  30. INSERT INTO t1 VALUES('fix this issue', 'at some point');
  31. }
  32. do_rbu_vacuum_test 1.2 $step $state
  33. do_execsql_test 1.3 {
  34. SELECT * FROM t1;
  35. } {{fix this issue} {at some point}}
  36. do_execsql_test 1.4 {
  37. SELECT rowid FROM t1 WHERE t1 MATCH 'fix';
  38. } {1}
  39. do_execsql_test 1.5 {
  40. INSERT INTO t1 VALUES('a b c', 'd e f');
  41. INSERT INTO t1 VALUES('l h i', 'd e f');
  42. DELETE FROM t1 WHERE docid = 2;
  43. INSERT INTO t1 VALUES('a b c', 'x y z');
  44. }
  45. do_rbu_vacuum_test 1.6 $step $state
  46. do_execsql_test 1.7 {
  47. INSERT INTO t1(t1) VALUES('integrity-check');
  48. SELECT * FROM t1;
  49. } {
  50. {fix this issue} {at some point}
  51. {l h i} {d e f}
  52. {a b c} {x y z}
  53. }
  54. }
  55. #-------------------------------------------------------------------------
  56. # Test that a database that contains fts5 tables can be vacuumed.
  57. #
  58. ifcapable fts5 {
  59. reset_db
  60. do_execsql_test 2.1 {
  61. CREATE VIRTUAL TABLE t1 USING fts5(z, y);
  62. INSERT INTO t1 VALUES('fix this issue', 'at some point');
  63. }
  64. do_rbu_vacuum_test 2.2 $step $state
  65. do_execsql_test 2.3 {
  66. SELECT * FROM t1;
  67. } {{fix this issue} {at some point}}
  68. do_execsql_test 2.4 {
  69. SELECT rowid FROM t1 ('fix');
  70. } {1}
  71. do_execsql_test 2.5 {
  72. INSERT INTO t1 VALUES('a b c', 'd e f');
  73. INSERT INTO t1 VALUES('l h i', 'd e f');
  74. DELETE FROM t1 WHERE rowid = 2;
  75. INSERT INTO t1 VALUES('a b c', 'x y z');
  76. }
  77. do_rbu_vacuum_test 2.6 $step $state
  78. do_execsql_test 2.7 {
  79. INSERT INTO t1(t1) VALUES('integrity-check');
  80. SELECT * FROM t1;
  81. } {
  82. {fix this issue} {at some point}
  83. {l h i} {d e f}
  84. {a b c} {x y z}
  85. }
  86. }
  87. #-------------------------------------------------------------------------
  88. # Test that a database that contains an rtree table can be vacuumed.
  89. #
  90. ifcapable rtree {
  91. reset_db
  92. do_execsql_test 3.1 {
  93. CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2);
  94. INSERT INTO rt VALUES(1, 45, 55);
  95. INSERT INTO rt VALUES(2, 50, 60);
  96. INSERT INTO rt VALUES(3, 55, 65);
  97. }
  98. do_rbu_vacuum_test 3.2 $step $state
  99. do_execsql_test 3.3 {
  100. SELECT * FROM rt;
  101. } {1 45.0 55.0 2 50.0 60.0 3 55.0 65.0}
  102. do_execsql_test 3.4.1 {
  103. SELECT rowid FROM rt WHERE x2>51 AND x1 < 51
  104. } {1 2}
  105. do_execsql_test 3.4.2 {
  106. SELECT rowid FROM rt WHERE x2>59 AND x1 < 59
  107. } {2 3}
  108. do_rbu_vacuum_test 3.5 $step $state
  109. do_execsql_test 3.6.1 {
  110. SELECT rowid FROM rt WHERE x2>51 AND x1 < 51
  111. } {1 2}
  112. do_execsql_test 3.6.2 {
  113. SELECT rowid FROM rt WHERE x2>59 AND x1 < 59
  114. } {2 3}
  115. }
  116. ifcapable trigger {
  117. reset_db
  118. do_execsql_test 4.1 {
  119. CREATE TABLE t1(a, b, c);
  120. INSERT INTO t1 VALUES(1, 2, 3);
  121. CREATE VIEW v1 AS SELECT * FROM t1;
  122. CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN SELECT 1; END;
  123. }
  124. do_execsql_test 4.2 {
  125. SELECT * FROM sqlite_master;
  126. } {
  127. table t1 t1 2 {CREATE TABLE t1(a, b, c)}
  128. view v1 v1 0 {CREATE VIEW v1 AS SELECT * FROM t1}
  129. trigger tr1 t1 0 {CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN SELECT 1; END}
  130. }
  131. do_rbu_vacuum_test 4.3 $step $state
  132. do_execsql_test 4.4 {
  133. SELECT * FROM sqlite_master;
  134. } {
  135. table t1 t1 2 {CREATE TABLE t1(a, b, c)}
  136. view v1 v1 0 {CREATE VIEW v1 AS SELECT * FROM t1}
  137. trigger tr1 t1 0 {CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN SELECT 1; END}
  138. }
  139. }
  140. }
  141. }
  142. #-------------------------------------------------------------------------
  143. # Test that passing a NULL value as the second argument to
  144. # sqlite3rbu_vacuum() causes it to:
  145. #
  146. # * Use <database>-vacuum as the state db, and
  147. # * Set the state db permissions to the same as those on the db file.
  148. #
  149. db close
  150. if {$::tcl_platform(platform)=="unix"} {
  151. forcedelete test.db
  152. sqlite3 db test.db
  153. do_execsql_test 5.0 {
  154. CREATE TABLE t1(a, b);
  155. INSERT INTO t1 VALUES(1, 2);
  156. INSERT INTO t1 VALUES(3, 4);
  157. INSERT INTO t1 VALUES(5, 6);
  158. INSERT INTO t1 VALUES(7, 8);
  159. }
  160. db close
  161. foreach {tn perm} {
  162. 1 00755
  163. 2 00666
  164. 3 00644
  165. 4 00444
  166. } {
  167. forcedelete test.db-vacuum
  168. do_test 5.$tn.1 {
  169. file attributes test.db -permissions $perm
  170. sqlite3rbu_vacuum rbu test.db
  171. rbu step
  172. } {SQLITE_OK}
  173. do_test 5.$tn.2 { file exists test.db-vacuum } 1
  174. # The result pattern might be 00xxx or 0oxxx depending on which
  175. # version of TCL is being used. So make perm2 into a regexp that
  176. # will match either
  177. regsub {^00} $perm {0.} perm2
  178. do_test 5.$tn.3 { file attributes test.db-vacuum -permissions} /$perm2/
  179. rbu close
  180. }
  181. }
  182. #-------------------------------------------------------------------------
  183. # Test the outcome of some other connection running a checkpoint while
  184. # the incremental checkpoint is suspended.
  185. #
  186. reset_db
  187. do_execsql_test 6.0 {
  188. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
  189. CREATE INDEX i1b ON t1(b);
  190. CREATE INDEX i1c ON t1(c);
  191. INSERT INTO t1 VALUES(1, 2, 3);
  192. INSERT INTO t1 VALUES(4, 5, 6);
  193. }
  194. forcedelete test.db2
  195. do_test 6.1 {
  196. sqlite3rbu_vacuum rbu test.db test.db2
  197. while {[rbu state]!="checkpoint"} { rbu step }
  198. rbu close
  199. } {SQLITE_OK}
  200. do_test 6.2 {
  201. execsql { SELECT 1 FROM sqlite_master LIMIT 1 }
  202. execsql { PRAGMA wal_checkpoint }
  203. execsql { SELECT 1 FROM sqlite_master LIMIT 1 }
  204. } {1}
  205. do_test 6.3 {
  206. sqlite3rbu_vacuum rbu test.db test.db2
  207. while {[rbu step]!="SQLITE_DONE"} { rbu step }
  208. rbu close
  209. execsql { PRAGMA integrity_check }
  210. } {ok}
  211. do_test 6.4 {
  212. sqlite3rbu_vacuum rbu test.db test.db-vactmp
  213. list [catch { rbu close } msg] $msg
  214. } {1 SQLITE_MISUSE}
  215. finish_test