000001 # 2010 September 24 000002 # 000003 # The author disclaims copyright to this source code. In place of 000004 # a legal notice, here is a blessing: 000005 # 000006 # May you do good and not evil. 000007 # May you find forgiveness for yourself and forgive others. 000008 # May you share freely, never taking more than you give. 000009 # 000010 #*********************************************************************** 000011 # 000012 # This file implements tests to verify that the "testable statements" in 000013 # the lang_reindex.html document are correct. 000014 # 000015 000016 set testdir [file dirname $argv0] 000017 source $testdir/tester.tcl 000018 000019 proc do_reindex_tests {args} { 000020 uplevel do_select_tests $args 000021 } 000022 000023 do_execsql_test e_reindex-0.0 { 000024 CREATE TABLE t1(a, b); 000025 CREATE INDEX i1 ON t1(a, b); 000026 CREATE INDEX i2 ON t1(b, a); 000027 } {} 000028 000029 # -- syntax diagram reindex-stmt 000030 # 000031 do_reindex_tests e_reindex-0.1 { 000032 1 "REINDEX" {} 000033 2 "REINDEX nocase" {} 000034 3 "REINDEX binary" {} 000035 4 "REINDEX t1" {} 000036 5 "REINDEX main.t1" {} 000037 6 "REINDEX i1" {} 000038 7 "REINDEX main.i1" {} 000039 } 000040 000041 # EVIDENCE-OF: R-52173-44778 The REINDEX command is used to delete and 000042 # recreate indices from scratch. 000043 # 000044 # Test this by corrupting some database indexes, running REINDEX, and 000045 # observing that the corruption is gone. 000046 # 000047 do_execsql_test e_reindex-1.1 { 000048 INSERT INTO t1 VALUES(1, 2); 000049 INSERT INTO t1 VALUES(3, 4); 000050 INSERT INTO t1 VALUES(5, 6); 000051 000052 CREATE TABLE saved(a,b,c,d,e); 000053 INSERT INTO saved SELECT * FROM sqlite_master WHERE type = 'index'; 000054 PRAGMA writable_schema = 1; 000055 DELETE FROM sqlite_master WHERE type = 'index'; 000056 } {} 000057 000058 db close 000059 sqlite3 db test.db 000060 do_execsql_test e_reindex-1.2 { 000061 DELETE FROM t1 WHERE a = 3; 000062 INSERT INTO t1 VALUES(7, 8); 000063 INSERT INTO t1 VALUES(9, 10); 000064 PRAGMA writable_schema = 1; 000065 INSERT INTO sqlite_master SELECT * FROM saved; 000066 DROP TABLE saved; 000067 } {} 000068 000069 db close 000070 sqlite3 db test.db 000071 do_execsql_test e_reindex-1.3 { 000072 PRAGMA integrity_check; 000073 } [list \ 000074 {row 3 missing from index i2} \ 000075 {row 3 missing from index i1} \ 000076 {row 4 missing from index i2} \ 000077 {row 4 missing from index i1} \ 000078 {wrong # of entries in index i2} \ 000079 {wrong # of entries in index i1} 000080 ] 000081 000082 do_execsql_test e_reindex-1.4 { 000083 REINDEX; 000084 PRAGMA integrity_check; 000085 } {ok} 000086 000087 #------------------------------------------------------------------------- 000088 # The remaining tests in this file focus on testing that the REINDEX 000089 # command reindexes the correct subset of the indexes in the database. 000090 # They all use the following dataset. 000091 # 000092 db close 000093 forcedelete test.db2 000094 forcedelete test.db 000095 sqlite3 db test.db 000096 000097 proc sort_by_length {lhs rhs} { 000098 set res [expr {[string length $lhs] - [string length $rhs]}] 000099 if {$res!=0} {return $res} 000100 return [string compare $lhs $rhs] 000101 } 000102 array set V {one 1 two 2 three 3 four 4 five 5 six 6 seven 7 eight 8} 000103 proc sort_by_value {lhs rhs} { 000104 global V 000105 set res [expr {$V($lhs) - $V($rhs)}] 000106 if {$res!=0} {return $res} 000107 return [string compare $lhs $rhs] 000108 } 000109 000110 db collate collA sort_by_length 000111 db collate collB sort_by_value 000112 000113 set BY(length) {one six two five four eight seven three} 000114 set BY(value) {one two three four five six seven eight} 000115 000116 do_execsql_test e_reindex-2.0 { 000117 ATTACH 'test.db2' AS aux; 000118 000119 CREATE TABLE t1(x); 000120 CREATE INDEX i1_a ON t1(x COLLATE collA); 000121 CREATE INDEX i1_b ON t1(x COLLATE collB); 000122 INSERT INTO t1 VALUES('one'); 000123 INSERT INTO t1 VALUES('two'); 000124 INSERT INTO t1 VALUES('three'); 000125 INSERT INTO t1 VALUES('four'); 000126 INSERT INTO t1 VALUES('five'); 000127 INSERT INTO t1 VALUES('six'); 000128 INSERT INTO t1 VALUES('seven'); 000129 INSERT INTO t1 VALUES('eight'); 000130 000131 CREATE TABLE t2(x); 000132 CREATE INDEX i2_a ON t2(x COLLATE collA); 000133 CREATE INDEX i2_b ON t2(x COLLATE collB); 000134 INSERT INTO t2 SELECT x FROM t1; 000135 000136 CREATE TABLE aux.t1(x); 000137 CREATE INDEX aux.i1_a ON t1(x COLLATE collA); 000138 CREATE INDEX aux.i1_b ON t1(x COLLATE collB); 000139 INSERT INTO aux.t1 SELECT x FROM main.t1; 000140 000141 } {} 000142 000143 proc test_index {tn tbl collation expected} { 000144 set sql "SELECT x FROM $tbl ORDER BY x COLLATE $collation" 000145 uplevel do_execsql_test e_reindex-2.$tn [list $sql] [list $::BY($expected)] 000146 } 000147 000148 proc set_collations {a b} { 000149 db collate collA "sort_by_$a" 000150 db collate collB "sort_by_$b" 000151 } 000152 000153 test_index 1.1 t1 collA length 000154 test_index 1.2 t1 collB value 000155 test_index 1.3 t2 collA length 000156 test_index 1.4 t2 collB value 000157 test_index 1.5 aux.t1 collA length 000158 test_index 1.6 aux.t1 collB value 000159 000160 000161 # EVIDENCE-OF: R-47362-07898 If the REINDEX keyword is not followed by a 000162 # collation-sequence or database object identifier, then all indices in 000163 # all attached databases are rebuilt. 000164 # 000165 set_collations value length 000166 do_execsql_test e_reindex-2.2.1 "REINDEX" {} 000167 test_index 2.2 t1 collA value 000168 test_index 2.3 t1 collB length 000169 test_index 2.4 t2 collA value 000170 test_index 2.5 t2 collB length 000171 test_index 2.6 aux.t1 collA value 000172 test_index 2.7 aux.t1 collB length 000173 000174 # EVIDENCE-OF: R-45878-07697 If the REINDEX keyword is followed by a 000175 # collation-sequence name, then all indices in all attached databases 000176 # that use the named collation sequences are recreated. 000177 # 000178 set_collations length value 000179 do_execsql_test e_reindex-2.3.1 "REINDEX collA" {} 000180 test_index 3.2 t1 collA length 000181 test_index 3.3 t1 collB length 000182 test_index 3.4 t2 collA length 000183 test_index 3.5 t2 collB length 000184 test_index 3.6 aux.t1 collA length 000185 test_index 3.7 aux.t1 collB length 000186 do_execsql_test e_reindex-2.3.8 "REINDEX collB" {} 000187 test_index 3.9 t1 collA length 000188 test_index 3.10 t1 collB value 000189 test_index 3.11 t2 collA length 000190 test_index 3.12 t2 collB value 000191 test_index 3.13 aux.t1 collA length 000192 test_index 3.14 aux.t1 collB value 000193 000194 # EVIDENCE-OF: R-49616-30196 Or, if the argument attached to the REINDEX 000195 # identifies a specific database table, then all indices attached to the 000196 # database table are rebuilt. 000197 # 000198 set_collations value length 000199 do_execsql_test e_reindex-2.4.1 "REINDEX t1" {} 000200 test_index 4.2 t1 collA value 000201 test_index 4.3 t1 collB length 000202 test_index 4.4 t2 collA length 000203 test_index 4.5 t2 collB value 000204 test_index 4.6 aux.t1 collA length 000205 test_index 4.7 aux.t1 collB value 000206 do_execsql_test e_reindex-2.4.8 "REINDEX aux.t1" {} 000207 test_index 4.9 t1 collA value 000208 test_index 4.10 t1 collB length 000209 test_index 4.11 t2 collA length 000210 test_index 4.12 t2 collB value 000211 test_index 4.13 aux.t1 collA value 000212 test_index 4.14 aux.t1 collB length 000213 do_execsql_test e_reindex-2.4.15 "REINDEX t2" {} 000214 test_index 4.16 t1 collA value 000215 test_index 4.17 t1 collB length 000216 test_index 4.18 t2 collA value 000217 test_index 4.19 t2 collB length 000218 test_index 4.20 aux.t1 collA value 000219 test_index 4.21 aux.t1 collB length 000220 000221 # EVIDENCE-OF: R-58823-28748 If it identifies a specific database index, 000222 # then just that index is recreated. 000223 # 000224 set_collations length value 000225 do_execsql_test e_reindex-2.5.1 "REINDEX i1_a" {} 000226 test_index 5.2 t1 collA length 000227 test_index 5.3 t1 collB length 000228 test_index 5.4 t2 collA value 000229 test_index 5.5 t2 collB length 000230 test_index 5.6 aux.t1 collA value 000231 test_index 5.7 aux.t1 collB length 000232 do_execsql_test e_reindex-2.5.8 "REINDEX i2_b" {} 000233 test_index 5.9 t1 collA length 000234 test_index 5.10 t1 collB length 000235 test_index 5.11 t2 collA value 000236 test_index 5.12 t2 collB value 000237 test_index 5.13 aux.t1 collA value 000238 test_index 5.14 aux.t1 collB length 000239 do_execsql_test e_reindex-2.5.15 "REINDEX aux.i1_b" {} 000240 test_index 5.16 t1 collA length 000241 test_index 5.17 t1 collB length 000242 test_index 5.18 t2 collA value 000243 test_index 5.19 t2 collB value 000244 test_index 5.20 aux.t1 collA value 000245 test_index 5.21 aux.t1 collB value 000246 do_execsql_test e_reindex-2.5.22 "REINDEX i1_b" {} 000247 test_index 5.23 t1 collA length 000248 test_index 5.24 t1 collB value 000249 test_index 5.25 t2 collA value 000250 test_index 5.26 t2 collB value 000251 test_index 5.27 aux.t1 collA value 000252 test_index 5.28 aux.t1 collB value 000253 do_execsql_test e_reindex-2.5.29 "REINDEX i2_a" {} 000254 test_index 5.30 t1 collA length 000255 test_index 5.31 t1 collB value 000256 test_index 5.32 t2 collA length 000257 test_index 5.33 t2 collB value 000258 test_index 5.34 aux.t1 collA value 000259 test_index 5.35 aux.t1 collB value 000260 do_execsql_test e_reindex-2.5.36 "REINDEX aux.i1_a" {} 000261 test_index 5.37 t1 collA length 000262 test_index 5.38 t1 collB value 000263 test_index 5.39 t2 collA length 000264 test_index 5.40 t2 collB value 000265 test_index 5.41 aux.t1 collA length 000266 test_index 5.42 aux.t1 collB value 000267 000268 # EVIDENCE-OF: R-35892-30289 For a command of the form "REINDEX name", a 000269 # match against collation-name takes precedence over a match against 000270 # index-name or table-name. 000271 # 000272 set_collations value length 000273 do_execsql_test e_reindex-2.6.0 { 000274 CREATE TABLE collA(x); 000275 CREATE INDEX icolla_a ON collA(x COLLATE collA); 000276 CREATE INDEX icolla_b ON collA(x COLLATE collB); 000277 000278 INSERT INTO collA SELECT x FROM t1; 000279 } {} 000280 000281 test_index 6.1 collA collA value 000282 test_index 6.2 collA collB length 000283 000284 set_collations length value 000285 do_execsql_test e_reindex-2.6.3 "REINDEX collA" {} 000286 test_index 6.4 collA collA length 000287 test_index 6.5 collA collB length 000288 do_execsql_test e_reindex-2.6.3 "REINDEX main.collA" {} 000289 test_index 6.4 collA collA length 000290 test_index 6.5 collA collB value 000291 000292 set_collations value length 000293 do_execsql_test e_reindex-2.6.6 "REINDEX main.collA" {} 000294 test_index 6.7 collA collA value 000295 test_index 6.8 collA collB length 000296 000297 finish_test