modernc.org/cc@v1.0.1/v2/testdata/_sqlite/test/e_reindex.test (about)

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