135ac58eaSdan# 2016 December 15 235ac58eaSdan# 335ac58eaSdan# The author disclaims copyright to this source code. In place of 435ac58eaSdan# a legal notice, here is a blessing: 535ac58eaSdan# 635ac58eaSdan# May you do good and not evil. 735ac58eaSdan# May you find forgiveness for yourself and forgive others. 835ac58eaSdan# May you share freely, never taking more than you give. 935ac58eaSdan# 1035ac58eaSdan#*********************************************************************** 1135ac58eaSdan# 1211da002cSdan# Test the shell tool ".lint fkey-indexes" command. 1311da002cSdan# 1435ac58eaSdan 1535ac58eaSdanset testdir [file dirname $argv0] 1635ac58eaSdansource $testdir/tester.tcl 17c5954199Sdrhifcapable !vtab {finish_test; return} 1835ac58eaSdanset testprefix shell6 1935ac58eaSdanset CLI [test_find_cli] 2035ac58eaSdandb close 2135ac58eaSdanforcedelete test.db test.db-journal test.db-wal 2235ac58eaSdan 2335ac58eaSdanforeach {tn schema output} { 2435ac58eaSdan 1 { 2535ac58eaSdan CREATE TABLE p1(a PRIMARY KEY, b); 2635ac58eaSdan CREATE TABLE c1(x, y REFERENCES p1); 2735ac58eaSdan } { 2835ac58eaSdan CREATE INDEX 'c1_y' ON 'c1'('y'); --> p1(a) 2935ac58eaSdan } 3035ac58eaSdan 3135ac58eaSdan 2 { 3235ac58eaSdan CREATE TABLE p1(a PRIMARY KEY, b); 3335ac58eaSdan CREATE TABLE c2(x REFERENCES p1, y REFERENCES p1); 3435ac58eaSdan } { 3535ac58eaSdan CREATE INDEX 'c2_y' ON 'c2'('y'); --> p1(a) 3635ac58eaSdan CREATE INDEX 'c2_x' ON 'c2'('x'); --> p1(a) 3735ac58eaSdan } 3835ac58eaSdan 3935ac58eaSdan 3 { 4035ac58eaSdan CREATE TABLE 'p 1'(a, b, c, PRIMARY KEY(c, b)); 4135ac58eaSdan CREATE TABLE 'c 1'(x, y, z, FOREIGN KEY (z, y) REFERENCES 'p 1'); 4235ac58eaSdan } { 4335ac58eaSdan CREATE INDEX 'c 1_z_y' ON 'c 1'('z', 'y'); --> p 1(c,b) 4435ac58eaSdan } 4535ac58eaSdan 4635ac58eaSdan 4 { 4735ac58eaSdan CREATE TABLE p1(a, 'b b b' PRIMARY KEY); 4835ac58eaSdan CREATE TABLE c1('x y z' REFERENCES p1); 4935ac58eaSdan CREATE INDEX i1 ON c1('x y z') WHERE "x y z" IS NOT NULL; 5035ac58eaSdan } { 5135ac58eaSdan } 5235ac58eaSdan 5335ac58eaSdan 5 { 5435ac58eaSdan CREATE TABLE p1(a, 'b b b' PRIMARY KEY); 5535ac58eaSdan CREATE TABLE c1('x y z' REFERENCES p1); 5635ac58eaSdan CREATE INDEX i1 ON c1('x y z') WHERE "x y z" IS NOT 12; 5735ac58eaSdan } { 5835ac58eaSdan CREATE INDEX 'c1_x y z' ON 'c1'('x y z'); --> p1(b b b) 5935ac58eaSdan } 6035ac58eaSdan 6135ac58eaSdan 6 { 6235ac58eaSdan CREATE TABLE x1(a, b, c, UNIQUE(a, b)); 6335ac58eaSdan CREATE TABLE y1(a, b, c, FOREIGN KEY(b, a) REFERENCES x1(a, b)); 6435ac58eaSdan CREATE INDEX y1i ON y1(a, c, b); 6535ac58eaSdan } { 6635ac58eaSdan CREATE INDEX 'y1_b_a' ON 'y1'('b', 'a'); --> x1(a,b) 6735ac58eaSdan } 6835ac58eaSdan 6935ac58eaSdan 6 { 7035ac58eaSdan CREATE TABLE x1(a COLLATE nocase, b, UNIQUE(a)); 7135ac58eaSdan CREATE TABLE y1(a COLLATE rtrim REFERENCES x1(a)); 7235ac58eaSdan } { 7335ac58eaSdan CREATE INDEX 'y1_a' ON 'y1'('a' COLLATE nocase); --> x1(a) 7435ac58eaSdan } 7535ac58eaSdan 7650da9382Sdan 7 { 7750da9382Sdan CREATE TABLE x1(a PRIMARY KEY COLLATE nocase, b); 7850da9382Sdan CREATE TABLE y1(a REFERENCES x1); 7950da9382Sdan } { 8050da9382Sdan CREATE INDEX 'y1_a' ON 'y1'('a' COLLATE nocase); --> x1(a) 8150da9382Sdan } 8250da9382Sdan 8350da9382Sdan 8 { 8450da9382Sdan CREATE TABLE x1(a, b COLLATE nocase, c COLLATE rtrim, PRIMARY KEY(c, b, a)); 8550da9382Sdan CREATE TABLE y1(d, e, f, FOREIGN KEY(d, e, f) REFERENCES x1); 8650da9382Sdan } { 8750da9382Sdan CREATE INDEX 'y1_d_e_f' ON 'y1'('d' COLLATE rtrim, 'e' COLLATE nocase, 'f'); --> x1(c,b,a) 8850da9382Sdan } 8950da9382Sdan 9054e2efcdSdan 9 { 9154e2efcdSdan CREATE TABLE p1(a, b UNIQUE); 9254e2efcdSdan CREATE TABLE c1(x INTEGER PRIMARY KEY REFERENCES p1(b)); 9354e2efcdSdan } { 9454e2efcdSdan } 9554e2efcdSdan 96*f9679318Sdan 10 { 97*f9679318Sdan CREATE TABLE parent (id INTEGER PRIMARY KEY); 98*f9679318Sdan CREATE TABLE child2 (id INT PRIMARY KEY, parentID INT REFERENCES parent) 99*f9679318Sdan WITHOUT ROWID; 100*f9679318Sdan } { 101*f9679318Sdan CREATE INDEX 'child2_parentID' ON 'child2'('parentID'); --> parent(id) 102*f9679318Sdan } 103*f9679318Sdan 10435ac58eaSdan} { 10535ac58eaSdan forcedelete test.db 10635ac58eaSdan sqlite3 db test.db 10735ac58eaSdan execsql $schema 10835ac58eaSdan 10935ac58eaSdan set expected "" 11035ac58eaSdan foreach line [split $output "\n"] { 11135ac58eaSdan set line [string trim $line] 11235ac58eaSdan if {$line!=""} { 11335ac58eaSdan append expected "$line\n" 11435ac58eaSdan } 11535ac58eaSdan } 11635ac58eaSdan 11735ac58eaSdan do_test 1.$tn.1 { 1183c7ebeb5Sdan set RES [catchcmd test.db [list .lint fkey-indexes]] 11935ac58eaSdan } [list 0 [string trim $expected]] 12035ac58eaSdan 12135ac58eaSdan do_test 1.$tn.2 { 12235ac58eaSdan execsql [lindex $RES 1] 1233c7ebeb5Sdan catchcmd test.db [list .lint fkey-indexes] 12435ac58eaSdan } {0 {}} 12535ac58eaSdan 12635ac58eaSdan db close 12735ac58eaSdan} 12835ac58eaSdan 12935ac58eaSdanfinish_test 130