1cd61c281Sdrh# 2002 March 6 2cd61c281Sdrh# 3cd61c281Sdrh# The author disclaims copyright to this source code. In place of 4cd61c281Sdrh# a legal notice, here is a blessing: 5cd61c281Sdrh# 6cd61c281Sdrh# May you do good and not evil. 7cd61c281Sdrh# May you find forgiveness for yourself and forgive others. 8cd61c281Sdrh# May you share freely, never taking more than you give. 9cd61c281Sdrh# 10cd61c281Sdrh#*********************************************************************** 11cd61c281Sdrh# This file implements regression tests for SQLite library. 12cd61c281Sdrh# 13cd61c281Sdrh# This file implements tests for the PRAGMA command. 14cd61c281Sdrh# 15*1941a3e8Sdrh# $Id: pragma.test,v 1.47 2007/01/22 13:02:24 drh Exp $ 16cd61c281Sdrh 17cd61c281Sdrhset testdir [file dirname $argv0] 18cd61c281Sdrhsource $testdir/tester.tcl 19cd61c281Sdrh 2091cf71b0Sdanielk1977# Test organization: 2191cf71b0Sdanielk1977# 2291cf71b0Sdanielk1977# pragma-1.*: Test cache_size, default_cache_size and synchronous on main db. 2391cf71b0Sdanielk1977# pragma-2.*: Test synchronous on attached db. 2491cf71b0Sdanielk1977# pragma-3.*: Test detection of table/index inconsistency by integrity_check. 2591cf71b0Sdanielk1977# pragma-4.*: Test cache_size and default_cache_size on attached db. 2691cf71b0Sdanielk1977# pragma-5.*: Test that pragma synchronous may not be used inside of a 2791cf71b0Sdanielk1977# transaction. 28cc6bd383Sdanielk1977# pragma-6.*: Test schema-query pragmas. 29cc6bd383Sdanielk1977# pragma-7.*: Miscellaneous tests. 30cc6bd383Sdanielk1977# pragma-8.*: Test user_version and schema_version pragmas. 319a09a3caStpoindex# pragma-9.*: Test temp_store and temp_store_directory. 32cc6bd383Sdanielk1977# pragma-10.*: Test the count_changes pragma in the presence of triggers. 3348af65aeSdanielk1977# pragma-11.*: Test the collation_list pragma. 3491cf71b0Sdanielk1977# 3591cf71b0Sdanielk1977 3605a82983Sdrhifcapable !pragma { 3705a82983Sdrh finish_test 3805a82983Sdrh return 3905a82983Sdrh} 4005a82983Sdrh 41cd61c281Sdrh# Delete the preexisting database to avoid the special setup 42cd61c281Sdrh# that the "all.test" script does. 43cd61c281Sdrh# 44cd61c281Sdrhdb close 4525403650Sdrhfile delete test.db test.db-journal 4625403650Sdrhfile delete test3.db test3.db-journal 47dddca286Sdrhsqlite3 db test.db; set DB [sqlite3_connection_pointer db] 48cd61c281Sdrh 49c7b4a44cSdanielk1977ifcapable pager_pragmas { 50cd61c281Sdrhdo_test pragma-1.1 { 51cd61c281Sdrh execsql { 52cd61c281Sdrh PRAGMA cache_size; 53cd61c281Sdrh PRAGMA default_cache_size; 54cd61c281Sdrh PRAGMA synchronous; 55cd61c281Sdrh } 5691cf71b0Sdanielk1977} {2000 2000 2} 57cd61c281Sdrhdo_test pragma-1.2 { 58cd61c281Sdrh execsql { 59eb43e5c7Sdrh PRAGMA synchronous=OFF; 60cd61c281Sdrh PRAGMA cache_size=1234; 61cd61c281Sdrh PRAGMA cache_size; 62cd61c281Sdrh PRAGMA default_cache_size; 63cd61c281Sdrh PRAGMA synchronous; 64cd61c281Sdrh } 65eb43e5c7Sdrh} {1234 2000 0} 66cd61c281Sdrhdo_test pragma-1.3 { 67cd61c281Sdrh db close 68ef4ac8f9Sdrh sqlite3 db test.db 69cd61c281Sdrh execsql { 70cd61c281Sdrh PRAGMA cache_size; 71cd61c281Sdrh PRAGMA default_cache_size; 72cd61c281Sdrh PRAGMA synchronous; 73cd61c281Sdrh } 7491cf71b0Sdanielk1977} {2000 2000 2} 75cd61c281Sdrhdo_test pragma-1.4 { 76cd61c281Sdrh execsql { 77cd61c281Sdrh PRAGMA synchronous=OFF; 78cd61c281Sdrh PRAGMA cache_size; 79cd61c281Sdrh PRAGMA default_cache_size; 80cd61c281Sdrh PRAGMA synchronous; 81cd61c281Sdrh } 8291cf71b0Sdanielk1977} {2000 2000 0} 83cd61c281Sdrhdo_test pragma-1.5 { 84cd61c281Sdrh execsql { 85cd61c281Sdrh PRAGMA cache_size=4321; 86cd61c281Sdrh PRAGMA cache_size; 87cd61c281Sdrh PRAGMA default_cache_size; 88cd61c281Sdrh PRAGMA synchronous; 89cd61c281Sdrh } 9091cf71b0Sdanielk1977} {4321 2000 0} 91cd61c281Sdrhdo_test pragma-1.6 { 92cd61c281Sdrh execsql { 93cd61c281Sdrh PRAGMA synchronous=ON; 94cd61c281Sdrh PRAGMA cache_size; 95cd61c281Sdrh PRAGMA default_cache_size; 96cd61c281Sdrh PRAGMA synchronous; 97cd61c281Sdrh } 9891cf71b0Sdanielk1977} {4321 2000 1} 99cd61c281Sdrhdo_test pragma-1.7 { 100cd61c281Sdrh db close 101ef4ac8f9Sdrh sqlite3 db test.db 102cd61c281Sdrh execsql { 103cd61c281Sdrh PRAGMA cache_size; 104cd61c281Sdrh PRAGMA default_cache_size; 105cd61c281Sdrh PRAGMA synchronous; 106cd61c281Sdrh } 10791cf71b0Sdanielk1977} {2000 2000 2} 108cd61c281Sdrhdo_test pragma-1.8 { 109cd61c281Sdrh execsql { 110cd61c281Sdrh PRAGMA default_cache_size=123; 111cd61c281Sdrh PRAGMA cache_size; 112cd61c281Sdrh PRAGMA default_cache_size; 113cd61c281Sdrh PRAGMA synchronous; 114cd61c281Sdrh } 11591cf71b0Sdanielk1977} {123 123 2} 116802d69a7Sdrhdo_test pragma-1.9.1 { 117cd61c281Sdrh db close 118dddca286Sdrh sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db] 119cd61c281Sdrh execsql { 120cd61c281Sdrh PRAGMA cache_size; 121cd61c281Sdrh PRAGMA default_cache_size; 122cd61c281Sdrh PRAGMA synchronous; 123cd61c281Sdrh } 12491cf71b0Sdanielk1977} {123 123 2} 125802d69a7Sdrhifcapable vacuum { 126802d69a7Sdrh do_test pragma-1.9.2 { 127802d69a7Sdrh execsql { 128802d69a7Sdrh VACUUM; 129802d69a7Sdrh PRAGMA cache_size; 130802d69a7Sdrh PRAGMA default_cache_size; 131802d69a7Sdrh PRAGMA synchronous; 132802d69a7Sdrh } 133802d69a7Sdrh } {123 123 2} 134802d69a7Sdrh} 13591cf71b0Sdanielk1977do_test pragma-1.10 { 1365a38705eSdrh execsql { 1374303feedSdrh PRAGMA synchronous=NORMAL; 1384303feedSdrh PRAGMA cache_size; 1394303feedSdrh PRAGMA default_cache_size; 1404303feedSdrh PRAGMA synchronous; 1414303feedSdrh } 14291cf71b0Sdanielk1977} {123 123 1} 14391cf71b0Sdanielk1977do_test pragma-1.11 { 1444303feedSdrh execsql { 1454303feedSdrh PRAGMA synchronous=FULL; 1464303feedSdrh PRAGMA cache_size; 1474303feedSdrh PRAGMA default_cache_size; 1484303feedSdrh PRAGMA synchronous; 1494303feedSdrh } 15091cf71b0Sdanielk1977} {123 123 2} 15191cf71b0Sdanielk1977do_test pragma-1.12 { 1524303feedSdrh db close 153dddca286Sdrh sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db] 1544303feedSdrh execsql { 1554303feedSdrh PRAGMA cache_size; 1564303feedSdrh PRAGMA default_cache_size; 1574303feedSdrh PRAGMA synchronous; 1584303feedSdrh } 15991cf71b0Sdanielk1977} {123 123 2} 16091cf71b0Sdanielk1977 1615260f7e9Sdrh# Make sure the pragma handler understands numeric values in addition 1625260f7e9Sdrh# to keywords like "off" and "full". 1635260f7e9Sdrh# 1645260f7e9Sdrhdo_test pragma-1.13 { 1655260f7e9Sdrh execsql { 1665260f7e9Sdrh PRAGMA synchronous=0; 1675260f7e9Sdrh PRAGMA synchronous; 1685260f7e9Sdrh } 1695260f7e9Sdrh} {0} 1705260f7e9Sdrhdo_test pragma-1.14 { 1715260f7e9Sdrh execsql { 1725260f7e9Sdrh PRAGMA synchronous=2; 1735260f7e9Sdrh PRAGMA synchronous; 1745260f7e9Sdrh } 1755260f7e9Sdrh} {2} 176c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas 1775260f7e9Sdrh 1785260f7e9Sdrh# Test turning "flag" pragmas on and off. 1795260f7e9Sdrh# 1805260f7e9Sdrhdo_test pragma-1.15 { 1815260f7e9Sdrh execsql { 1825260f7e9Sdrh PRAGMA vdbe_listing=YES; 1835260f7e9Sdrh PRAGMA vdbe_listing; 1845260f7e9Sdrh } 1855260f7e9Sdrh} {1} 1865260f7e9Sdrhdo_test pragma-1.16 { 1875260f7e9Sdrh execsql { 1885260f7e9Sdrh PRAGMA vdbe_listing=NO; 1895260f7e9Sdrh PRAGMA vdbe_listing; 1905260f7e9Sdrh } 1915260f7e9Sdrh} {0} 1925260f7e9Sdrhdo_test pragma-1.17 { 1935260f7e9Sdrh execsql { 1945260f7e9Sdrh PRAGMA parser_trace=ON; 1955260f7e9Sdrh PRAGMA parser_trace=OFF; 1965260f7e9Sdrh } 1975260f7e9Sdrh} {} 1985260f7e9Sdrhdo_test pragma-1.18 { 1995260f7e9Sdrh execsql { 2005260f7e9Sdrh PRAGMA bogus = -1234; -- Parsing of negative values 2015260f7e9Sdrh } 2025260f7e9Sdrh} {} 2035260f7e9Sdrh 20491cf71b0Sdanielk1977# Test modifying the safety_level of an attached database. 20591cf71b0Sdanielk1977do_test pragma-2.1 { 20691cf71b0Sdanielk1977 file delete -force test2.db 20791cf71b0Sdanielk1977 file delete -force test2.db-journal 2084303feedSdrh execsql { 20991cf71b0Sdanielk1977 ATTACH 'test2.db' AS aux; 2104303feedSdrh } 21191cf71b0Sdanielk1977} {} 212c7b4a44cSdanielk1977ifcapable pager_pragmas { 21391cf71b0Sdanielk1977do_test pragma-2.2 { 2144303feedSdrh execsql { 21591cf71b0Sdanielk1977 pragma aux.synchronous; 2164303feedSdrh } 21791cf71b0Sdanielk1977} {2} 21891cf71b0Sdanielk1977do_test pragma-2.3 { 21991cf71b0Sdanielk1977 execsql { 22091cf71b0Sdanielk1977 pragma aux.synchronous = OFF; 22191cf71b0Sdanielk1977 pragma aux.synchronous; 22291cf71b0Sdanielk1977 pragma synchronous; 22391cf71b0Sdanielk1977 } 22491cf71b0Sdanielk1977} {0 2} 22591cf71b0Sdanielk1977do_test pragma-2.4 { 22691cf71b0Sdanielk1977 execsql { 22791cf71b0Sdanielk1977 pragma aux.synchronous = ON; 22891cf71b0Sdanielk1977 pragma synchronous; 22991cf71b0Sdanielk1977 pragma aux.synchronous; 23091cf71b0Sdanielk1977 } 23191cf71b0Sdanielk1977} {2 1} 232c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas 2334303feedSdrh 234ed717fe3Sdrh# Construct a corrupted index and make sure the integrity_check 235ed717fe3Sdrh# pragma finds it. 236ed717fe3Sdrh# 23725d6543dSdrh# These tests won't work if the database is encrypted 23825d6543dSdrh# 239ed717fe3Sdrhdo_test pragma-3.1 { 240ed717fe3Sdrh execsql { 241ed717fe3Sdrh BEGIN; 242ed717fe3Sdrh CREATE TABLE t2(a,b,c); 243ed717fe3Sdrh CREATE INDEX i2 ON t2(a); 244ed717fe3Sdrh INSERT INTO t2 VALUES(11,2,3); 245ed717fe3Sdrh INSERT INTO t2 VALUES(22,3,4); 246ed717fe3Sdrh COMMIT; 247ed717fe3Sdrh SELECT rowid, * from t2; 248ed717fe3Sdrh } 249ed717fe3Sdrh} {1 11 2 3 2 22 3 4} 25040e016e4Sdrhif {![sqlite3 -has-codec] && $sqlite_options(integrityck)} { 251ed717fe3Sdrh do_test pragma-3.2 { 252ed717fe3Sdrh set rootpage [execsql {SELECT rootpage FROM sqlite_master WHERE name='i2'}] 25396fc5fe6Sdanielk1977 set db [btree_open test.db 100 0] 254ed717fe3Sdrh btree_begin_transaction $db 255ed717fe3Sdrh set c [btree_cursor $db $rootpage 1] 256ed717fe3Sdrh btree_first $c 257ed717fe3Sdrh btree_delete $c 258ed717fe3Sdrh btree_commit $db 259ed717fe3Sdrh btree_close $db 260ed717fe3Sdrh execsql {PRAGMA integrity_check} 2614be295beSdrh } {{rowid 1 missing from index i2} {wrong # of entries in index i2}} 26225d6543dSdrh} 26391cf71b0Sdanielk1977do_test pragma-3.3 { 26491cf71b0Sdanielk1977 execsql { 26591cf71b0Sdanielk1977 DROP INDEX i2; 26691cf71b0Sdanielk1977 } 26791cf71b0Sdanielk1977} {} 26891cf71b0Sdanielk1977 26991cf71b0Sdanielk1977# Test modifying the cache_size of an attached database. 270c7b4a44cSdanielk1977ifcapable pager_pragmas { 27191cf71b0Sdanielk1977do_test pragma-4.1 { 27291cf71b0Sdanielk1977 execsql { 27391cf71b0Sdanielk1977 pragma aux.cache_size; 27491cf71b0Sdanielk1977 pragma aux.default_cache_size; 27591cf71b0Sdanielk1977 } 27691cf71b0Sdanielk1977} {2000 2000} 2771bdd9b57Sdrhdo_test pragma-4.2 { 2781bdd9b57Sdrh execsql { 27991cf71b0Sdanielk1977 pragma aux.cache_size = 50; 28091cf71b0Sdanielk1977 pragma aux.cache_size; 28191cf71b0Sdanielk1977 pragma aux.default_cache_size; 2821bdd9b57Sdrh } 28391cf71b0Sdanielk1977} {50 2000} 2841bdd9b57Sdrhdo_test pragma-4.3 { 2851bdd9b57Sdrh execsql { 28691cf71b0Sdanielk1977 pragma aux.default_cache_size = 456; 28791cf71b0Sdanielk1977 pragma aux.cache_size; 28891cf71b0Sdanielk1977 pragma aux.default_cache_size; 2891bdd9b57Sdrh } 29091cf71b0Sdanielk1977} {456 456} 2911bdd9b57Sdrhdo_test pragma-4.4 { 2921bdd9b57Sdrh execsql { 29391cf71b0Sdanielk1977 pragma cache_size; 29491cf71b0Sdanielk1977 pragma default_cache_size; 2951bdd9b57Sdrh } 29691cf71b0Sdanielk1977} {123 123} 2971bdd9b57Sdrhdo_test pragma-4.5 { 2981bdd9b57Sdrh execsql { 29991cf71b0Sdanielk1977 DETACH aux; 30091cf71b0Sdanielk1977 ATTACH 'test3.db' AS aux; 30191cf71b0Sdanielk1977 pragma aux.cache_size; 30291cf71b0Sdanielk1977 pragma aux.default_cache_size; 3031bdd9b57Sdrh } 30491cf71b0Sdanielk1977} {2000 2000} 3051bdd9b57Sdrhdo_test pragma-4.6 { 3061bdd9b57Sdrh execsql { 30791cf71b0Sdanielk1977 DETACH aux; 30891cf71b0Sdanielk1977 ATTACH 'test2.db' AS aux; 30991cf71b0Sdanielk1977 pragma aux.cache_size; 31091cf71b0Sdanielk1977 pragma aux.default_cache_size; 3111bdd9b57Sdrh } 31291cf71b0Sdanielk1977} {456 456} 313c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas 3141bdd9b57Sdrh 31591cf71b0Sdanielk1977# Test that modifying the sync-level in the middle of a transaction is 31691cf71b0Sdanielk1977# disallowed. 317c7b4a44cSdanielk1977ifcapable pager_pragmas { 31891cf71b0Sdanielk1977do_test pragma-5.0 { 3191bdd9b57Sdrh execsql { 32091cf71b0Sdanielk1977 pragma synchronous; 3211bdd9b57Sdrh } 32291cf71b0Sdanielk1977} {2} 32391cf71b0Sdanielk1977do_test pragma-5.1 { 3241bdd9b57Sdrh catchsql { 3251bdd9b57Sdrh BEGIN; 32691cf71b0Sdanielk1977 pragma synchronous = OFF; 3271bdd9b57Sdrh } 32891cf71b0Sdanielk1977} {1 {Safety level may not be changed inside a transaction}} 32991cf71b0Sdanielk1977do_test pragma-5.2 { 33091cf71b0Sdanielk1977 execsql { 33191cf71b0Sdanielk1977 pragma synchronous; 33291cf71b0Sdanielk1977 } 33391cf71b0Sdanielk1977} {2} 3345260f7e9Sdrhcatchsql {COMMIT;} 335c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas 3365260f7e9Sdrh 3375260f7e9Sdrh# Test schema-query pragmas 3385260f7e9Sdrh# 33927188fb5Sdanielk1977ifcapable schema_pragmas { 34053c0f748Sdanielk1977ifcapable tempdb { 3415260f7e9Sdrh do_test pragma-6.1 { 342956bc92cSdrh set res {} 34340e016e4Sdrh execsql {SELECT * FROM sqlite_temp_master} 3445260f7e9Sdrh foreach {idx name file} [execsql {pragma database_list}] { 3455260f7e9Sdrh lappend res $idx $name 3465260f7e9Sdrh } 3475260f7e9Sdrh set res 348a71aa001Sdrh } {0 main 1 temp 2 aux} 34953c0f748Sdanielk1977} 3505260f7e9Sdrhdo_test pragma-6.2 { 3515260f7e9Sdrh execsql { 3525260f7e9Sdrh pragma table_info(t2) 3535260f7e9Sdrh } 354bfa8b102Sdrh} {0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0} 355736c7d4bSdrhdb nullvalue <<NULL>> 356417ec638Sdrhdo_test pragma-6.2.2 { 357417ec638Sdrh execsql { 358736c7d4bSdrh CREATE TABLE t5( 359736c7d4bSdrh a TEXT DEFAULT CURRENT_TIMESTAMP, 360736c7d4bSdrh b DEFAULT (5+3), 361736c7d4bSdrh c TEXT, 362736c7d4bSdrh d INTEGER DEFAULT NULL, 363736c7d4bSdrh e TEXT DEFAULT '' 364736c7d4bSdrh ); 365417ec638Sdrh PRAGMA table_info(t5); 366417ec638Sdrh } 367736c7d4bSdrh} {0 a TEXT 0 CURRENT_TIMESTAMP 0 1 b {} 0 5+3 0 2 c TEXT 0 <<NULL>> 0 3 d INTEGER 0 NULL 0 4 e TEXT 0 '' 0} 368736c7d4bSdrhdb nullvalue {} 3696bf89570Sdrhifcapable {foreignkey} { 3705260f7e9Sdrh do_test pragma-6.3 { 3715260f7e9Sdrh execsql { 3725260f7e9Sdrh CREATE TABLE t3(a int references t2(b), b UNIQUE); 3735260f7e9Sdrh pragma foreign_key_list(t3); 3745260f7e9Sdrh } 3755260f7e9Sdrh } {0 0 t2 a b} 3765260f7e9Sdrh do_test pragma-6.4 { 3775260f7e9Sdrh execsql { 3785260f7e9Sdrh pragma index_list(t3); 3795260f7e9Sdrh } 3805260f7e9Sdrh } {0 sqlite_autoindex_t3_1 1} 3816bf89570Sdrh} 3826bf89570Sdrhifcapable {!foreignkey} { 3836bf89570Sdrh execsql {CREATE TABLE t3(a,b UNIQUE)} 3846bf89570Sdrh} 3855260f7e9Sdrhdo_test pragma-6.5 { 3865260f7e9Sdrh execsql { 3875260f7e9Sdrh CREATE INDEX t3i1 ON t3(a,b); 3885260f7e9Sdrh pragma index_info(t3i1); 3895260f7e9Sdrh } 3905260f7e9Sdrh} {0 0 a 1 1 b} 39127188fb5Sdanielk1977} ;# ifcapable schema_pragmas 3925260f7e9Sdrh# Miscellaneous tests 3935260f7e9Sdrh# 39427188fb5Sdanielk1977ifcapable schema_pragmas { 3955260f7e9Sdrhdo_test pragma-7.1 { 3965260f7e9Sdrh # Make sure a pragma knows to read the schema if it needs to 3975260f7e9Sdrh db close 3985260f7e9Sdrh sqlite3 db test.db 3995260f7e9Sdrh execsql { 4005260f7e9Sdrh pragma index_list(t3); 4015260f7e9Sdrh } 4025260f7e9Sdrh} {0 t3i1 0 1 sqlite_autoindex_t3_1 1} 40327188fb5Sdanielk1977} ;# ifcapable schema_pragmas 4046c62608fSdrhifcapable {utf16} { 4055260f7e9Sdrh do_test pragma-7.2 { 4065260f7e9Sdrh db close 4075260f7e9Sdrh sqlite3 db test.db 4085260f7e9Sdrh catchsql { 4095260f7e9Sdrh pragma encoding=bogus; 4105260f7e9Sdrh } 4115260f7e9Sdrh } {1 {unsupported encoding: bogus}} 4126c62608fSdrh} 41353c0f748Sdanielk1977ifcapable tempdb { 4145260f7e9Sdrh do_test pragma-7.3 { 4155260f7e9Sdrh db close 4165260f7e9Sdrh sqlite3 db test.db 4175260f7e9Sdrh execsql { 4185260f7e9Sdrh pragma lock_status; 4195260f7e9Sdrh } 4205260f7e9Sdrh } {main unlocked temp closed} 42153c0f748Sdanielk1977} else { 42253c0f748Sdanielk1977 do_test pragma-7.3 { 42353c0f748Sdanielk1977 db close 42453c0f748Sdanielk1977 sqlite3 db test.db 42553c0f748Sdanielk1977 execsql { 42653c0f748Sdanielk1977 pragma lock_status; 42753c0f748Sdanielk1977 } 42853c0f748Sdanielk1977 } {main unlocked} 42953c0f748Sdanielk1977} 4305260f7e9Sdrh 4315260f7e9Sdrh 432dae2495bSdanielk1977#---------------------------------------------------------------------- 433b92b70bbSdanielk1977# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA 434b92b70bbSdanielk1977# user_version" statements. 435dae2495bSdanielk1977# 436b92b70bbSdanielk1977# pragma-8.1: PRAGMA schema_version 437b92b70bbSdanielk1977# pragma-8.2: PRAGMA user_version 438dae2495bSdanielk1977# 439dae2495bSdanielk1977 44011cf9fbcSdanielk1977ifcapable schema_version { 44111cf9fbcSdanielk1977 442b92b70bbSdanielk1977# First check that we can set the schema version and then retrieve the 443dae2495bSdanielk1977# same value. 444dae2495bSdanielk1977do_test pragma-8.1.1 { 445dae2495bSdanielk1977 execsql { 446b92b70bbSdanielk1977 PRAGMA schema_version = 105; 447dae2495bSdanielk1977 } 448dae2495bSdanielk1977} {} 449dae2495bSdanielk1977do_test pragma-8.1.2 { 45025403650Sdrh execsql2 { 451b92b70bbSdanielk1977 PRAGMA schema_version; 452dae2495bSdanielk1977 } 45325403650Sdrh} {schema_version 105} 454dae2495bSdanielk1977do_test pragma-8.1.3 { 455dae2495bSdanielk1977 execsql { 456b92b70bbSdanielk1977 PRAGMA schema_version = 106; 457dae2495bSdanielk1977 } 458dae2495bSdanielk1977} {} 459dae2495bSdanielk1977do_test pragma-8.1.4 { 460dae2495bSdanielk1977 execsql { 461b92b70bbSdanielk1977 PRAGMA schema_version; 462dae2495bSdanielk1977 } 463dae2495bSdanielk1977} 106 464dae2495bSdanielk1977 465b92b70bbSdanielk1977# Check that creating a table modifies the schema-version (this is really 466b92b70bbSdanielk1977# to verify that the value being read is in fact the schema version). 467dae2495bSdanielk1977do_test pragma-8.1.5 { 468dae2495bSdanielk1977 execsql { 469dae2495bSdanielk1977 CREATE TABLE t4(a, b, c); 470dae2495bSdanielk1977 INSERT INTO t4 VALUES(1, 2, 3); 471dae2495bSdanielk1977 SELECT * FROM t4; 472dae2495bSdanielk1977 } 473dae2495bSdanielk1977} {1 2 3} 474dae2495bSdanielk1977do_test pragma-8.1.6 { 475dae2495bSdanielk1977 execsql { 476b92b70bbSdanielk1977 PRAGMA schema_version; 477dae2495bSdanielk1977 } 478dae2495bSdanielk1977} 107 479dae2495bSdanielk1977 480dae2495bSdanielk1977# Now open a second connection to the database. Ensure that changing the 481b92b70bbSdanielk1977# schema-version using the first connection forces the second connection 482dae2495bSdanielk1977# to reload the schema. This has to be done using the C-API test functions, 483dae2495bSdanielk1977# because the TCL API accounts for SCHEMA_ERROR and retries the query. 484dae2495bSdanielk1977do_test pragma-8.1.7 { 485dddca286Sdrh sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2] 486dae2495bSdanielk1977 execsql { 487dae2495bSdanielk1977 SELECT * FROM t4; 488dae2495bSdanielk1977 } db2 489dae2495bSdanielk1977} {1 2 3} 490dae2495bSdanielk1977do_test pragma-8.1.8 { 491dae2495bSdanielk1977 execsql { 492b92b70bbSdanielk1977 PRAGMA schema_version = 108; 493dae2495bSdanielk1977 } 494dae2495bSdanielk1977} {} 495dae2495bSdanielk1977do_test pragma-8.1.9 { 496dae2495bSdanielk1977 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY] 497dae2495bSdanielk1977 sqlite3_step $::STMT 498dae2495bSdanielk1977} SQLITE_ERROR 499dae2495bSdanielk1977do_test pragma-8.1.10 { 500dae2495bSdanielk1977 sqlite3_finalize $::STMT 501dae2495bSdanielk1977} SQLITE_SCHEMA 502dae2495bSdanielk1977 503b92b70bbSdanielk1977# Make sure the schema-version can be manipulated in an attached database. 504dae2495bSdanielk1977file delete -force test2.db 505dae2495bSdanielk1977file delete -force test2.db-journal 506dae2495bSdanielk1977do_test pragma-8.1.11 { 507dae2495bSdanielk1977 execsql { 508dae2495bSdanielk1977 ATTACH 'test2.db' AS aux; 509dae2495bSdanielk1977 CREATE TABLE aux.t1(a, b, c); 510b92b70bbSdanielk1977 PRAGMA aux.schema_version = 205; 511dae2495bSdanielk1977 } 512dae2495bSdanielk1977} {} 513dae2495bSdanielk1977do_test pragma-8.1.12 { 514dae2495bSdanielk1977 execsql { 515b92b70bbSdanielk1977 PRAGMA aux.schema_version; 516dae2495bSdanielk1977 } 517dae2495bSdanielk1977} 205 518dae2495bSdanielk1977do_test pragma-8.1.13 { 519dae2495bSdanielk1977 execsql { 520b92b70bbSdanielk1977 PRAGMA schema_version; 521dae2495bSdanielk1977 } 522dae2495bSdanielk1977} 108 523dae2495bSdanielk1977 524b92b70bbSdanielk1977# And check that modifying the schema-version in an attached database 525dae2495bSdanielk1977# forces the second connection to reload the schema. 526dae2495bSdanielk1977do_test pragma-8.1.14 { 527dddca286Sdrh sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2] 528dae2495bSdanielk1977 execsql { 529dae2495bSdanielk1977 ATTACH 'test2.db' AS aux; 530dae2495bSdanielk1977 SELECT * FROM aux.t1; 531dae2495bSdanielk1977 } db2 532dae2495bSdanielk1977} {} 533dae2495bSdanielk1977do_test pragma-8.1.15 { 534dae2495bSdanielk1977 execsql { 535b92b70bbSdanielk1977 PRAGMA aux.schema_version = 206; 536dae2495bSdanielk1977 } 537dae2495bSdanielk1977} {} 538dae2495bSdanielk1977do_test pragma-8.1.16 { 539dae2495bSdanielk1977 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY] 540dae2495bSdanielk1977 sqlite3_step $::STMT 541dae2495bSdanielk1977} SQLITE_ERROR 542dae2495bSdanielk1977do_test pragma-8.1.17 { 543dae2495bSdanielk1977 sqlite3_finalize $::STMT 544dae2495bSdanielk1977} SQLITE_SCHEMA 545dae2495bSdanielk1977do_test pragma-8.1.18 { 546dae2495bSdanielk1977 db2 close 547dae2495bSdanielk1977} {} 548dae2495bSdanielk1977 549b92b70bbSdanielk1977# Now test that the user-version can be read and written (and that we aren't 550b92b70bbSdanielk1977# accidentally manipulating the schema-version instead). 551dae2495bSdanielk1977do_test pragma-8.2.1 { 55225403650Sdrh execsql2 { 553b92b70bbSdanielk1977 PRAGMA user_version; 554dae2495bSdanielk1977 } 55525403650Sdrh} {user_version 0} 556dae2495bSdanielk1977do_test pragma-8.2.2 { 557dae2495bSdanielk1977 execsql { 558b92b70bbSdanielk1977 PRAGMA user_version = 2; 559dae2495bSdanielk1977 } 560dae2495bSdanielk1977} {} 561802d69a7Sdrhdo_test pragma-8.2.3.1 { 56225403650Sdrh execsql2 { 563b92b70bbSdanielk1977 PRAGMA user_version; 564dae2495bSdanielk1977 } 56525403650Sdrh} {user_version 2} 566802d69a7Sdrhdo_test pragma-8.2.3.2 { 567802d69a7Sdrh db close 568802d69a7Sdrh sqlite3 db test.db 569802d69a7Sdrh execsql { 570802d69a7Sdrh PRAGMA user_version; 571802d69a7Sdrh } 572802d69a7Sdrh} {2} 573802d69a7Sdrhdo_test pragma-8.2.4.1 { 574dae2495bSdanielk1977 execsql { 575b92b70bbSdanielk1977 PRAGMA schema_version; 576dae2495bSdanielk1977 } 577dae2495bSdanielk1977} {108} 578802d69a7Sdrhifcapable vacuum { 579802d69a7Sdrh do_test pragma-8.2.4.2 { 580802d69a7Sdrh execsql { 581802d69a7Sdrh VACUUM; 582802d69a7Sdrh PRAGMA user_version; 583802d69a7Sdrh } 584802d69a7Sdrh } {2} 585802d69a7Sdrh do_test pragma-8.2.4.3 { 586802d69a7Sdrh execsql { 587802d69a7Sdrh PRAGMA schema_version; 588802d69a7Sdrh } 589802d69a7Sdrh } {109} 590802d69a7Sdrh} 591802d69a7Sdrhdb eval {ATTACH 'test2.db' AS aux} 592dae2495bSdanielk1977 593b92b70bbSdanielk1977# Check that the user-version in the auxilary database can be manipulated ( 594dae2495bSdanielk1977# and that we aren't accidentally manipulating the same in the main db). 595dae2495bSdanielk1977do_test pragma-8.2.5 { 596dae2495bSdanielk1977 execsql { 597b92b70bbSdanielk1977 PRAGMA aux.user_version; 598dae2495bSdanielk1977 } 599dae2495bSdanielk1977} {0} 600dae2495bSdanielk1977do_test pragma-8.2.6 { 601dae2495bSdanielk1977 execsql { 602b92b70bbSdanielk1977 PRAGMA aux.user_version = 3; 603dae2495bSdanielk1977 } 604dae2495bSdanielk1977} {} 605dae2495bSdanielk1977do_test pragma-8.2.7 { 606dae2495bSdanielk1977 execsql { 607b92b70bbSdanielk1977 PRAGMA aux.user_version; 608dae2495bSdanielk1977 } 609dae2495bSdanielk1977} {3} 610dae2495bSdanielk1977do_test pragma-8.2.8 { 611dae2495bSdanielk1977 execsql { 612b92b70bbSdanielk1977 PRAGMA main.user_version; 613dae2495bSdanielk1977 } 614dae2495bSdanielk1977} {2} 615dae2495bSdanielk1977 616b92b70bbSdanielk1977# Now check that a ROLLBACK resets the user-version if it has been modified 617dae2495bSdanielk1977# within a transaction. 618dae2495bSdanielk1977do_test pragma-8.2.9 { 619dae2495bSdanielk1977 execsql { 620dae2495bSdanielk1977 BEGIN; 621b92b70bbSdanielk1977 PRAGMA aux.user_version = 10; 622b92b70bbSdanielk1977 PRAGMA user_version = 11; 623dae2495bSdanielk1977 } 624dae2495bSdanielk1977} {} 625dae2495bSdanielk1977do_test pragma-8.2.10 { 626dae2495bSdanielk1977 execsql { 627b92b70bbSdanielk1977 PRAGMA aux.user_version; 628dae2495bSdanielk1977 } 629dae2495bSdanielk1977} {10} 630dae2495bSdanielk1977do_test pragma-8.2.11 { 631dae2495bSdanielk1977 execsql { 632b92b70bbSdanielk1977 PRAGMA main.user_version; 633dae2495bSdanielk1977 } 634dae2495bSdanielk1977} {11} 635dae2495bSdanielk1977do_test pragma-8.2.12 { 636dae2495bSdanielk1977 execsql { 637dae2495bSdanielk1977 ROLLBACK; 638b92b70bbSdanielk1977 PRAGMA aux.user_version; 639dae2495bSdanielk1977 } 640dae2495bSdanielk1977} {3} 641dae2495bSdanielk1977do_test pragma-8.2.13 { 642dae2495bSdanielk1977 execsql { 643b92b70bbSdanielk1977 PRAGMA main.user_version; 644dae2495bSdanielk1977 } 645dae2495bSdanielk1977} {2} 646dae2495bSdanielk1977 647b92b70bbSdanielk1977# Try a negative value for the user-version 648dae2495bSdanielk1977do_test pragma-8.2.14 { 649dae2495bSdanielk1977 execsql { 650b92b70bbSdanielk1977 PRAGMA user_version = -450; 651dae2495bSdanielk1977 } 652dae2495bSdanielk1977} {} 653dae2495bSdanielk1977do_test pragma-8.2.15 { 654dae2495bSdanielk1977 execsql { 655b92b70bbSdanielk1977 PRAGMA user_version; 656dae2495bSdanielk1977 } 657dae2495bSdanielk1977} {-450} 658d9c847dcSdanielk1977} ; # ifcapable schema_version 659d9c847dcSdanielk1977 6609a09a3caStpoindex 6619a09a3caStpoindex# Test temp_store and temp_store_directory pragmas 6629a09a3caStpoindex# 663268283bcSdrhifcapable pager_pragmas { 6649a09a3caStpoindexdo_test pragma-9.1 { 6659a09a3caStpoindex db close 6669a09a3caStpoindex sqlite3 db test.db 6679a09a3caStpoindex execsql { 6689a09a3caStpoindex PRAGMA temp_store; 6699a09a3caStpoindex } 6709a09a3caStpoindex} {0} 6719a09a3caStpoindexdo_test pragma-9.2 { 6729a09a3caStpoindex execsql { 6739a09a3caStpoindex PRAGMA temp_store=file; 6749a09a3caStpoindex PRAGMA temp_store; 6759a09a3caStpoindex } 6769a09a3caStpoindex} {1} 6779a09a3caStpoindexdo_test pragma-9.3 { 6789a09a3caStpoindex execsql { 6799a09a3caStpoindex PRAGMA temp_store=memory; 6809a09a3caStpoindex PRAGMA temp_store; 6819a09a3caStpoindex } 6829a09a3caStpoindex} {2} 6839a09a3caStpoindexdo_test pragma-9.4 { 6849a09a3caStpoindex execsql { 6859a09a3caStpoindex PRAGMA temp_store_directory; 6869a09a3caStpoindex } 6879a09a3caStpoindex} {} 6889a09a3caStpoindexdo_test pragma-9.5 { 689268283bcSdrh set pwd [string map {' ''} [pwd]] 690268283bcSdrh execsql " 691268283bcSdrh PRAGMA temp_store_directory='$pwd'; 6929a09a3caStpoindex " 6939a09a3caStpoindex} {} 6949a09a3caStpoindexdo_test pragma-9.6 { 6959a09a3caStpoindex execsql { 6969a09a3caStpoindex PRAGMA temp_store_directory; 6979a09a3caStpoindex } 698*1941a3e8Sdrh} [list [pwd]] 6999a09a3caStpoindexdo_test pragma-9.7 { 700268283bcSdrh catchsql { 7019a09a3caStpoindex PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR'; 7029a09a3caStpoindex } 703268283bcSdrh} {1 {not a writable directory}} 7049a09a3caStpoindexdo_test pragma-9.8 { 7059a09a3caStpoindex execsql { 7069a09a3caStpoindex PRAGMA temp_store_directory=''; 7079a09a3caStpoindex } 7089a09a3caStpoindex} {} 70953c0f748Sdanielk1977ifcapable tempdb { 7109a09a3caStpoindex do_test pragma-9.9 { 7119a09a3caStpoindex execsql { 7129a09a3caStpoindex PRAGMA temp_store_directory; 7139a09a3caStpoindex PRAGMA temp_store=FILE; 7149a09a3caStpoindex CREATE TEMP TABLE temp_store_directory_test(a integer); 7159a09a3caStpoindex INSERT INTO temp_store_directory_test values (2); 7169a09a3caStpoindex SELECT * FROM temp_store_directory_test; 7179a09a3caStpoindex } 7189a09a3caStpoindex } {2} 71953c0f748Sdanielk1977} 7209a09a3caStpoindexdo_test pragma-9.10 { 721268283bcSdrh catchsql " 722268283bcSdrh PRAGMA temp_store_directory='$pwd'; 7239a09a3caStpoindex SELECT * FROM temp_store_directory_test; 7249a09a3caStpoindex " 725268283bcSdrh} {1 {no such table: temp_store_directory_test}} 726268283bcSdrh} ;# ifcapable pager_pragmas 7279a09a3caStpoindex 728cc6bd383Sdanielk1977ifcapable trigger { 729cc6bd383Sdanielk1977 730cc6bd383Sdanielk1977do_test pragma-10.0 { 731cc6bd383Sdanielk1977 catchsql { 732cc6bd383Sdanielk1977 DROP TABLE main.t1; 733cc6bd383Sdanielk1977 } 734cc6bd383Sdanielk1977 execsql { 735cc6bd383Sdanielk1977 PRAGMA count_changes = 1; 736cc6bd383Sdanielk1977 737cc6bd383Sdanielk1977 CREATE TABLE t1(a PRIMARY KEY); 738cc6bd383Sdanielk1977 CREATE TABLE t1_mirror(a); 739cc6bd383Sdanielk1977 CREATE TABLE t1_mirror2(a); 740cc6bd383Sdanielk1977 CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN 741cc6bd383Sdanielk1977 INSERT INTO t1_mirror VALUES(new.a); 742cc6bd383Sdanielk1977 END; 743cc6bd383Sdanielk1977 CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN 744cc6bd383Sdanielk1977 INSERT INTO t1_mirror2 VALUES(new.a); 745cc6bd383Sdanielk1977 END; 746cc6bd383Sdanielk1977 CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN 747cc6bd383Sdanielk1977 UPDATE t1_mirror SET a = new.a WHERE a = old.a; 748cc6bd383Sdanielk1977 END; 749cc6bd383Sdanielk1977 CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN 750cc6bd383Sdanielk1977 UPDATE t1_mirror2 SET a = new.a WHERE a = old.a; 751cc6bd383Sdanielk1977 END; 752cc6bd383Sdanielk1977 CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN 753cc6bd383Sdanielk1977 DELETE FROM t1_mirror WHERE a = old.a; 754cc6bd383Sdanielk1977 END; 755cc6bd383Sdanielk1977 CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN 756cc6bd383Sdanielk1977 DELETE FROM t1_mirror2 WHERE a = old.a; 757cc6bd383Sdanielk1977 END; 758cc6bd383Sdanielk1977 } 759cc6bd383Sdanielk1977} {} 760cc6bd383Sdanielk1977 761cc6bd383Sdanielk1977do_test pragma-10.1 { 762cc6bd383Sdanielk1977 execsql { 763cc6bd383Sdanielk1977 INSERT INTO t1 VALUES(randstr(10,10)); 764cc6bd383Sdanielk1977 } 765cc6bd383Sdanielk1977} {1} 766cc6bd383Sdanielk1977do_test pragma-10.2 { 767cc6bd383Sdanielk1977 execsql { 768cc6bd383Sdanielk1977 UPDATE t1 SET a = randstr(10,10); 769cc6bd383Sdanielk1977 } 770cc6bd383Sdanielk1977} {1} 771cc6bd383Sdanielk1977do_test pragma-10.3 { 772cc6bd383Sdanielk1977 execsql { 773cc6bd383Sdanielk1977 DELETE FROM t1; 774cc6bd383Sdanielk1977 } 775cc6bd383Sdanielk1977} {1} 776cc6bd383Sdanielk1977 777cc6bd383Sdanielk1977} ;# ifcapable trigger 778cc6bd383Sdanielk1977 77948af65aeSdanielk1977ifcapable schema_pragmas { 78048af65aeSdanielk1977 do_test pragma-11.1 { 78148af65aeSdanielk1977 execsql2 { 78248af65aeSdanielk1977 pragma collation_list; 78348af65aeSdanielk1977 } 78448af65aeSdanielk1977 } {seq 0 name NOCASE seq 1 name BINARY} 78548af65aeSdanielk1977 do_test pragma-11.2 { 78648af65aeSdanielk1977 db collate New_Collation blah... 78748af65aeSdanielk1977 execsql { 78848af65aeSdanielk1977 pragma collation_list; 78948af65aeSdanielk1977 } 79048af65aeSdanielk1977 } {0 New_Collation 1 NOCASE 2 BINARY} 79148af65aeSdanielk1977} 79248af65aeSdanielk1977 793ddfb2f03Sdanielk1977ifcapable schema_pragmas&&tempdb { 794ddfb2f03Sdanielk1977 do_test pragma-12.1 { 795ddfb2f03Sdanielk1977 sqlite3 db2 test.db 796ddfb2f03Sdanielk1977 execsql { 797ddfb2f03Sdanielk1977 PRAGMA temp.table_info('abc'); 798ddfb2f03Sdanielk1977 } db2 799ddfb2f03Sdanielk1977 } {} 800ddfb2f03Sdanielk1977 db2 close 801ddfb2f03Sdanielk1977 802ddfb2f03Sdanielk1977 do_test pragma-12.2 { 803ddfb2f03Sdanielk1977 sqlite3 db2 test.db 804ddfb2f03Sdanielk1977 execsql { 805ddfb2f03Sdanielk1977 PRAGMA temp.default_cache_size = 200; 806ddfb2f03Sdanielk1977 PRAGMA temp.default_cache_size; 807ddfb2f03Sdanielk1977 } db2 808ddfb2f03Sdanielk1977 } {200} 809ddfb2f03Sdanielk1977 db2 close 810ddfb2f03Sdanielk1977 811ddfb2f03Sdanielk1977 do_test pragma-12.3 { 812ddfb2f03Sdanielk1977 sqlite3 db2 test.db 813ddfb2f03Sdanielk1977 execsql { 814ddfb2f03Sdanielk1977 PRAGMA temp.cache_size = 400; 815ddfb2f03Sdanielk1977 PRAGMA temp.cache_size; 816ddfb2f03Sdanielk1977 } db2 817ddfb2f03Sdanielk1977 } {400} 818ddfb2f03Sdanielk1977 db2 close 819ddfb2f03Sdanielk1977} 820ddfb2f03Sdanielk1977 8214b2688abSdanielk1977ifcapable bloblit { 8224b2688abSdanielk1977 82305a82983Sdrhdo_test pragma-13.1 { 82405a82983Sdrh execsql { 82505a82983Sdrh DROP TABLE IF EXISTS t4; 82605a82983Sdrh PRAGMA vdbe_trace=on; 82705a82983Sdrh PRAGMA vdbe_listing=on; 82805a82983Sdrh PRAGMA sql_trace=on; 82905a82983Sdrh CREATE TABLE t4(a INTEGER PRIMARY KEY,b); 83005a82983Sdrh INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789'); 83105a82983Sdrh INSERT INTO t4(b) VALUES(randstr(30,30)); 83205a82983Sdrh INSERT INTO t4(b) VALUES(1.23456); 83305a82983Sdrh INSERT INTO t4(b) VALUES(NULL); 83405a82983Sdrh INSERT INTO t4(b) VALUES(0); 83505a82983Sdrh INSERT INTO t4(b) SELECT b||b||b||b FROM t4; 83605a82983Sdrh SELECT * FROM t4; 83705a82983Sdrh } 83805a82983Sdrh execsql { 83905a82983Sdrh PRAGMA vdbe_trace=off; 84005a82983Sdrh PRAGMA vdbe_listing=off; 84105a82983Sdrh PRAGMA sql_trace=off; 84205a82983Sdrh } 84305a82983Sdrh} {} 84405a82983Sdrh 8454b2688abSdanielk1977} ;# ifcapable bloblit 8464b2688abSdanielk1977 8475558a8a6Sdanielk1977# Reset the sqlite3_temp_directory variable for the next run of tests: 8485558a8a6Sdanielk1977sqlite3 dbX :memory: 8495558a8a6Sdanielk1977dbX eval {PRAGMA temp_store_directory = ""} 8505558a8a6Sdanielk1977dbX close 8515558a8a6Sdanielk1977 852cd61c281Sdrhfinish_test 853