e_reindex.test 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. # 2010 September 24
  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 tests to verify that the "testable statements" in
  13. # the lang_reindex.html document are correct.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. proc do_reindex_tests {args} {
  18. uplevel do_select_tests $args
  19. }
  20. do_execsql_test e_reindex-0.0 {
  21. CREATE TABLE t1(a, b);
  22. CREATE INDEX i1 ON t1(a, b);
  23. CREATE INDEX i2 ON t1(b, a);
  24. } {}
  25. # -- syntax diagram reindex-stmt
  26. #
  27. do_reindex_tests e_reindex-0.1 {
  28. 1 "REINDEX" {}
  29. 2 "REINDEX nocase" {}
  30. 3 "REINDEX binary" {}
  31. 4 "REINDEX t1" {}
  32. 5 "REINDEX main.t1" {}
  33. 6 "REINDEX i1" {}
  34. 7 "REINDEX main.i1" {}
  35. }
  36. # EVIDENCE-OF: R-52173-44778 The REINDEX command is used to delete and
  37. # recreate indices from scratch.
  38. #
  39. # Test this by corrupting some database indexes, running REINDEX, and
  40. # observing that the corruption is gone.
  41. #
  42. do_execsql_test e_reindex-1.1 {
  43. INSERT INTO t1 VALUES(1, 2);
  44. INSERT INTO t1 VALUES(3, 4);
  45. INSERT INTO t1 VALUES(5, 6);
  46. PRAGMA writable_schema = 1;
  47. UPDATE sqlite_master SET sql = '-- ' || sql WHERE type = 'index';
  48. } {}
  49. db close
  50. sqlite3 db test.db
  51. do_execsql_test e_reindex-1.2 {
  52. DELETE FROM t1 WHERE a = 3;
  53. INSERT INTO t1 VALUES(7, 8);
  54. INSERT INTO t1 VALUES(9, 10);
  55. PRAGMA writable_schema = 1;
  56. UPDATE sqlite_master SET sql = substr(sql, 4) WHERE type = 'index';
  57. } {}
  58. db close
  59. sqlite3 db test.db
  60. do_execsql_test e_reindex-1.3 {
  61. PRAGMA integrity_check;
  62. } [list \
  63. {rowid 4 missing from index i2} \
  64. {rowid 4 missing from index i1} \
  65. {rowid 5 missing from index i2} \
  66. {rowid 5 missing from index i1} \
  67. {wrong # of entries in index i2} \
  68. {wrong # of entries in index i1}
  69. ]
  70. do_execsql_test e_reindex-1.4 {
  71. REINDEX;
  72. PRAGMA integrity_check;
  73. } {ok}
  74. #-------------------------------------------------------------------------
  75. # The remaining tests in this file focus on testing that the REINDEX
  76. # command reindexes the correct subset of the indexes in the database.
  77. # They all use the following dataset.
  78. #
  79. db close
  80. forcedelete test.db2
  81. forcedelete test.db
  82. sqlite3 db test.db
  83. proc sort_by_length {lhs rhs} {
  84. set res [expr {[string length $lhs] - [string length $rhs]}]
  85. if {$res!=0} {return $res}
  86. return [string compare $lhs $rhs]
  87. }
  88. array set V {one 1 two 2 three 3 four 4 five 5 six 6 seven 7 eight 8}
  89. proc sort_by_value {lhs rhs} {
  90. global V
  91. set res [expr {$V($lhs) - $V($rhs)}]
  92. if {$res!=0} {return $res}
  93. return [string compare $lhs $rhs]
  94. }
  95. db collate collA sort_by_length
  96. db collate collB sort_by_value
  97. set BY(length) {one six two five four eight seven three}
  98. set BY(value) {one two three four five six seven eight}
  99. do_execsql_test e_reindex-2.0 {
  100. ATTACH 'test.db2' AS aux;
  101. CREATE TABLE t1(x);
  102. CREATE INDEX i1_a ON t1(x COLLATE collA);
  103. CREATE INDEX i1_b ON t1(x COLLATE collB);
  104. INSERT INTO t1 VALUES('one');
  105. INSERT INTO t1 VALUES('two');
  106. INSERT INTO t1 VALUES('three');
  107. INSERT INTO t1 VALUES('four');
  108. INSERT INTO t1 VALUES('five');
  109. INSERT INTO t1 VALUES('six');
  110. INSERT INTO t1 VALUES('seven');
  111. INSERT INTO t1 VALUES('eight');
  112. CREATE TABLE t2(x);
  113. CREATE INDEX i2_a ON t2(x COLLATE collA);
  114. CREATE INDEX i2_b ON t2(x COLLATE collB);
  115. INSERT INTO t2 SELECT x FROM t1;
  116. CREATE TABLE aux.t1(x);
  117. CREATE INDEX aux.i1_a ON t1(x COLLATE collA);
  118. CREATE INDEX aux.i1_b ON t1(x COLLATE collB);
  119. INSERT INTO aux.t1 SELECT x FROM main.t1;
  120. } {}
  121. proc test_index {tn tbl collation expected} {
  122. set sql "SELECT x FROM $tbl ORDER BY x COLLATE $collation"
  123. uplevel do_execsql_test e_reindex-2.$tn [list $sql] [list $::BY($expected)]
  124. }
  125. proc set_collations {a b} {
  126. db collate collA "sort_by_$a"
  127. db collate collB "sort_by_$b"
  128. }
  129. test_index 1.1 t1 collA length
  130. test_index 1.2 t1 collB value
  131. test_index 1.3 t2 collA length
  132. test_index 1.4 t2 collB value
  133. test_index 1.5 aux.t1 collA length
  134. test_index 1.6 aux.t1 collB value
  135. # EVIDENCE-OF: R-47362-07898 If the REINDEX keyword is not followed by a
  136. # collation-sequence or database object identifier, then all indices in
  137. # all attached databases are rebuilt.
  138. #
  139. set_collations value length
  140. do_execsql_test e_reindex-2.2.1 "REINDEX" {}
  141. test_index 2.2 t1 collA value
  142. test_index 2.3 t1 collB length
  143. test_index 2.4 t2 collA value
  144. test_index 2.5 t2 collB length
  145. test_index 2.6 aux.t1 collA value
  146. test_index 2.7 aux.t1 collB length
  147. # EVIDENCE-OF: R-45878-07697 If the REINDEX keyword is followed by a
  148. # collation-sequence name, then all indices in all attached databases
  149. # that use the named collation sequences are recreated.
  150. #
  151. set_collations length value
  152. do_execsql_test e_reindex-2.3.1 "REINDEX collA" {}
  153. test_index 3.2 t1 collA length
  154. test_index 3.3 t1 collB length
  155. test_index 3.4 t2 collA length
  156. test_index 3.5 t2 collB length
  157. test_index 3.6 aux.t1 collA length
  158. test_index 3.7 aux.t1 collB length
  159. do_execsql_test e_reindex-2.3.8 "REINDEX collB" {}
  160. test_index 3.9 t1 collA length
  161. test_index 3.10 t1 collB value
  162. test_index 3.11 t2 collA length
  163. test_index 3.12 t2 collB value
  164. test_index 3.13 aux.t1 collA length
  165. test_index 3.14 aux.t1 collB value
  166. # EVIDENCE-OF: R-49616-30196 Or, if the argument attached to the REINDEX
  167. # identifies a specific database table, then all indices attached to the
  168. # database table are rebuilt.
  169. #
  170. set_collations value length
  171. do_execsql_test e_reindex-2.4.1 "REINDEX t1" {}
  172. test_index 4.2 t1 collA value
  173. test_index 4.3 t1 collB length
  174. test_index 4.4 t2 collA length
  175. test_index 4.5 t2 collB value
  176. test_index 4.6 aux.t1 collA length
  177. test_index 4.7 aux.t1 collB value
  178. do_execsql_test e_reindex-2.4.8 "REINDEX aux.t1" {}
  179. test_index 4.9 t1 collA value
  180. test_index 4.10 t1 collB length
  181. test_index 4.11 t2 collA length
  182. test_index 4.12 t2 collB value
  183. test_index 4.13 aux.t1 collA value
  184. test_index 4.14 aux.t1 collB length
  185. do_execsql_test e_reindex-2.4.15 "REINDEX t2" {}
  186. test_index 4.16 t1 collA value
  187. test_index 4.17 t1 collB length
  188. test_index 4.18 t2 collA value
  189. test_index 4.19 t2 collB length
  190. test_index 4.20 aux.t1 collA value
  191. test_index 4.21 aux.t1 collB length
  192. # EVIDENCE-OF: R-58823-28748 If it identifies a specific database index,
  193. # then just that index is recreated.
  194. #
  195. set_collations length value
  196. do_execsql_test e_reindex-2.5.1 "REINDEX i1_a" {}
  197. test_index 5.2 t1 collA length
  198. test_index 5.3 t1 collB length
  199. test_index 5.4 t2 collA value
  200. test_index 5.5 t2 collB length
  201. test_index 5.6 aux.t1 collA value
  202. test_index 5.7 aux.t1 collB length
  203. do_execsql_test e_reindex-2.5.8 "REINDEX i2_b" {}
  204. test_index 5.9 t1 collA length
  205. test_index 5.10 t1 collB length
  206. test_index 5.11 t2 collA value
  207. test_index 5.12 t2 collB value
  208. test_index 5.13 aux.t1 collA value
  209. test_index 5.14 aux.t1 collB length
  210. do_execsql_test e_reindex-2.5.15 "REINDEX aux.i1_b" {}
  211. test_index 5.16 t1 collA length
  212. test_index 5.17 t1 collB length
  213. test_index 5.18 t2 collA value
  214. test_index 5.19 t2 collB value
  215. test_index 5.20 aux.t1 collA value
  216. test_index 5.21 aux.t1 collB value
  217. do_execsql_test e_reindex-2.5.22 "REINDEX i1_b" {}
  218. test_index 5.23 t1 collA length
  219. test_index 5.24 t1 collB value
  220. test_index 5.25 t2 collA value
  221. test_index 5.26 t2 collB value
  222. test_index 5.27 aux.t1 collA value
  223. test_index 5.28 aux.t1 collB value
  224. do_execsql_test e_reindex-2.5.29 "REINDEX i2_a" {}
  225. test_index 5.30 t1 collA length
  226. test_index 5.31 t1 collB value
  227. test_index 5.32 t2 collA length
  228. test_index 5.33 t2 collB value
  229. test_index 5.34 aux.t1 collA value
  230. test_index 5.35 aux.t1 collB value
  231. do_execsql_test e_reindex-2.5.36 "REINDEX aux.i1_a" {}
  232. test_index 5.37 t1 collA length
  233. test_index 5.38 t1 collB value
  234. test_index 5.39 t2 collA length
  235. test_index 5.40 t2 collB value
  236. test_index 5.41 aux.t1 collA length
  237. test_index 5.42 aux.t1 collB value
  238. # EVIDENCE-OF: R-15639-02023 If no database-name is specified and there
  239. # exists both a table or index and a collation sequence of the specified
  240. # name, SQLite interprets this as a request to rebuild the indices that
  241. # use the named collation sequence.
  242. #
  243. set_collations value length
  244. do_execsql_test e_reindex-2.6.0 {
  245. CREATE TABLE collA(x);
  246. CREATE INDEX icolla_a ON collA(x COLLATE collA);
  247. CREATE INDEX icolla_b ON collA(x COLLATE collB);
  248. INSERT INTO collA SELECT x FROM t1;
  249. } {}
  250. test_index 6.1 collA collA value
  251. test_index 6.2 collA collB length
  252. set_collations length value
  253. do_execsql_test e_reindex-2.6.3 "REINDEX collA" {}
  254. test_index 6.4 collA collA length
  255. test_index 6.5 collA collB length
  256. do_execsql_test e_reindex-2.6.3 "REINDEX main.collA" {}
  257. test_index 6.4 collA collA length
  258. test_index 6.5 collA collB value
  259. set_collations value length
  260. do_execsql_test e_reindex-2.6.6 "REINDEX main.collA" {}
  261. test_index 6.7 collA collA value
  262. test_index 6.8 collA collB length
  263. finish_test