1b7af4452Sdrh# 2007 May 02 2b7af4452Sdrh# 3b7af4452Sdrh# The author disclaims copyright to this source code. In place of 4b7af4452Sdrh# a legal notice, here is a blessing: 5b7af4452Sdrh# 6b7af4452Sdrh# May you do good and not evil. 7b7af4452Sdrh# May you find forgiveness for yourself and forgive others. 8b7af4452Sdrh# May you share freely, never taking more than you give. 9b7af4452Sdrh# 10b7af4452Sdrh#*********************************************************************** 11b7af4452Sdrh# This file implements regression tests for SQLite library. The 12b7af4452Sdrh# focus of this file is testing that it is OK to create new tables 13b7af4452Sdrh# and indices while creating existing tables and indices. 14b7af4452Sdrh# 15b7af4452Sdrh 16b7af4452Sdrhset testdir [file dirname $argv0] 17b7af4452Sdrhsource $testdir/tester.tcl 18b7af4452Sdrh 19847d3ab4Sdrhifcapable autovacuum { 20847d3ab4Sdrh set upperBound 2 21847d3ab4Sdrh} else { 22847d3ab4Sdrh set upperBound 0 23847d3ab4Sdrh} 24847d3ab4Sdrh 25b7af4452Sdrh# Run these tests for all possible values of autovacuum. 26b7af4452Sdrh# 27847d3ab4Sdrhfor {set av 0} {$av<=$upperBound} {incr av} { 28b7af4452Sdrh db close 29fda06befSmistachkin forcedelete test.db test.db-journal 30b7af4452Sdrh sqlite3 db test.db 31b7af4452Sdrh 32b7af4452Sdrh # Create a table that spans multiple pages. It is important 33b7af4452Sdrh # that part of the database be in pages beyond the root page. 34b7af4452Sdrh # 35b7af4452Sdrh do_test createtab-$av.1 { 36b7af4452Sdrh execsql "PRAGMA auto_vacuum=$av" 37b7af4452Sdrh execsql { 38b7af4452Sdrh PRAGMA page_size=1024; 39b7af4452Sdrh CREATE TABLE t1(x INTEGER PRIMARY KEY, y); 40b7af4452Sdrh INSERT INTO t1 VALUES(1, hex(randomblob(200))); 41b7af4452Sdrh INSERT INTO t1 VALUES(2, hex(randomblob(200))); 42b7af4452Sdrh INSERT INTO t1 VALUES(3, hex(randomblob(200))); 43b7af4452Sdrh INSERT INTO t1 VALUES(4, hex(randomblob(200))); 44b7af4452Sdrh SELECT count(*) FROM t1; 45b7af4452Sdrh } 46b7af4452Sdrh } {4} 474152e677Sdanielk1977 484152e677Sdanielk1977 set isUtf16 0 494152e677Sdanielk1977 ifcapable utf16 { 504152e677Sdanielk1977 set isUtf16 [expr {[execsql {PRAGMA encoding}] != "UTF-8"}] 514152e677Sdanielk1977 } 524152e677Sdanielk1977 53b7af4452Sdrh do_test createtab-$av.2 { 54b7af4452Sdrh file size test.db 554152e677Sdanielk1977 } [expr {1024*(4+($av!=0)+(${isUtf16}*2))}] 56b7af4452Sdrh 57b7af4452Sdrh # Start reading the table 58b7af4452Sdrh # 59b7af4452Sdrh do_test createtab-$av.3 { 60b7af4452Sdrh set STMT [sqlite3_prepare db {SELECT x FROM t1} -1 TAIL] 61b7af4452Sdrh sqlite3_step $STMT 62b7af4452Sdrh } {SQLITE_ROW} 63b7af4452Sdrh do_test createtab-$av.4 { 64b7af4452Sdrh sqlite3_column_int $STMT 0 65b7af4452Sdrh } {1} 66b7af4452Sdrh 67b7af4452Sdrh # While still reading the table, create a new table. 68b7af4452Sdrh # 69b7af4452Sdrh do_test createtab-$av.5 { 70b7af4452Sdrh execsql { 71b7af4452Sdrh CREATE TABLE t2(a,b); 72b7af4452Sdrh INSERT INTO t2 VALUES(1,2); 73b7af4452Sdrh SELECT * FROM t2; 74b7af4452Sdrh } 75b7af4452Sdrh } {1 2} 76b7af4452Sdrh 77b7af4452Sdrh # Continue reading the original table. 78b7af4452Sdrh # 79b7af4452Sdrh do_test createtab-$av.6 { 80b7af4452Sdrh sqlite3_column_int $STMT 0 81b7af4452Sdrh } {1} 82b7af4452Sdrh do_test createtab-$av.7 { 83b7af4452Sdrh sqlite3_step $STMT 84b7af4452Sdrh } {SQLITE_ROW} 85b7af4452Sdrh do_test createtab-$av.8 { 86b7af4452Sdrh sqlite3_column_int $STMT 0 87b7af4452Sdrh } {2} 88b7af4452Sdrh 89b7af4452Sdrh # Do another cycle of creating a new database table while contining 90b7af4452Sdrh # to read the original table. 91b7af4452Sdrh # 92b7af4452Sdrh do_test createtab-$av.11 { 93b7af4452Sdrh execsql { 94b7af4452Sdrh CREATE TABLE t3(a,b); 95b7af4452Sdrh INSERT INTO t3 VALUES(4,5); 96b7af4452Sdrh SELECT * FROM t3; 97b7af4452Sdrh } 98b7af4452Sdrh } {4 5} 99b7af4452Sdrh do_test createtab-$av.12 { 100b7af4452Sdrh sqlite3_column_int $STMT 0 101b7af4452Sdrh } {2} 102b7af4452Sdrh do_test createtab-$av.13 { 103b7af4452Sdrh sqlite3_step $STMT 104b7af4452Sdrh } {SQLITE_ROW} 105b7af4452Sdrh do_test createtab-$av.14 { 106b7af4452Sdrh sqlite3_column_int $STMT 0 107b7af4452Sdrh } {3} 108b7af4452Sdrh 109b7af4452Sdrh # One more cycle. 110b7af4452Sdrh # 111b7af4452Sdrh do_test createtab-$av.21 { 112b7af4452Sdrh execsql { 113b7af4452Sdrh CREATE TABLE t4(a,b); 114b7af4452Sdrh INSERT INTO t4 VALUES('abc','xyz'); 115b7af4452Sdrh SELECT * FROM t4; 116b7af4452Sdrh } 117b7af4452Sdrh } {abc xyz} 118b7af4452Sdrh do_test createtab-$av.22 { 119b7af4452Sdrh sqlite3_column_int $STMT 0 120b7af4452Sdrh } {3} 121b7af4452Sdrh do_test createtab-$av.23 { 122b7af4452Sdrh sqlite3_step $STMT 123b7af4452Sdrh } {SQLITE_ROW} 124b7af4452Sdrh do_test createtab-$av.24 { 125b7af4452Sdrh sqlite3_column_int $STMT 0 126b7af4452Sdrh } {4} 127b7af4452Sdrh 128b7af4452Sdrh # Finish reading. Do an integrity check on the database. 129b7af4452Sdrh # 130b7af4452Sdrh do_test createtab-$av.30 { 131b7af4452Sdrh sqlite3_step $STMT 132b7af4452Sdrh } {SQLITE_DONE} 133b7af4452Sdrh do_test createtab-$av.31 { 134b7af4452Sdrh sqlite3_finalize $STMT 135b7af4452Sdrh } {SQLITE_OK} 136b7af4452Sdrh do_test createtab-$av.32 { 137b7af4452Sdrh execsql { 138b7af4452Sdrh SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1 139b7af4452Sdrh } 140b7af4452Sdrh } {t1 t2 t3 t4} 141b7af4452Sdrh integrity_check createtab-$av.40 142b7af4452Sdrh 143b7af4452Sdrh} 144b7af4452Sdrh 145*8fe25c64Sdrh# 2019-03-31 Ensure that a proper error is returned for an index 146*8fe25c64Sdrh# with too many columns. 147*8fe25c64Sdrh# 148*8fe25c64Sdrhdo_test createtab-3.1 { 149*8fe25c64Sdrh db eval {DROP TABLE IF EXISTS t1;} 150*8fe25c64Sdrh set sql "CREATE TABLE t1(x,UNIQUE(x[string repeat ,x 100000]))" 151*8fe25c64Sdrh catchsql $sql 152*8fe25c64Sdrh} {1 {too many columns in index}} 153*8fe25c64Sdrh 154b7af4452Sdrhfinish_test 155