xref: /sqlite-3.40.0/test/pragma.test (revision 7efa426e)
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#
15838cce43Sdanielk1977# $Id: pragma.test,v 1.73 2009/01/12 14:01:45 danielk1977 Exp $
16cd61c281Sdrh
17cd61c281Sdrhset testdir [file dirname $argv0]
18cd61c281Sdrhsource $testdir/tester.tcl
195885e762Sdanset testprefix pragma
20cd61c281Sdrh
2168928b6cSdan# Do not use a codec for tests in this file, as the database file is
2268928b6cSdan# manipulated directly using tcl scripts (using the [hexio_write] command).
2368928b6cSdan#
2468928b6cSdando_not_use_codec
2568928b6cSdan
2691cf71b0Sdanielk1977# Test organization:
2791cf71b0Sdanielk1977#
2891cf71b0Sdanielk1977# pragma-1.*: Test cache_size, default_cache_size and synchronous on main db.
2991cf71b0Sdanielk1977# pragma-2.*: Test synchronous on attached db.
3091cf71b0Sdanielk1977# pragma-3.*: Test detection of table/index inconsistency by integrity_check.
3191cf71b0Sdanielk1977# pragma-4.*: Test cache_size and default_cache_size on attached db.
3291cf71b0Sdanielk1977# pragma-5.*: Test that pragma synchronous may not be used inside of a
3391cf71b0Sdanielk1977#             transaction.
34cc6bd383Sdanielk1977# pragma-6.*: Test schema-query pragmas.
35cc6bd383Sdanielk1977# pragma-7.*: Miscellaneous tests.
36cc6bd383Sdanielk1977# pragma-8.*: Test user_version and schema_version pragmas.
379a09a3caStpoindex# pragma-9.*: Test temp_store and temp_store_directory.
38cc6bd383Sdanielk1977# pragma-10.*: Test the count_changes pragma in the presence of triggers.
3948af65aeSdanielk1977# pragma-11.*: Test the collation_list pragma.
4059a93791Sdanielk1977# pragma-14.*: Test the page_count pragma.
418cf6c554Sdanielk1977# pragma-15.*: Test that the value set using the cache_size pragma is not
428cf6c554Sdanielk1977#              reset when the schema is reloaded.
43aebf413dSaswift# pragma-16.*: Test proxy locking
44a112d140Smistachkin# pragma-20.*: Test data_store_directory.
455885e762Sdan# pragma-22.*: Test that "PRAGMA [db].integrity_check" respects the "db"
465885e762Sdan#              directive - if it is present.
4791cf71b0Sdanielk1977#
4891cf71b0Sdanielk1977
4905a82983Sdrhifcapable !pragma {
5005a82983Sdrh  finish_test
5105a82983Sdrh  return
5205a82983Sdrh}
5305a82983Sdrh
54cd61c281Sdrh# Delete the preexisting database to avoid the special setup
55cd61c281Sdrh# that the "all.test" script does.
56cd61c281Sdrh#
57cd61c281Sdrhdb close
58fda06befSmistachkindelete_file test.db test.db-journal
59fda06befSmistachkindelete_file test3.db test3.db-journal
60dddca286Sdrhsqlite3 db test.db; set DB [sqlite3_connection_pointer db]
61cd61c281Sdrh
621e9daa6aSdrh
63c7b4a44cSdanielk1977ifcapable pager_pragmas {
641e9daa6aSdrhset DFLT_CACHE_SZ [db one {PRAGMA default_cache_size}]
651e9daa6aSdrhset TEMP_CACHE_SZ [db one {PRAGMA temp.default_cache_size}]
66cd61c281Sdrhdo_test pragma-1.1 {
67cd61c281Sdrh  execsql {
68cd61c281Sdrh    PRAGMA cache_size;
69cd61c281Sdrh    PRAGMA default_cache_size;
70cd61c281Sdrh    PRAGMA synchronous;
71cd61c281Sdrh  }
721e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
73cd61c281Sdrhdo_test pragma-1.2 {
74cd61c281Sdrh  execsql {
75eb43e5c7Sdrh    PRAGMA synchronous=OFF;
76cd61c281Sdrh    PRAGMA cache_size=1234;
77cd61c281Sdrh    PRAGMA cache_size;
78cd61c281Sdrh    PRAGMA default_cache_size;
79cd61c281Sdrh    PRAGMA synchronous;
80cd61c281Sdrh  }
811e9daa6aSdrh} [list 1234 $DFLT_CACHE_SZ 0]
82cd61c281Sdrhdo_test pragma-1.3 {
83cd61c281Sdrh  db close
84ef4ac8f9Sdrh  sqlite3 db test.db
85cd61c281Sdrh  execsql {
86cd61c281Sdrh    PRAGMA cache_size;
87cd61c281Sdrh    PRAGMA default_cache_size;
88cd61c281Sdrh    PRAGMA synchronous;
89cd61c281Sdrh  }
901e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
91cd61c281Sdrhdo_test pragma-1.4 {
92cd61c281Sdrh  execsql {
93cd61c281Sdrh    PRAGMA synchronous=OFF;
94cd61c281Sdrh    PRAGMA cache_size;
95cd61c281Sdrh    PRAGMA default_cache_size;
96cd61c281Sdrh    PRAGMA synchronous;
97cd61c281Sdrh  }
981e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 0]
99cd61c281Sdrhdo_test pragma-1.5 {
100cd61c281Sdrh  execsql {
101d2cb50b7Sdrh    PRAGMA cache_size=-4321;
102cd61c281Sdrh    PRAGMA cache_size;
103cd61c281Sdrh    PRAGMA default_cache_size;
104cd61c281Sdrh    PRAGMA synchronous;
105cd61c281Sdrh  }
1063b42abb3Sdrh} [list -4321 $DFLT_CACHE_SZ 0]
107cd61c281Sdrhdo_test pragma-1.6 {
108cd61c281Sdrh  execsql {
109cd61c281Sdrh    PRAGMA synchronous=ON;
110cd61c281Sdrh    PRAGMA cache_size;
111cd61c281Sdrh    PRAGMA default_cache_size;
112cd61c281Sdrh    PRAGMA synchronous;
113cd61c281Sdrh  }
1143b42abb3Sdrh} [list -4321 $DFLT_CACHE_SZ 1]
115cd61c281Sdrhdo_test pragma-1.7 {
116cd61c281Sdrh  db close
117ef4ac8f9Sdrh  sqlite3 db test.db
118cd61c281Sdrh  execsql {
119cd61c281Sdrh    PRAGMA cache_size;
120cd61c281Sdrh    PRAGMA default_cache_size;
121cd61c281Sdrh    PRAGMA synchronous;
122cd61c281Sdrh  }
1231e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
124cd61c281Sdrhdo_test pragma-1.8 {
125cd61c281Sdrh  execsql {
126d2cb50b7Sdrh    PRAGMA default_cache_size=-123;
127cd61c281Sdrh    PRAGMA cache_size;
128cd61c281Sdrh    PRAGMA default_cache_size;
129cd61c281Sdrh    PRAGMA synchronous;
130cd61c281Sdrh  }
13191cf71b0Sdanielk1977} {123 123 2}
132802d69a7Sdrhdo_test pragma-1.9.1 {
133cd61c281Sdrh  db close
134dddca286Sdrh  sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
135cd61c281Sdrh  execsql {
136cd61c281Sdrh    PRAGMA cache_size;
137cd61c281Sdrh    PRAGMA default_cache_size;
138cd61c281Sdrh    PRAGMA synchronous;
139cd61c281Sdrh  }
14091cf71b0Sdanielk1977} {123 123 2}
141802d69a7Sdrhifcapable vacuum {
142802d69a7Sdrh  do_test pragma-1.9.2 {
143802d69a7Sdrh    execsql {
144802d69a7Sdrh      VACUUM;
145802d69a7Sdrh      PRAGMA cache_size;
146802d69a7Sdrh      PRAGMA default_cache_size;
147802d69a7Sdrh      PRAGMA synchronous;
148802d69a7Sdrh    }
149802d69a7Sdrh  } {123 123 2}
150802d69a7Sdrh}
15191cf71b0Sdanielk1977do_test pragma-1.10 {
1525a38705eSdrh  execsql {
1534303feedSdrh    PRAGMA synchronous=NORMAL;
1544303feedSdrh    PRAGMA cache_size;
1554303feedSdrh    PRAGMA default_cache_size;
1564303feedSdrh    PRAGMA synchronous;
1574303feedSdrh  }
15891cf71b0Sdanielk1977} {123 123 1}
15991cf71b0Sdanielk1977do_test pragma-1.11 {
1604303feedSdrh  execsql {
1614303feedSdrh    PRAGMA synchronous=FULL;
1624303feedSdrh    PRAGMA cache_size;
1634303feedSdrh    PRAGMA default_cache_size;
1644303feedSdrh    PRAGMA synchronous;
1654303feedSdrh  }
16691cf71b0Sdanielk1977} {123 123 2}
16791cf71b0Sdanielk1977do_test pragma-1.12 {
1684303feedSdrh  db close
169dddca286Sdrh  sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
1704303feedSdrh  execsql {
1714303feedSdrh    PRAGMA cache_size;
1724303feedSdrh    PRAGMA default_cache_size;
1734303feedSdrh    PRAGMA synchronous;
1744303feedSdrh  }
17591cf71b0Sdanielk1977} {123 123 2}
17691cf71b0Sdanielk1977
1775260f7e9Sdrh# Make sure the pragma handler understands numeric values in addition
1785260f7e9Sdrh# to keywords like "off" and "full".
1795260f7e9Sdrh#
1805260f7e9Sdrhdo_test pragma-1.13 {
1815260f7e9Sdrh  execsql {
1825260f7e9Sdrh    PRAGMA synchronous=0;
1835260f7e9Sdrh    PRAGMA synchronous;
1845260f7e9Sdrh  }
1855260f7e9Sdrh} {0}
1865260f7e9Sdrhdo_test pragma-1.14 {
1875260f7e9Sdrh  execsql {
1885260f7e9Sdrh    PRAGMA synchronous=2;
1895260f7e9Sdrh    PRAGMA synchronous;
1905260f7e9Sdrh  }
1915260f7e9Sdrh} {2}
192c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
1935260f7e9Sdrh
1945260f7e9Sdrh# Test turning "flag" pragmas on and off.
1955260f7e9Sdrh#
1966338c762Sdanielk1977ifcapable debug {
1976338c762Sdanielk1977  # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
1986338c762Sdanielk1977  #
1995260f7e9Sdrh  do_test pragma-1.15 {
2005260f7e9Sdrh    execsql {
2015260f7e9Sdrh      PRAGMA vdbe_listing=YES;
2025260f7e9Sdrh      PRAGMA vdbe_listing;
2035260f7e9Sdrh    }
2045260f7e9Sdrh  } {1}
2055260f7e9Sdrh  do_test pragma-1.16 {
2065260f7e9Sdrh    execsql {
2075260f7e9Sdrh      PRAGMA vdbe_listing=NO;
2085260f7e9Sdrh      PRAGMA vdbe_listing;
2095260f7e9Sdrh    }
2105260f7e9Sdrh  } {0}
2116338c762Sdanielk1977}
2126338c762Sdanielk1977
2135260f7e9Sdrhdo_test pragma-1.17 {
2145260f7e9Sdrh  execsql {
2155260f7e9Sdrh    PRAGMA parser_trace=ON;
2165260f7e9Sdrh    PRAGMA parser_trace=OFF;
2175260f7e9Sdrh  }
2185260f7e9Sdrh} {}
2195260f7e9Sdrhdo_test pragma-1.18 {
2205260f7e9Sdrh  execsql {
2215260f7e9Sdrh    PRAGMA bogus = -1234;  -- Parsing of negative values
2225260f7e9Sdrh  }
2235260f7e9Sdrh} {}
2245260f7e9Sdrh
22591cf71b0Sdanielk1977# Test modifying the safety_level of an attached database.
2265a8f9374Sdanielk1977ifcapable pager_pragmas&&attach {
22791cf71b0Sdanielk1977  do_test pragma-2.1 {
228fda06befSmistachkin    forcedelete test2.db
229fda06befSmistachkin    forcedelete test2.db-journal
2304303feedSdrh    execsql {
23191cf71b0Sdanielk1977      ATTACH 'test2.db' AS aux;
2324303feedSdrh    }
23391cf71b0Sdanielk1977  } {}
23491cf71b0Sdanielk1977  do_test pragma-2.2 {
2354303feedSdrh    execsql {
23691cf71b0Sdanielk1977      pragma aux.synchronous;
2374303feedSdrh    }
23891cf71b0Sdanielk1977  } {2}
23991cf71b0Sdanielk1977  do_test pragma-2.3 {
24091cf71b0Sdanielk1977    execsql {
24191cf71b0Sdanielk1977      pragma aux.synchronous = OFF;
24291cf71b0Sdanielk1977      pragma aux.synchronous;
24391cf71b0Sdanielk1977      pragma synchronous;
24491cf71b0Sdanielk1977    }
24591cf71b0Sdanielk1977  } {0 2}
24691cf71b0Sdanielk1977  do_test pragma-2.4 {
24791cf71b0Sdanielk1977    execsql {
24891cf71b0Sdanielk1977      pragma aux.synchronous = ON;
24991cf71b0Sdanielk1977      pragma synchronous;
25091cf71b0Sdanielk1977      pragma aux.synchronous;
25191cf71b0Sdanielk1977    }
25291cf71b0Sdanielk1977  } {2 1}
253c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
2544303feedSdrh
255ed717fe3Sdrh# Construct a corrupted index and make sure the integrity_check
256ed717fe3Sdrh# pragma finds it.
257ed717fe3Sdrh#
25825d6543dSdrh# These tests won't work if the database is encrypted
25925d6543dSdrh#
260ed717fe3Sdrhdo_test pragma-3.1 {
2611e9daa6aSdrh  db close
262fda06befSmistachkin  forcedelete test.db test.db-journal
2631e9daa6aSdrh  sqlite3 db test.db
264ed717fe3Sdrh  execsql {
2651e9daa6aSdrh    PRAGMA auto_vacuum=OFF;
266ed717fe3Sdrh    BEGIN;
267ed717fe3Sdrh    CREATE TABLE t2(a,b,c);
268ed717fe3Sdrh    CREATE INDEX i2 ON t2(a);
269ed717fe3Sdrh    INSERT INTO t2 VALUES(11,2,3);
270ed717fe3Sdrh    INSERT INTO t2 VALUES(22,3,4);
271ed717fe3Sdrh    COMMIT;
272ed717fe3Sdrh    SELECT rowid, * from t2;
273ed717fe3Sdrh  }
274ed717fe3Sdrh} {1 11 2 3 2 22 3 4}
2755a8f9374Sdanielk1977ifcapable attach {
27640e016e4Sdrh  if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
277ed717fe3Sdrh    do_test pragma-3.2 {
278bb8a279eSdrh      db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
279bb8a279eSdrh      set pgsz [db eval {PRAGMA page_size}]
280bb8a279eSdrh      # overwrite the header on the rootpage of the index in order to
281bb8a279eSdrh      # make the index appear to be empty.
282bb8a279eSdrh      #
283bb8a279eSdrh      set offset [expr {$pgsz*($rootpage-1)}]
284bb8a279eSdrh      hexio_write test.db $offset 0a00000000040000000000
285bb8a279eSdrh      db close
286bb8a279eSdrh      sqlite3 db test.db
287ed717fe3Sdrh      execsql {PRAGMA integrity_check}
2886fbe41acSdrh    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
28991cf71b0Sdanielk1977    do_test pragma-3.3 {
2901dcdbc06Sdrh      execsql {PRAGMA integrity_check=1}
2916fbe41acSdrh    } {{row 1 missing from index i2}}
2921dcdbc06Sdrh    do_test pragma-3.4 {
29391cf71b0Sdanielk1977      execsql {
2941dcdbc06Sdrh        ATTACH DATABASE 'test.db' AS t2;
2951dcdbc06Sdrh        PRAGMA integrity_check
29691cf71b0Sdanielk1977      }
2976fbe41acSdrh    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
2981dcdbc06Sdrh    do_test pragma-3.5 {
2991dcdbc06Sdrh      execsql {
300bb8a279eSdrh        PRAGMA integrity_check=4
3011dcdbc06Sdrh      }
3026fbe41acSdrh    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2}}
3031dcdbc06Sdrh    do_test pragma-3.6 {
3041dcdbc06Sdrh      execsql {
3051dcdbc06Sdrh        PRAGMA integrity_check=xyz
3061dcdbc06Sdrh      }
3076fbe41acSdrh    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
3081dcdbc06Sdrh    do_test pragma-3.7 {
3091dcdbc06Sdrh      execsql {
3101dcdbc06Sdrh        PRAGMA integrity_check=0
3111dcdbc06Sdrh      }
3126fbe41acSdrh    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
3131dcdbc06Sdrh
3141dcdbc06Sdrh    # Add additional corruption by appending unused pages to the end of
3151dcdbc06Sdrh    # the database file testerr.db
3161dcdbc06Sdrh    #
3171dcdbc06Sdrh    do_test pragma-3.8 {
3181dcdbc06Sdrh      execsql {DETACH t2}
319fda06befSmistachkin      forcedelete testerr.db testerr.db-journal
320e3d559adSdrh      set out [open testerr.db w]
321e3d559adSdrh      fconfigure $out -translation binary
322e3d559adSdrh      set in [open test.db r]
323e3d559adSdrh      fconfigure $in -translation binary
3241dcdbc06Sdrh      puts -nonewline $out [read $in]
3251dcdbc06Sdrh      seek $in 0
3261dcdbc06Sdrh      puts -nonewline $out [read $in]
3271dcdbc06Sdrh      close $in
3281dcdbc06Sdrh      close $out
329dd3cd977Sdrh      hexio_write testerr.db 28 00000000
3301dcdbc06Sdrh      execsql {REINDEX t2}
3311dcdbc06Sdrh      execsql {PRAGMA integrity_check}
3321dcdbc06Sdrh    } {ok}
33341c58b78Sdanielk1977    do_test pragma-3.8.1 {
33441c58b78Sdanielk1977      execsql {PRAGMA quick_check}
33541c58b78Sdanielk1977    } {ok}
3365d16a9a6Sdrh    do_test pragma-3.8.2 {
3375d16a9a6Sdrh      execsql {PRAGMA QUICK_CHECK}
3385d16a9a6Sdrh    } {ok}
3391dcdbc06Sdrh    do_test pragma-3.9 {
3401dcdbc06Sdrh      execsql {
3411dcdbc06Sdrh        ATTACH 'testerr.db' AS t2;
3421dcdbc06Sdrh        PRAGMA integrity_check
3431dcdbc06Sdrh      }
3441dcdbc06Sdrh    } {{*** in database t2 ***
3451dcdbc06SdrhPage 4 is never used
3461dcdbc06SdrhPage 5 is never used
3476fbe41acSdrhPage 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
3481dcdbc06Sdrh    do_test pragma-3.10 {
3491dcdbc06Sdrh      execsql {
3501dcdbc06Sdrh        PRAGMA integrity_check=1
3511dcdbc06Sdrh      }
3521dcdbc06Sdrh    } {{*** in database t2 ***
3531dcdbc06SdrhPage 4 is never used}}
3541dcdbc06Sdrh    do_test pragma-3.11 {
3551dcdbc06Sdrh      execsql {
3561dcdbc06Sdrh        PRAGMA integrity_check=5
3571dcdbc06Sdrh      }
3581dcdbc06Sdrh    } {{*** in database t2 ***
3591dcdbc06SdrhPage 4 is never used
3601dcdbc06SdrhPage 5 is never used
3616fbe41acSdrhPage 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2}}
3621dcdbc06Sdrh    do_test pragma-3.12 {
3631dcdbc06Sdrh      execsql {
3641dcdbc06Sdrh        PRAGMA integrity_check=4
3651dcdbc06Sdrh      }
3661dcdbc06Sdrh    } {{*** in database t2 ***
3671dcdbc06SdrhPage 4 is never used
3681dcdbc06SdrhPage 5 is never used
3696fbe41acSdrhPage 6 is never used} {row 1 missing from index i2}}
3701dcdbc06Sdrh    do_test pragma-3.13 {
3711dcdbc06Sdrh      execsql {
3721dcdbc06Sdrh        PRAGMA integrity_check=3
3731dcdbc06Sdrh      }
3741dcdbc06Sdrh    } {{*** in database t2 ***
3751dcdbc06SdrhPage 4 is never used
3761dcdbc06SdrhPage 5 is never used
3771dcdbc06SdrhPage 6 is never used}}
3781dcdbc06Sdrh    do_test pragma-3.14 {
3791dcdbc06Sdrh      execsql {
3801dcdbc06Sdrh        PRAGMA integrity_check(2)
3811dcdbc06Sdrh      }
3821dcdbc06Sdrh    } {{*** in database t2 ***
3831dcdbc06SdrhPage 4 is never used
3841dcdbc06SdrhPage 5 is never used}}
3851dcdbc06Sdrh    do_test pragma-3.15 {
3861dcdbc06Sdrh      execsql {
3871dcdbc06Sdrh        ATTACH 'testerr.db' AS t3;
3881dcdbc06Sdrh        PRAGMA integrity_check
3891dcdbc06Sdrh      }
3901dcdbc06Sdrh    } {{*** in database t2 ***
3911dcdbc06SdrhPage 4 is never used
3921dcdbc06SdrhPage 5 is never used
3936fbe41acSdrhPage 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
3941dcdbc06SdrhPage 4 is never used
3951dcdbc06SdrhPage 5 is never used
3966fbe41acSdrhPage 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
3971dcdbc06Sdrh    do_test pragma-3.16 {
3981dcdbc06Sdrh      execsql {
399bb8a279eSdrh        PRAGMA integrity_check(10)
4001dcdbc06Sdrh      }
4011dcdbc06Sdrh    } {{*** in database t2 ***
4021dcdbc06SdrhPage 4 is never used
4031dcdbc06SdrhPage 5 is never used
4046fbe41acSdrhPage 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
4051dcdbc06SdrhPage 4 is never used
4061dcdbc06SdrhPage 5 is never used
4076fbe41acSdrhPage 6 is never used} {row 1 missing from index i2}}
4081dcdbc06Sdrh    do_test pragma-3.17 {
4091dcdbc06Sdrh      execsql {
410bb8a279eSdrh        PRAGMA integrity_check=8
4111dcdbc06Sdrh      }
4121dcdbc06Sdrh    } {{*** in database t2 ***
4131dcdbc06SdrhPage 4 is never used
4141dcdbc06SdrhPage 5 is never used
4156fbe41acSdrhPage 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {*** in database t3 ***
4161dcdbc06SdrhPage 4 is never used
4171dcdbc06SdrhPage 5 is never used}}
4181dcdbc06Sdrh    do_test pragma-3.18 {
4191dcdbc06Sdrh      execsql {
4201dcdbc06Sdrh        PRAGMA integrity_check=4
4211dcdbc06Sdrh      }
4221dcdbc06Sdrh    } {{*** in database t2 ***
4231dcdbc06SdrhPage 4 is never used
4241dcdbc06SdrhPage 5 is never used
4256fbe41acSdrhPage 6 is never used} {row 1 missing from index i2}}
4261dcdbc06Sdrh  }
427d2cb50b7Sdrh  do_test pragma-3.19 {
428d2cb50b7Sdrh    catch {db close}
429fda06befSmistachkin    forcedelete test.db test.db-journal
430d2cb50b7Sdrh    sqlite3 db test.db
431d2cb50b7Sdrh    db eval {PRAGMA integrity_check}
432d2cb50b7Sdrh  } {ok}
4335a8f9374Sdanielk1977}
434cefc87fcSdrh
435cefc87fcSdrh# Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
436cefc87fcSdrh# constraint violations.
437cefc87fcSdrh#
438cefc87fcSdrhdo_execsql_test pragma-3.20 {
439cefc87fcSdrh  CREATE TABLE t1(a,b);
440cefc87fcSdrh  CREATE INDEX t1a ON t1(a);
441cefc87fcSdrh  INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
442cefc87fcSdrh  PRAGMA writable_schema=ON;
443cefc87fcSdrh  UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
444cefc87fcSdrh   WHERE name='t1a';
445cefc87fcSdrh  UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
446cefc87fcSdrh   WHERE name='t1';
447cefc87fcSdrh  PRAGMA writable_schema=OFF;
448cefc87fcSdrh  ALTER TABLE t1 RENAME TO t1x;
449cefc87fcSdrh  PRAGMA integrity_check;
450cefc87fcSdrh} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
451cefc87fcSdrhdo_execsql_test pragma-3.21 {
452cefc87fcSdrh  PRAGMA integrity_check(3);
453cefc87fcSdrh} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
454cefc87fcSdrhdo_execsql_test pragma-3.22 {
455cefc87fcSdrh  PRAGMA integrity_check(2);
456cefc87fcSdrh} {{non-unique entry in index t1a} {NULL value in t1x.a}}
457*7efa426eSdrhdo_execsql_test pragma-3.23 {
458cefc87fcSdrh  PRAGMA integrity_check(1);
459cefc87fcSdrh} {{non-unique entry in index t1a}}
46091cf71b0Sdanielk1977
461*7efa426eSdrh# PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
462*7efa426eSdrh# interface) used to leave index cursors in an inconsistent state
463*7efa426eSdrh# which could result in an assertion fault in sqlite3BtreeKey()
464*7efa426eSdrh# called from saveCursorPosition() if content is removed from the
465*7efa426eSdrh# index while the integrity_check is still running.  This test verifies
466*7efa426eSdrh# that problem has been fixed.
467*7efa426eSdrh#
468*7efa426eSdrhdo_test pragma-3.30 {
469*7efa426eSdrh  db close
470*7efa426eSdrh  delete_file test.db
471*7efa426eSdrh  sqlite3 db test.db
472*7efa426eSdrh  db eval {
473*7efa426eSdrh    CREATE TABLE t1(a,b,c);
474*7efa426eSdrh    WITH RECURSIVE
475*7efa426eSdrh      c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
476*7efa426eSdrh    INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
477*7efa426eSdrh    CREATE INDEX t1a ON t1(a);
478*7efa426eSdrh    CREATE INDEX t1bc ON t1(b,c);
479*7efa426eSdrh  }
480*7efa426eSdrh  db eval {PRAGMA integrity_check} {
481*7efa426eSdrh     db eval {DELETE FROM t1}
482*7efa426eSdrh  }
483*7efa426eSdrh} {}
484*7efa426eSdrh
48591cf71b0Sdanielk1977# Test modifying the cache_size of an attached database.
4865a8f9374Sdanielk1977ifcapable pager_pragmas&&attach {
48791cf71b0Sdanielk1977do_test pragma-4.1 {
48891cf71b0Sdanielk1977  execsql {
4891e9daa6aSdrh    ATTACH 'test2.db' AS aux;
49091cf71b0Sdanielk1977    pragma aux.cache_size;
49191cf71b0Sdanielk1977    pragma aux.default_cache_size;
49291cf71b0Sdanielk1977  }
4931e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
4941bdd9b57Sdrhdo_test pragma-4.2 {
4951bdd9b57Sdrh  execsql {
49691cf71b0Sdanielk1977    pragma aux.cache_size = 50;
49791cf71b0Sdanielk1977    pragma aux.cache_size;
49891cf71b0Sdanielk1977    pragma aux.default_cache_size;
4991bdd9b57Sdrh  }
5001e9daa6aSdrh} [list 50 $DFLT_CACHE_SZ]
5011bdd9b57Sdrhdo_test pragma-4.3 {
5021bdd9b57Sdrh  execsql {
50391cf71b0Sdanielk1977    pragma aux.default_cache_size = 456;
50491cf71b0Sdanielk1977    pragma aux.cache_size;
50591cf71b0Sdanielk1977    pragma aux.default_cache_size;
5061bdd9b57Sdrh  }
50791cf71b0Sdanielk1977} {456 456}
5081bdd9b57Sdrhdo_test pragma-4.4 {
5091bdd9b57Sdrh  execsql {
51091cf71b0Sdanielk1977    pragma cache_size;
51191cf71b0Sdanielk1977    pragma default_cache_size;
5121bdd9b57Sdrh  }
5131e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
5141bdd9b57Sdrhdo_test pragma-4.5 {
5151bdd9b57Sdrh  execsql {
51691cf71b0Sdanielk1977    DETACH aux;
51791cf71b0Sdanielk1977    ATTACH 'test3.db' AS aux;
51891cf71b0Sdanielk1977    pragma aux.cache_size;
51991cf71b0Sdanielk1977    pragma aux.default_cache_size;
5201bdd9b57Sdrh  }
5211e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
5221bdd9b57Sdrhdo_test pragma-4.6 {
5231bdd9b57Sdrh  execsql {
52491cf71b0Sdanielk1977    DETACH aux;
52591cf71b0Sdanielk1977    ATTACH 'test2.db' AS aux;
52691cf71b0Sdanielk1977    pragma aux.cache_size;
52791cf71b0Sdanielk1977    pragma aux.default_cache_size;
5281bdd9b57Sdrh  }
52991cf71b0Sdanielk1977} {456 456}
530c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
5311bdd9b57Sdrh
53291cf71b0Sdanielk1977# Test that modifying the sync-level in the middle of a transaction is
53391cf71b0Sdanielk1977# disallowed.
534c7b4a44cSdanielk1977ifcapable pager_pragmas {
53591cf71b0Sdanielk1977do_test pragma-5.0 {
5361bdd9b57Sdrh  execsql {
53791cf71b0Sdanielk1977    pragma synchronous;
5381bdd9b57Sdrh  }
53991cf71b0Sdanielk1977} {2}
54091cf71b0Sdanielk1977do_test pragma-5.1 {
5411bdd9b57Sdrh  catchsql {
5421bdd9b57Sdrh    BEGIN;
54391cf71b0Sdanielk1977    pragma synchronous = OFF;
5441bdd9b57Sdrh  }
54591cf71b0Sdanielk1977} {1 {Safety level may not be changed inside a transaction}}
54691cf71b0Sdanielk1977do_test pragma-5.2 {
54791cf71b0Sdanielk1977  execsql {
54891cf71b0Sdanielk1977    pragma synchronous;
54991cf71b0Sdanielk1977  }
55091cf71b0Sdanielk1977} {2}
5515260f7e9Sdrhcatchsql {COMMIT;}
552c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
5535260f7e9Sdrh
5545260f7e9Sdrh# Test schema-query pragmas
5555260f7e9Sdrh#
55627188fb5Sdanielk1977ifcapable schema_pragmas {
5575a8f9374Sdanielk1977ifcapable tempdb&&attach {
5585260f7e9Sdrh  do_test pragma-6.1 {
559956bc92cSdrh    set res {}
56040e016e4Sdrh    execsql {SELECT * FROM sqlite_temp_master}
5615260f7e9Sdrh    foreach {idx name file} [execsql {pragma database_list}] {
5625260f7e9Sdrh      lappend res $idx $name
5635260f7e9Sdrh    }
5645260f7e9Sdrh    set res
565a71aa001Sdrh  } {0 main 1 temp 2 aux}
56653c0f748Sdanielk1977}
5675260f7e9Sdrhdo_test pragma-6.2 {
5685260f7e9Sdrh  execsql {
569d2cb50b7Sdrh    CREATE TABLE t2(a,b,c);
5705260f7e9Sdrh    pragma table_info(t2)
5715260f7e9Sdrh  }
572bfa8b102Sdrh} {0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0}
573d2cb50b7Sdrhdo_test pragma-6.2.1 {
574d2cb50b7Sdrh  execsql {
575d2cb50b7Sdrh    pragma table_info;
576d2cb50b7Sdrh  }
577d2cb50b7Sdrh} {}
578736c7d4bSdrhdb nullvalue <<NULL>>
579417ec638Sdrhdo_test pragma-6.2.2 {
580417ec638Sdrh  execsql {
581736c7d4bSdrh    CREATE TABLE t5(
582736c7d4bSdrh      a TEXT DEFAULT CURRENT_TIMESTAMP,
583736c7d4bSdrh      b DEFAULT (5+3),
584736c7d4bSdrh      c TEXT,
585736c7d4bSdrh      d INTEGER DEFAULT NULL,
586384b7fe2Sdrh      e TEXT DEFAULT '',
587384b7fe2Sdrh      UNIQUE(b,c,d),
588384b7fe2Sdrh      PRIMARY KEY(e,b,c)
589736c7d4bSdrh    );
590417ec638Sdrh    PRAGMA table_info(t5);
591417ec638Sdrh  }
592384b7fe2Sdrh} {0 a TEXT 0 CURRENT_TIMESTAMP 0 1 b {} 0 5+3 2 2 c TEXT 0 <<NULL>> 3 3 d INTEGER 0 NULL 0 4 e TEXT 0 '' 1}
593736c7d4bSdrhdb nullvalue {}
594384b7fe2Sdrhdo_test pragma-6.2.3 {
595384b7fe2Sdrh  execsql {
596384b7fe2Sdrh    CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
597384b7fe2Sdrh    pragma table_info(t2_3)
598384b7fe2Sdrh  }
599384b7fe2Sdrh} {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
6006bf89570Sdrhifcapable {foreignkey} {
601d2cb50b7Sdrh  do_test pragma-6.3.1 {
6025260f7e9Sdrh    execsql {
6035260f7e9Sdrh      CREATE TABLE t3(a int references t2(b), b UNIQUE);
6045260f7e9Sdrh      pragma foreign_key_list(t3);
6055260f7e9Sdrh    }
6061da40a38Sdan  } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
607d2cb50b7Sdrh  do_test pragma-6.3.2 {
608d2cb50b7Sdrh    execsql {
609d2cb50b7Sdrh      pragma foreign_key_list;
610d2cb50b7Sdrh    }
611d2cb50b7Sdrh  } {}
612d2cb50b7Sdrh  do_test pragma-6.3.3 {
613d2cb50b7Sdrh    execsql {
614d2cb50b7Sdrh      pragma foreign_key_list(t3_bogus);
615d2cb50b7Sdrh    }
616d2cb50b7Sdrh  } {}
617d2cb50b7Sdrh  do_test pragma-6.3.4 {
618d2cb50b7Sdrh    execsql {
619d2cb50b7Sdrh      pragma foreign_key_list(t5);
620d2cb50b7Sdrh    }
621d2cb50b7Sdrh  } {}
6225260f7e9Sdrh  do_test pragma-6.4 {
6235260f7e9Sdrh    execsql {
6245260f7e9Sdrh      pragma index_list(t3);
6255260f7e9Sdrh    }
6263ef26156Sdrh  } {0 sqlite_autoindex_t3_1 1}
6276bf89570Sdrh}
6286bf89570Sdrhifcapable {!foreignkey} {
6296bf89570Sdrh  execsql {CREATE TABLE t3(a,b UNIQUE)}
6306bf89570Sdrh}
631d2cb50b7Sdrhdo_test pragma-6.5.1 {
6325260f7e9Sdrh  execsql {
6335260f7e9Sdrh    CREATE INDEX t3i1 ON t3(a,b);
6345260f7e9Sdrh    pragma index_info(t3i1);
6355260f7e9Sdrh  }
6365260f7e9Sdrh} {0 0 a 1 1 b}
637d2cb50b7Sdrhdo_test pragma-6.5.2 {
638d2cb50b7Sdrh  execsql {
639d2cb50b7Sdrh    pragma index_info(t3i1_bogus);
640d2cb50b7Sdrh  }
641d2cb50b7Sdrh} {}
642260d8a6aSdanielk1977
643260d8a6aSdanielk1977ifcapable tempdb {
644260d8a6aSdanielk1977  # Test for ticket #3320. When a temp table of the same name exists, make
645260d8a6aSdanielk1977  # sure the schema of the main table can still be queried using
646260d8a6aSdanielk1977  # "pragma table_info":
647260d8a6aSdanielk1977  do_test pragma-6.6.1 {
648260d8a6aSdanielk1977    execsql {
649260d8a6aSdanielk1977      CREATE TABLE trial(col_main);
650260d8a6aSdanielk1977      CREATE TEMP TABLE trial(col_temp);
651260d8a6aSdanielk1977    }
652260d8a6aSdanielk1977  } {}
653260d8a6aSdanielk1977  do_test pragma-6.6.2 {
654260d8a6aSdanielk1977    execsql {
655260d8a6aSdanielk1977      PRAGMA table_info(trial);
656260d8a6aSdanielk1977    }
657260d8a6aSdanielk1977  } {0 col_temp {} 0 {} 0}
658260d8a6aSdanielk1977  do_test pragma-6.6.3 {
659260d8a6aSdanielk1977    execsql {
660260d8a6aSdanielk1977      PRAGMA temp.table_info(trial);
661260d8a6aSdanielk1977    }
662260d8a6aSdanielk1977  } {0 col_temp {} 0 {} 0}
663260d8a6aSdanielk1977  do_test pragma-6.6.4 {
664260d8a6aSdanielk1977    execsql {
665260d8a6aSdanielk1977      PRAGMA main.table_info(trial);
666260d8a6aSdanielk1977    }
667260d8a6aSdanielk1977  } {0 col_main {} 0 {} 0}
668260d8a6aSdanielk1977}
669f96a3778Sdanielk1977
670f96a3778Sdanielk1977do_test pragma-6.7 {
671f96a3778Sdanielk1977  execsql {
672f96a3778Sdanielk1977    CREATE TABLE test_table(
673f96a3778Sdanielk1977      one INT NOT NULL DEFAULT -1,
674f96a3778Sdanielk1977      two text,
675f96a3778Sdanielk1977      three VARCHAR(45, 65) DEFAULT 'abcde',
676f96a3778Sdanielk1977      four REAL DEFAULT X'abcdef',
677f96a3778Sdanielk1977      five DEFAULT CURRENT_TIME
678f96a3778Sdanielk1977    );
679f96a3778Sdanielk1977    PRAGMA table_info(test_table);
680f96a3778Sdanielk1977  }
681f96a3778Sdanielk1977} [concat \
682f96a3778Sdanielk1977  {0 one INT 1 -1 0} \
683f96a3778Sdanielk1977  {1 two text 0 {} 0} \
684f96a3778Sdanielk1977  {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
685f96a3778Sdanielk1977  {3 four REAL 0 X'abcdef' 0} \
686f96a3778Sdanielk1977  {4 five {} 0 CURRENT_TIME 0} \
687f96a3778Sdanielk1977]
68827188fb5Sdanielk1977} ;# ifcapable schema_pragmas
6895260f7e9Sdrh# Miscellaneous tests
6905260f7e9Sdrh#
69127188fb5Sdanielk1977ifcapable schema_pragmas {
692d2cb50b7Sdrhdo_test pragma-7.1.1 {
6935260f7e9Sdrh  # Make sure a pragma knows to read the schema if it needs to
6945260f7e9Sdrh  db close
6955260f7e9Sdrh  sqlite3 db test.db
6965260f7e9Sdrh  execsql {
6975260f7e9Sdrh    pragma index_list(t3);
6985260f7e9Sdrh  }
6993ef26156Sdrh} {0 t3i1 0 1 sqlite_autoindex_t3_1 1}
700d2cb50b7Sdrhdo_test pragma-7.1.2 {
701d2cb50b7Sdrh  execsql {
702d2cb50b7Sdrh    pragma index_list(t3_bogus);
703d2cb50b7Sdrh  }
704d2cb50b7Sdrh} {}
70527188fb5Sdanielk1977} ;# ifcapable schema_pragmas
7066c62608fSdrhifcapable {utf16} {
707cb354603Sdan  if {[permutation] == ""} {
7085260f7e9Sdrh    do_test pragma-7.2 {
7095260f7e9Sdrh      db close
7105260f7e9Sdrh      sqlite3 db test.db
7115260f7e9Sdrh      catchsql {
7125260f7e9Sdrh        pragma encoding=bogus;
7135260f7e9Sdrh      }
7145260f7e9Sdrh    } {1 {unsupported encoding: bogus}}
7156c62608fSdrh  }
716cb354603Sdan}
71753c0f748Sdanielk1977ifcapable tempdb {
7185260f7e9Sdrh  do_test pragma-7.3 {
7195260f7e9Sdrh    db close
7205260f7e9Sdrh    sqlite3 db test.db
7215260f7e9Sdrh    execsql {
7225260f7e9Sdrh      pragma lock_status;
7235260f7e9Sdrh    }
7245260f7e9Sdrh  } {main unlocked temp closed}
72553c0f748Sdanielk1977} else {
72653c0f748Sdanielk1977  do_test pragma-7.3 {
72753c0f748Sdanielk1977    db close
72853c0f748Sdanielk1977    sqlite3 db test.db
72953c0f748Sdanielk1977    execsql {
73053c0f748Sdanielk1977      pragma lock_status;
73153c0f748Sdanielk1977    }
73253c0f748Sdanielk1977  } {main unlocked}
73353c0f748Sdanielk1977}
7345260f7e9Sdrh
7355260f7e9Sdrh
736dae2495bSdanielk1977#----------------------------------------------------------------------
737b92b70bbSdanielk1977# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
738b92b70bbSdanielk1977# user_version" statements.
739dae2495bSdanielk1977#
740b92b70bbSdanielk1977# pragma-8.1: PRAGMA schema_version
741b92b70bbSdanielk1977# pragma-8.2: PRAGMA user_version
742dae2495bSdanielk1977#
743dae2495bSdanielk1977
74411cf9fbcSdanielk1977ifcapable schema_version {
74511cf9fbcSdanielk1977
746b92b70bbSdanielk1977# First check that we can set the schema version and then retrieve the
747dae2495bSdanielk1977# same value.
748dae2495bSdanielk1977do_test pragma-8.1.1 {
749dae2495bSdanielk1977  execsql {
750b92b70bbSdanielk1977    PRAGMA schema_version = 105;
751dae2495bSdanielk1977  }
752dae2495bSdanielk1977} {}
753dae2495bSdanielk1977do_test pragma-8.1.2 {
75425403650Sdrh  execsql2 {
755b92b70bbSdanielk1977    PRAGMA schema_version;
756dae2495bSdanielk1977  }
75725403650Sdrh} {schema_version 105}
758dae2495bSdanielk1977do_test pragma-8.1.3 {
759dae2495bSdanielk1977  execsql {
760b92b70bbSdanielk1977    PRAGMA schema_version = 106;
761dae2495bSdanielk1977  }
762dae2495bSdanielk1977} {}
763dae2495bSdanielk1977do_test pragma-8.1.4 {
764dae2495bSdanielk1977  execsql {
765b92b70bbSdanielk1977    PRAGMA schema_version;
766dae2495bSdanielk1977  }
767dae2495bSdanielk1977} 106
768dae2495bSdanielk1977
769b92b70bbSdanielk1977# Check that creating a table modifies the schema-version (this is really
770b92b70bbSdanielk1977# to verify that the value being read is in fact the schema version).
771dae2495bSdanielk1977do_test pragma-8.1.5 {
772dae2495bSdanielk1977  execsql {
773dae2495bSdanielk1977    CREATE TABLE t4(a, b, c);
774dae2495bSdanielk1977    INSERT INTO t4 VALUES(1, 2, 3);
775dae2495bSdanielk1977    SELECT * FROM t4;
776dae2495bSdanielk1977  }
777dae2495bSdanielk1977} {1 2 3}
778dae2495bSdanielk1977do_test pragma-8.1.6 {
779dae2495bSdanielk1977  execsql {
780b92b70bbSdanielk1977    PRAGMA schema_version;
781dae2495bSdanielk1977  }
782dae2495bSdanielk1977} 107
783dae2495bSdanielk1977
784dae2495bSdanielk1977# Now open a second connection to the database. Ensure that changing the
785b92b70bbSdanielk1977# schema-version using the first connection forces the second connection
786dae2495bSdanielk1977# to reload the schema. This has to be done using the C-API test functions,
787dae2495bSdanielk1977# because the TCL API accounts for SCHEMA_ERROR and retries the query.
788dae2495bSdanielk1977do_test pragma-8.1.7 {
789dddca286Sdrh  sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
790dae2495bSdanielk1977  execsql {
791dae2495bSdanielk1977    SELECT * FROM t4;
792dae2495bSdanielk1977  } db2
793dae2495bSdanielk1977} {1 2 3}
794dae2495bSdanielk1977do_test pragma-8.1.8 {
795dae2495bSdanielk1977  execsql {
796b92b70bbSdanielk1977    PRAGMA schema_version = 108;
797dae2495bSdanielk1977  }
798dae2495bSdanielk1977} {}
799dae2495bSdanielk1977do_test pragma-8.1.9 {
800dae2495bSdanielk1977  set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
801dae2495bSdanielk1977  sqlite3_step $::STMT
802dae2495bSdanielk1977} SQLITE_ERROR
803dae2495bSdanielk1977do_test pragma-8.1.10 {
804dae2495bSdanielk1977  sqlite3_finalize $::STMT
805dae2495bSdanielk1977} SQLITE_SCHEMA
806dae2495bSdanielk1977
807b92b70bbSdanielk1977# Make sure the schema-version can be manipulated in an attached database.
808fda06befSmistachkinforcedelete test2.db
809fda06befSmistachkinforcedelete test2.db-journal
8105a8f9374Sdanielk1977ifcapable attach {
811dae2495bSdanielk1977  do_test pragma-8.1.11 {
812dae2495bSdanielk1977    execsql {
813dae2495bSdanielk1977      ATTACH 'test2.db' AS aux;
814dae2495bSdanielk1977      CREATE TABLE aux.t1(a, b, c);
815b92b70bbSdanielk1977      PRAGMA aux.schema_version = 205;
816dae2495bSdanielk1977    }
817dae2495bSdanielk1977  } {}
818dae2495bSdanielk1977  do_test pragma-8.1.12 {
819dae2495bSdanielk1977    execsql {
820b92b70bbSdanielk1977      PRAGMA aux.schema_version;
821dae2495bSdanielk1977    }
822dae2495bSdanielk1977  } 205
8235a8f9374Sdanielk1977}
824dae2495bSdanielk1977do_test pragma-8.1.13 {
825dae2495bSdanielk1977  execsql {
826b92b70bbSdanielk1977    PRAGMA schema_version;
827dae2495bSdanielk1977  }
828dae2495bSdanielk1977} 108
829dae2495bSdanielk1977
830b92b70bbSdanielk1977# And check that modifying the schema-version in an attached database
831dae2495bSdanielk1977# forces the second connection to reload the schema.
8325a8f9374Sdanielk1977ifcapable attach {
833dae2495bSdanielk1977  do_test pragma-8.1.14 {
834dddca286Sdrh    sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
835dae2495bSdanielk1977    execsql {
836dae2495bSdanielk1977      ATTACH 'test2.db' AS aux;
837dae2495bSdanielk1977      SELECT * FROM aux.t1;
838dae2495bSdanielk1977    } db2
839dae2495bSdanielk1977  } {}
840dae2495bSdanielk1977  do_test pragma-8.1.15 {
841dae2495bSdanielk1977    execsql {
842b92b70bbSdanielk1977      PRAGMA aux.schema_version = 206;
843dae2495bSdanielk1977    }
844dae2495bSdanielk1977  } {}
845dae2495bSdanielk1977  do_test pragma-8.1.16 {
846dae2495bSdanielk1977    set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
847dae2495bSdanielk1977    sqlite3_step $::STMT
848dae2495bSdanielk1977  } SQLITE_ERROR
849dae2495bSdanielk1977  do_test pragma-8.1.17 {
850dae2495bSdanielk1977    sqlite3_finalize $::STMT
851dae2495bSdanielk1977  } SQLITE_SCHEMA
852dae2495bSdanielk1977  do_test pragma-8.1.18 {
853dae2495bSdanielk1977    db2 close
854dae2495bSdanielk1977  } {}
8555a8f9374Sdanielk1977}
856dae2495bSdanielk1977
857b92b70bbSdanielk1977# Now test that the user-version can be read and written (and that we aren't
858b92b70bbSdanielk1977# accidentally manipulating the schema-version instead).
859dae2495bSdanielk1977do_test pragma-8.2.1 {
86025403650Sdrh  execsql2 {
861b92b70bbSdanielk1977    PRAGMA user_version;
862dae2495bSdanielk1977  }
86325403650Sdrh} {user_version 0}
864dae2495bSdanielk1977do_test pragma-8.2.2 {
865dae2495bSdanielk1977  execsql {
866b92b70bbSdanielk1977    PRAGMA user_version = 2;
867dae2495bSdanielk1977  }
868dae2495bSdanielk1977} {}
869802d69a7Sdrhdo_test pragma-8.2.3.1 {
87025403650Sdrh  execsql2 {
871b92b70bbSdanielk1977    PRAGMA user_version;
872dae2495bSdanielk1977  }
87325403650Sdrh} {user_version 2}
874802d69a7Sdrhdo_test pragma-8.2.3.2 {
875802d69a7Sdrh  db close
876802d69a7Sdrh  sqlite3 db test.db
877802d69a7Sdrh  execsql {
878802d69a7Sdrh    PRAGMA user_version;
879802d69a7Sdrh  }
880802d69a7Sdrh} {2}
881802d69a7Sdrhdo_test pragma-8.2.4.1 {
882dae2495bSdanielk1977  execsql {
883b92b70bbSdanielk1977    PRAGMA schema_version;
884dae2495bSdanielk1977  }
885dae2495bSdanielk1977} {108}
886802d69a7Sdrhifcapable vacuum {
887802d69a7Sdrh  do_test pragma-8.2.4.2 {
888802d69a7Sdrh    execsql {
889802d69a7Sdrh      VACUUM;
890802d69a7Sdrh      PRAGMA user_version;
891802d69a7Sdrh    }
892802d69a7Sdrh  } {2}
893802d69a7Sdrh  do_test pragma-8.2.4.3 {
894802d69a7Sdrh    execsql {
895802d69a7Sdrh      PRAGMA schema_version;
896802d69a7Sdrh    }
897802d69a7Sdrh  } {109}
898802d69a7Sdrh}
8995a8f9374Sdanielk1977
9005a8f9374Sdanielk1977ifcapable attach {
901802d69a7Sdrh  db eval {ATTACH 'test2.db' AS aux}
902dae2495bSdanielk1977
903b92b70bbSdanielk1977  # Check that the user-version in the auxilary database can be manipulated (
904dae2495bSdanielk1977  # and that we aren't accidentally manipulating the same in the main db).
905dae2495bSdanielk1977  do_test pragma-8.2.5 {
906dae2495bSdanielk1977    execsql {
907b92b70bbSdanielk1977      PRAGMA aux.user_version;
908dae2495bSdanielk1977    }
909dae2495bSdanielk1977  } {0}
910dae2495bSdanielk1977  do_test pragma-8.2.6 {
911dae2495bSdanielk1977    execsql {
912b92b70bbSdanielk1977      PRAGMA aux.user_version = 3;
913dae2495bSdanielk1977    }
914dae2495bSdanielk1977  } {}
915dae2495bSdanielk1977  do_test pragma-8.2.7 {
916dae2495bSdanielk1977    execsql {
917b92b70bbSdanielk1977      PRAGMA aux.user_version;
918dae2495bSdanielk1977    }
919dae2495bSdanielk1977  } {3}
920dae2495bSdanielk1977  do_test pragma-8.2.8 {
921dae2495bSdanielk1977    execsql {
922b92b70bbSdanielk1977      PRAGMA main.user_version;
923dae2495bSdanielk1977    }
924dae2495bSdanielk1977  } {2}
925dae2495bSdanielk1977
926b92b70bbSdanielk1977  # Now check that a ROLLBACK resets the user-version if it has been modified
927dae2495bSdanielk1977  # within a transaction.
928dae2495bSdanielk1977  do_test pragma-8.2.9 {
929dae2495bSdanielk1977    execsql {
930dae2495bSdanielk1977      BEGIN;
931b92b70bbSdanielk1977      PRAGMA aux.user_version = 10;
932b92b70bbSdanielk1977      PRAGMA user_version = 11;
933dae2495bSdanielk1977    }
934dae2495bSdanielk1977  } {}
935dae2495bSdanielk1977  do_test pragma-8.2.10 {
936dae2495bSdanielk1977    execsql {
937b92b70bbSdanielk1977      PRAGMA aux.user_version;
938dae2495bSdanielk1977    }
939dae2495bSdanielk1977  } {10}
940dae2495bSdanielk1977  do_test pragma-8.2.11 {
941dae2495bSdanielk1977    execsql {
942b92b70bbSdanielk1977      PRAGMA main.user_version;
943dae2495bSdanielk1977    }
944dae2495bSdanielk1977  } {11}
945dae2495bSdanielk1977  do_test pragma-8.2.12 {
946dae2495bSdanielk1977    execsql {
947dae2495bSdanielk1977      ROLLBACK;
948b92b70bbSdanielk1977      PRAGMA aux.user_version;
949dae2495bSdanielk1977    }
950dae2495bSdanielk1977  } {3}
951dae2495bSdanielk1977  do_test pragma-8.2.13 {
952dae2495bSdanielk1977    execsql {
953b92b70bbSdanielk1977      PRAGMA main.user_version;
954dae2495bSdanielk1977    }
955dae2495bSdanielk1977  } {2}
9565a8f9374Sdanielk1977}
957dae2495bSdanielk1977
958b92b70bbSdanielk1977# Try a negative value for the user-version
959dae2495bSdanielk1977do_test pragma-8.2.14 {
960dae2495bSdanielk1977  execsql {
961b92b70bbSdanielk1977    PRAGMA user_version = -450;
962dae2495bSdanielk1977  }
963dae2495bSdanielk1977} {}
964dae2495bSdanielk1977do_test pragma-8.2.15 {
965dae2495bSdanielk1977  execsql {
966b92b70bbSdanielk1977    PRAGMA user_version;
967dae2495bSdanielk1977  }
968dae2495bSdanielk1977} {-450}
969d9c847dcSdanielk1977} ; # ifcapable schema_version
970d9c847dcSdanielk1977
971d19744f3Sdrh# Check to see if TEMP_STORE is memory or disk.  Return strings
972d19744f3Sdrh# "memory" or "disk" as appropriate.
973d19744f3Sdrh#
974d19744f3Sdrhproc check_temp_store {} {
975d19744f3Sdrh  db eval {CREATE TEMP TABLE IF NOT EXISTS a(b)}
976d19744f3Sdrh  db eval {PRAGMA database_list} {
977d19744f3Sdrh    if {$name=="temp"} {
97817b90b53Sdanielk1977      set bt [btree_from_db db 1]
97917b90b53Sdanielk1977      if {[btree_ismemdb $bt]} {
980d19744f3Sdrh        return "memory"
981d19744f3Sdrh      }
98217b90b53Sdanielk1977      return "disk"
983d19744f3Sdrh    }
984d19744f3Sdrh  }
985d19744f3Sdrh  return "unknown"
986d19744f3Sdrh}
987d19744f3Sdrh
9884ee09b4bSdrh# Application_ID
9894ee09b4bSdrh#
9904ee09b4bSdrhdo_test pragma-8.3.1 {
9914ee09b4bSdrh  execsql {
9924ee09b4bSdrh    PRAGMA application_id;
9934ee09b4bSdrh  }
9944ee09b4bSdrh} {0}
9954ee09b4bSdrhdo_test pragma-8.3.2 {
9964ee09b4bSdrh  execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
9974ee09b4bSdrh} {12345}
9989a09a3caStpoindex
9999a09a3caStpoindex# Test temp_store and temp_store_directory pragmas
10009a09a3caStpoindex#
1001268283bcSdrhifcapable pager_pragmas {
10029a09a3caStpoindexdo_test pragma-9.1 {
10039a09a3caStpoindex  db close
10049a09a3caStpoindex  sqlite3 db test.db
10059a09a3caStpoindex  execsql {
10069a09a3caStpoindex    PRAGMA temp_store;
10079a09a3caStpoindex  }
10089a09a3caStpoindex} {0}
1009d19744f3Sdrhif {$TEMP_STORE<=1} {
1010d19744f3Sdrh  do_test pragma-9.1.1 {
1011d19744f3Sdrh    check_temp_store
1012d19744f3Sdrh  } {disk}
1013d19744f3Sdrh} else {
1014d19744f3Sdrh  do_test pragma-9.1.1 {
1015d19744f3Sdrh    check_temp_store
1016d19744f3Sdrh  } {memory}
1017d19744f3Sdrh}
1018d19744f3Sdrh
10199a09a3caStpoindexdo_test pragma-9.2 {
1020d19744f3Sdrh  db close
1021d19744f3Sdrh  sqlite3 db test.db
10229a09a3caStpoindex  execsql {
10239a09a3caStpoindex    PRAGMA temp_store=file;
10249a09a3caStpoindex    PRAGMA temp_store;
10259a09a3caStpoindex  }
10269a09a3caStpoindex} {1}
1027d19744f3Sdrhif {$TEMP_STORE==3} {
1028d19744f3Sdrh  # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1029d19744f3Sdrh  do_test pragma-9.2.1 {
1030d19744f3Sdrh    check_temp_store
1031d19744f3Sdrh  } {memory}
1032d19744f3Sdrh} else {
1033d19744f3Sdrh  do_test pragma-9.2.1 {
1034d19744f3Sdrh    check_temp_store
1035d19744f3Sdrh  } {disk}
1036d19744f3Sdrh}
1037d19744f3Sdrh
10389a09a3caStpoindexdo_test pragma-9.3 {
1039d19744f3Sdrh  db close
1040d19744f3Sdrh  sqlite3 db test.db
10419a09a3caStpoindex  execsql {
10429a09a3caStpoindex    PRAGMA temp_store=memory;
10439a09a3caStpoindex    PRAGMA temp_store;
10449a09a3caStpoindex  }
10459a09a3caStpoindex} {2}
1046d19744f3Sdrhif {$TEMP_STORE==0} {
1047d19744f3Sdrh  # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1048d19744f3Sdrh  do_test pragma-9.3.1 {
1049d19744f3Sdrh    check_temp_store
1050d19744f3Sdrh  } {disk}
1051d19744f3Sdrh} else {
1052d19744f3Sdrh  do_test pragma-9.3.1 {
1053d19744f3Sdrh    check_temp_store
1054d19744f3Sdrh  } {memory}
1055d19744f3Sdrh}
1056d19744f3Sdrh
10579a09a3caStpoindexdo_test pragma-9.4 {
10589a09a3caStpoindex  execsql {
10599a09a3caStpoindex    PRAGMA temp_store_directory;
10609a09a3caStpoindex  }
10619a09a3caStpoindex} {}
106278f82d1eSdrhifcapable wsd {
10639a09a3caStpoindex  do_test pragma-9.5 {
1064f8a78464Smistachkin    set pwd [string map {' ''} [file nativename [get_pwd]]]
1065268283bcSdrh    execsql "
1066268283bcSdrh      PRAGMA temp_store_directory='$pwd';
10679a09a3caStpoindex    "
10689a09a3caStpoindex  } {}
10699a09a3caStpoindex  do_test pragma-9.6 {
10709a09a3caStpoindex    execsql {
10719a09a3caStpoindex      PRAGMA temp_store_directory;
10729a09a3caStpoindex    }
1073f8a78464Smistachkin  } [list [file nativename [get_pwd]]]
10749a09a3caStpoindex  do_test pragma-9.7 {
1075268283bcSdrh    catchsql {
10769a09a3caStpoindex      PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
10779a09a3caStpoindex    }
1078268283bcSdrh  } {1 {not a writable directory}}
10799a09a3caStpoindex  do_test pragma-9.8 {
10809a09a3caStpoindex    execsql {
10819a09a3caStpoindex      PRAGMA temp_store_directory='';
10829a09a3caStpoindex    }
10839a09a3caStpoindex  } {}
10841e9daa6aSdrh  if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
108553c0f748Sdanielk1977    ifcapable tempdb {
10869a09a3caStpoindex      do_test pragma-9.9 {
10879a09a3caStpoindex        execsql {
10889a09a3caStpoindex          PRAGMA temp_store_directory;
10899a09a3caStpoindex          PRAGMA temp_store=FILE;
10909a09a3caStpoindex          CREATE TEMP TABLE temp_store_directory_test(a integer);
10919a09a3caStpoindex          INSERT INTO temp_store_directory_test values (2);
10929a09a3caStpoindex          SELECT * FROM temp_store_directory_test;
10939a09a3caStpoindex        }
10949a09a3caStpoindex      } {2}
10959a09a3caStpoindex      do_test pragma-9.10 {
1096268283bcSdrh        catchsql "
1097268283bcSdrh          PRAGMA temp_store_directory='$pwd';
10989a09a3caStpoindex          SELECT * FROM temp_store_directory_test;
10999a09a3caStpoindex        "
1100268283bcSdrh      } {1 {no such table: temp_store_directory_test}}
11011e9daa6aSdrh    }
11021e9daa6aSdrh  }
110378f82d1eSdrh}
110495b289b6Sdanielk1977do_test pragma-9.11 {
110595b289b6Sdanielk1977  execsql {
110695b289b6Sdanielk1977    PRAGMA temp_store = 0;
110795b289b6Sdanielk1977    PRAGMA temp_store;
110895b289b6Sdanielk1977  }
110995b289b6Sdanielk1977} {0}
111095b289b6Sdanielk1977do_test pragma-9.12 {
111195b289b6Sdanielk1977  execsql {
111295b289b6Sdanielk1977    PRAGMA temp_store = 1;
111395b289b6Sdanielk1977    PRAGMA temp_store;
111495b289b6Sdanielk1977  }
111595b289b6Sdanielk1977} {1}
111695b289b6Sdanielk1977do_test pragma-9.13 {
111795b289b6Sdanielk1977  execsql {
111895b289b6Sdanielk1977    PRAGMA temp_store = 2;
111995b289b6Sdanielk1977    PRAGMA temp_store;
112095b289b6Sdanielk1977  }
112195b289b6Sdanielk1977} {2}
112295b289b6Sdanielk1977do_test pragma-9.14 {
112395b289b6Sdanielk1977  execsql {
112495b289b6Sdanielk1977    PRAGMA temp_store = 3;
112595b289b6Sdanielk1977    PRAGMA temp_store;
112695b289b6Sdanielk1977  }
112795b289b6Sdanielk1977} {0}
112895b289b6Sdanielk1977do_test pragma-9.15 {
112995b289b6Sdanielk1977  catchsql {
113095b289b6Sdanielk1977    BEGIN EXCLUSIVE;
113195b289b6Sdanielk1977    CREATE TEMP TABLE temp_table(t);
113295b289b6Sdanielk1977    INSERT INTO temp_table VALUES('valuable data');
113395b289b6Sdanielk1977    PRAGMA temp_store = 1;
113495b289b6Sdanielk1977  }
113595b289b6Sdanielk1977} {1 {temporary storage cannot be changed from within a transaction}}
113695b289b6Sdanielk1977do_test pragma-9.16 {
113795b289b6Sdanielk1977  execsql {
113895b289b6Sdanielk1977    SELECT * FROM temp_table;
113995b289b6Sdanielk1977    COMMIT;
114095b289b6Sdanielk1977  }
114195b289b6Sdanielk1977} {{valuable data}}
1142983e2309Sdanielk1977
1143983e2309Sdanielk1977do_test pragma-9.17 {
1144983e2309Sdanielk1977  execsql {
1145983e2309Sdanielk1977    INSERT INTO temp_table VALUES('valuable data II');
1146983e2309Sdanielk1977    SELECT * FROM temp_table;
1147983e2309Sdanielk1977  }
1148983e2309Sdanielk1977} {{valuable data} {valuable data II}}
1149983e2309Sdanielk1977
1150983e2309Sdanielk1977do_test pragma-9.18 {
1151983e2309Sdanielk1977  set rc [catch {
1152983e2309Sdanielk1977    db eval {SELECT t FROM temp_table} {
1153983e2309Sdanielk1977      execsql {pragma temp_store = 1}
1154983e2309Sdanielk1977    }
1155983e2309Sdanielk1977  } msg]
1156983e2309Sdanielk1977  list $rc $msg
1157983e2309Sdanielk1977} {1 {temporary storage cannot be changed from within a transaction}}
1158983e2309Sdanielk1977
1159268283bcSdrh} ;# ifcapable pager_pragmas
11609a09a3caStpoindex
1161cc6bd383Sdanielk1977ifcapable trigger {
1162cc6bd383Sdanielk1977
1163cc6bd383Sdanielk1977do_test pragma-10.0 {
1164cc6bd383Sdanielk1977  catchsql {
1165cc6bd383Sdanielk1977    DROP TABLE main.t1;
1166cc6bd383Sdanielk1977  }
1167cc6bd383Sdanielk1977  execsql {
1168cc6bd383Sdanielk1977    PRAGMA count_changes = 1;
1169cc6bd383Sdanielk1977
1170cc6bd383Sdanielk1977    CREATE TABLE t1(a PRIMARY KEY);
1171cc6bd383Sdanielk1977    CREATE TABLE t1_mirror(a);
1172cc6bd383Sdanielk1977    CREATE TABLE t1_mirror2(a);
1173cc6bd383Sdanielk1977    CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1174cc6bd383Sdanielk1977      INSERT INTO t1_mirror VALUES(new.a);
1175cc6bd383Sdanielk1977    END;
1176cc6bd383Sdanielk1977    CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1177cc6bd383Sdanielk1977      INSERT INTO t1_mirror2 VALUES(new.a);
1178cc6bd383Sdanielk1977    END;
1179cc6bd383Sdanielk1977    CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1180cc6bd383Sdanielk1977      UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1181cc6bd383Sdanielk1977    END;
1182cc6bd383Sdanielk1977    CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1183cc6bd383Sdanielk1977      UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1184cc6bd383Sdanielk1977    END;
1185cc6bd383Sdanielk1977    CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1186cc6bd383Sdanielk1977      DELETE FROM t1_mirror WHERE a = old.a;
1187cc6bd383Sdanielk1977    END;
1188cc6bd383Sdanielk1977    CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1189cc6bd383Sdanielk1977      DELETE FROM t1_mirror2 WHERE a = old.a;
1190cc6bd383Sdanielk1977    END;
1191cc6bd383Sdanielk1977  }
1192cc6bd383Sdanielk1977} {}
1193cc6bd383Sdanielk1977
1194cc6bd383Sdanielk1977do_test pragma-10.1 {
1195cc6bd383Sdanielk1977  execsql {
1196cc6bd383Sdanielk1977    INSERT INTO t1 VALUES(randstr(10,10));
1197cc6bd383Sdanielk1977  }
1198cc6bd383Sdanielk1977} {1}
1199cc6bd383Sdanielk1977do_test pragma-10.2 {
1200cc6bd383Sdanielk1977  execsql {
1201cc6bd383Sdanielk1977    UPDATE t1 SET a = randstr(10,10);
1202cc6bd383Sdanielk1977  }
1203cc6bd383Sdanielk1977} {1}
1204cc6bd383Sdanielk1977do_test pragma-10.3 {
1205cc6bd383Sdanielk1977  execsql {
1206cc6bd383Sdanielk1977    DELETE FROM t1;
1207cc6bd383Sdanielk1977  }
1208cc6bd383Sdanielk1977} {1}
1209cc6bd383Sdanielk1977
1210cc6bd383Sdanielk1977} ;# ifcapable trigger
1211cc6bd383Sdanielk1977
121248af65aeSdanielk1977ifcapable schema_pragmas {
121348af65aeSdanielk1977  do_test pragma-11.1 {
121448af65aeSdanielk1977    execsql2 {
121548af65aeSdanielk1977      pragma collation_list;
121648af65aeSdanielk1977    }
12172d823315Sdrh  } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
121848af65aeSdanielk1977  do_test pragma-11.2 {
121948af65aeSdanielk1977    db collate New_Collation blah...
122048af65aeSdanielk1977    execsql {
122148af65aeSdanielk1977      pragma collation_list;
122248af65aeSdanielk1977    }
12232d823315Sdrh  } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
122448af65aeSdanielk1977}
122548af65aeSdanielk1977
1226ddfb2f03Sdanielk1977ifcapable schema_pragmas&&tempdb {
1227ddfb2f03Sdanielk1977  do_test pragma-12.1 {
1228ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1229ddfb2f03Sdanielk1977    execsql {
1230ddfb2f03Sdanielk1977      PRAGMA temp.table_info('abc');
1231ddfb2f03Sdanielk1977    } db2
1232ddfb2f03Sdanielk1977  } {}
1233ddfb2f03Sdanielk1977  db2 close
1234ddfb2f03Sdanielk1977
1235ddfb2f03Sdanielk1977  do_test pragma-12.2 {
1236ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1237ddfb2f03Sdanielk1977    execsql {
1238ddfb2f03Sdanielk1977      PRAGMA temp.default_cache_size = 200;
1239ddfb2f03Sdanielk1977      PRAGMA temp.default_cache_size;
1240ddfb2f03Sdanielk1977    } db2
1241ddfb2f03Sdanielk1977  } {200}
1242ddfb2f03Sdanielk1977  db2 close
1243ddfb2f03Sdanielk1977
1244ddfb2f03Sdanielk1977  do_test pragma-12.3 {
1245ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1246ddfb2f03Sdanielk1977    execsql {
1247ddfb2f03Sdanielk1977      PRAGMA temp.cache_size = 400;
1248ddfb2f03Sdanielk1977      PRAGMA temp.cache_size;
1249ddfb2f03Sdanielk1977    } db2
1250ddfb2f03Sdanielk1977  } {400}
1251ddfb2f03Sdanielk1977  db2 close
1252ddfb2f03Sdanielk1977}
1253ddfb2f03Sdanielk1977
12544b2688abSdanielk1977ifcapable bloblit {
12554b2688abSdanielk1977
125605a82983Sdrhdo_test pragma-13.1 {
125705a82983Sdrh  execsql {
125805a82983Sdrh    DROP TABLE IF EXISTS t4;
125905a82983Sdrh    PRAGMA vdbe_trace=on;
126005a82983Sdrh    PRAGMA vdbe_listing=on;
126105a82983Sdrh    PRAGMA sql_trace=on;
126205a82983Sdrh    CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
126305a82983Sdrh    INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
126405a82983Sdrh    INSERT INTO t4(b) VALUES(randstr(30,30));
126505a82983Sdrh    INSERT INTO t4(b) VALUES(1.23456);
126605a82983Sdrh    INSERT INTO t4(b) VALUES(NULL);
126705a82983Sdrh    INSERT INTO t4(b) VALUES(0);
126805a82983Sdrh    INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
126905a82983Sdrh    SELECT * FROM t4;
127005a82983Sdrh  }
127105a82983Sdrh  execsql {
127205a82983Sdrh    PRAGMA vdbe_trace=off;
127305a82983Sdrh    PRAGMA vdbe_listing=off;
127405a82983Sdrh    PRAGMA sql_trace=off;
127505a82983Sdrh  }
127605a82983Sdrh} {}
127705a82983Sdrh
12784b2688abSdanielk1977} ;# ifcapable bloblit
12794b2688abSdanielk1977
128059a93791Sdanielk1977ifcapable pager_pragmas {
128159a93791Sdanielk1977  db close
1282fda06befSmistachkin  forcedelete test.db
128359a93791Sdanielk1977  sqlite3 db test.db
128459a93791Sdanielk1977
128559a93791Sdanielk1977  do_test pragma-14.1 {
128659a93791Sdanielk1977    execsql { pragma auto_vacuum = 0 }
128759a93791Sdanielk1977    execsql { pragma page_count }
128859a93791Sdanielk1977  } {0}
128959a93791Sdanielk1977
129059a93791Sdanielk1977  do_test pragma-14.2 {
129159a93791Sdanielk1977    execsql {
129259a93791Sdanielk1977      CREATE TABLE abc(a, b, c);
129359a93791Sdanielk1977      PRAGMA page_count;
129459a93791Sdanielk1977    }
129559a93791Sdanielk1977  } {2}
12965d16a9a6Sdrh  do_test pragma-14.2uc {
12975d16a9a6Sdrh    execsql {pragma PAGE_COUNT}
12985d16a9a6Sdrh  } {2}
129959a93791Sdanielk1977
130059a93791Sdanielk1977  do_test pragma-14.3 {
130159a93791Sdanielk1977    execsql {
130259a93791Sdanielk1977      BEGIN;
130359a93791Sdanielk1977      CREATE TABLE def(a, b, c);
130459a93791Sdanielk1977      PRAGMA page_count;
130559a93791Sdanielk1977    }
130659a93791Sdanielk1977  } {3}
13075d16a9a6Sdrh  do_test pragma-14.3uc {
13085d16a9a6Sdrh    execsql {pragma PAGE_COUNT}
13095d16a9a6Sdrh  } {3}
131059a93791Sdanielk1977
131159a93791Sdanielk1977  do_test pragma-14.4 {
131259a93791Sdanielk1977    set page_size [db one {pragma page_size}]
131359a93791Sdanielk1977    expr [file size test.db] / $page_size
131459a93791Sdanielk1977  } {2}
131559a93791Sdanielk1977
131659a93791Sdanielk1977  do_test pragma-14.5 {
131759a93791Sdanielk1977    execsql {
131859a93791Sdanielk1977      ROLLBACK;
131959a93791Sdanielk1977      PRAGMA page_count;
132059a93791Sdanielk1977    }
132159a93791Sdanielk1977  } {2}
132259a93791Sdanielk1977
132359a93791Sdanielk1977  do_test pragma-14.6 {
1324fda06befSmistachkin    forcedelete test2.db
132559a93791Sdanielk1977    sqlite3 db2 test2.db
132659a93791Sdanielk1977    execsql {
132759a93791Sdanielk1977      PRAGMA auto_vacuum = 0;
132859a93791Sdanielk1977      CREATE TABLE t1(a, b, c);
132959a93791Sdanielk1977      CREATE TABLE t2(a, b, c);
133059a93791Sdanielk1977      CREATE TABLE t3(a, b, c);
133159a93791Sdanielk1977      CREATE TABLE t4(a, b, c);
133259a93791Sdanielk1977    } db2
133359a93791Sdanielk1977    db2 close
133459a93791Sdanielk1977    execsql {
133559a93791Sdanielk1977      ATTACH 'test2.db' AS aux;
133659a93791Sdanielk1977      PRAGMA aux.page_count;
133759a93791Sdanielk1977    }
133859a93791Sdanielk1977  } {5}
13395d16a9a6Sdrh  do_test pragma-14.6uc {
13405d16a9a6Sdrh    execsql {pragma AUX.PAGE_COUNT}
13415d16a9a6Sdrh  } {5}
134259a93791Sdanielk1977}
134359a93791Sdanielk1977
13448cf6c554Sdanielk1977# Test that the value set using the cache_size pragma is not reset when the
13458cf6c554Sdanielk1977# schema is reloaded.
13468cf6c554Sdanielk1977#
13478cf6c554Sdanielk1977ifcapable pager_pragmas {
13488cf6c554Sdanielk1977  db close
13498cf6c554Sdanielk1977  sqlite3 db test.db
13508cf6c554Sdanielk1977  do_test pragma-15.1 {
13518cf6c554Sdanielk1977    execsql {
13528cf6c554Sdanielk1977      PRAGMA cache_size=59;
13538cf6c554Sdanielk1977      PRAGMA cache_size;
13548cf6c554Sdanielk1977    }
13558cf6c554Sdanielk1977  } {59}
13568cf6c554Sdanielk1977  do_test pragma-15.2 {
13578cf6c554Sdanielk1977    sqlite3 db2 test.db
13588cf6c554Sdanielk1977    execsql {
13598cf6c554Sdanielk1977      CREATE TABLE newtable(a, b, c);
13608cf6c554Sdanielk1977    } db2
13618cf6c554Sdanielk1977    db2 close
13628cf6c554Sdanielk1977  } {}
13638cf6c554Sdanielk1977  do_test pragma-15.3 {
13648cf6c554Sdanielk1977    # Evaluating this statement will cause the schema to be reloaded (because
13658cf6c554Sdanielk1977    # the schema was changed by another connection in pragma-15.2). At one
13668cf6c554Sdanielk1977    # point there was a bug that reset the cache_size to its default value
13678cf6c554Sdanielk1977    # when this happened.
13688cf6c554Sdanielk1977    execsql { SELECT * FROM sqlite_master }
13698cf6c554Sdanielk1977    execsql { PRAGMA cache_size }
13708cf6c554Sdanielk1977  } {59}
13718cf6c554Sdanielk1977}
13728cf6c554Sdanielk1977
13735558a8a6Sdanielk1977# Reset the sqlite3_temp_directory variable for the next run of tests:
13745558a8a6Sdanielk1977sqlite3 dbX :memory:
13755558a8a6Sdanielk1977dbX eval {PRAGMA temp_store_directory = ""}
13765558a8a6Sdanielk1977dbX close
13775558a8a6Sdanielk1977
1378838cce43Sdanielk1977ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
1379aebf413dSaswift  set sqlite_hostid_num 1
1380aebf413dSaswift
1381aebf413dSaswift  set using_proxy 0
1382aebf413dSaswift  foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1383aebf413dSaswift    set using_proxy $value
1384aebf413dSaswift  }
1385aebf413dSaswift
1386aebf413dSaswift  # Test the lock_proxy_file pragmas.
1387aebf413dSaswift  #
1388aebf413dSaswift  db close
1389aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1390aebf413dSaswift
1391aebf413dSaswift  sqlite3 db test.db
1392aebf413dSaswift  do_test pragma-16.1 {
1393aebf413dSaswift    execsql {
1394aebf413dSaswift      PRAGMA lock_proxy_file="mylittleproxy";
1395aebf413dSaswift      select * from sqlite_master;
1396aebf413dSaswift    }
1397aebf413dSaswift    execsql {
1398aebf413dSaswift      PRAGMA lock_proxy_file;
1399aebf413dSaswift    }
1400aebf413dSaswift  } {mylittleproxy}
1401aebf413dSaswift
1402aebf413dSaswift  do_test pragma-16.2 {
1403aebf413dSaswift    sqlite3 db2 test.db
1404aebf413dSaswift    execsql {
1405aebf413dSaswift      PRAGMA lock_proxy_file="mylittleproxy";
1406aebf413dSaswift    } db2
1407aebf413dSaswift  } {}
1408aebf413dSaswift
1409aebf413dSaswift  db2 close
1410aebf413dSaswift  do_test pragma-16.2.1 {
1411aebf413dSaswift    sqlite3 db2 test.db
1412aebf413dSaswift    execsql {
1413aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1414aebf413dSaswift      select * from sqlite_master;
1415aebf413dSaswift    } db2
1416aebf413dSaswift    execsql {
1417aebf413dSaswift      PRAGMA lock_proxy_file;
1418aebf413dSaswift    } db2
1419aebf413dSaswift  } {mylittleproxy}
1420aebf413dSaswift
1421aebf413dSaswift  db2 close
1422aebf413dSaswift  do_test pragma-16.3 {
1423aebf413dSaswift    sqlite3 db2 test.db
1424aebf413dSaswift    execsql {
1425aebf413dSaswift      PRAGMA lock_proxy_file="myotherproxy";
1426aebf413dSaswift    } db2
1427aebf413dSaswift    catchsql {
1428aebf413dSaswift      select * from sqlite_master;
1429aebf413dSaswift    } db2
1430aebf413dSaswift  } {1 {database is locked}}
1431aebf413dSaswift
1432aebf413dSaswift  do_test pragma-16.4 {
1433aebf413dSaswift    db2 close
1434aebf413dSaswift    db close
1435aebf413dSaswift    sqlite3 db2 test.db
1436aebf413dSaswift    execsql {
1437aebf413dSaswift      PRAGMA lock_proxy_file="myoriginalproxy";
1438aebf413dSaswift      PRAGMA lock_proxy_file="myotherproxy";
1439aebf413dSaswift      PRAGMA lock_proxy_file;
1440aebf413dSaswift    } db2
1441aebf413dSaswift  } {myotherproxy}
1442aebf413dSaswift
1443aebf413dSaswift  db2 close
1444aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1445aebf413dSaswift  do_test pragma-16.5 {
1446aebf413dSaswift    sqlite3 db2 test.db
1447aebf413dSaswift    execsql {
1448aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1449aebf413dSaswift      PRAGMA lock_proxy_file;
1450aebf413dSaswift    } db2
1451aebf413dSaswift  } {myotherproxy}
1452aebf413dSaswift
1453aebf413dSaswift  do_test pragma-16.6 {
1454aebf413dSaswift    db2 close
1455aebf413dSaswift    sqlite3 db2 test2.db
1456aebf413dSaswift    set lockpath [execsql {
1457aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1458aebf413dSaswift      PRAGMA lock_proxy_file;
1459aebf413dSaswift    } db2]
1460aebf413dSaswift    string match "*test2.db:auto:" $lockpath
1461aebf413dSaswift  } {1}
1462aebf413dSaswift
1463aebf413dSaswift  set sqlite_hostid_num 2
1464aebf413dSaswift  do_test pragma-16.7 {
146514d14607Sdan    list [catch {
1466aebf413dSaswift      sqlite3 db test2.db
1467aebf413dSaswift      execsql {
1468aebf413dSaswift        PRAGMA lock_proxy_file=":auto:";
1469aebf413dSaswift        select * from sqlite_master;
1470aebf413dSaswift      }
147114d14607Sdan    } msg] $msg
1472aebf413dSaswift  } {1 {database is locked}}
1473aebf413dSaswift  db close
1474aebf413dSaswift
1475aebf413dSaswift  do_test pragma-16.8 {
147614d14607Sdan    list [catch {
1477aebf413dSaswift      sqlite3 db test2.db
147814d14607Sdan      execsql { select * from sqlite_master }
147914d14607Sdan    } msg] $msg
1480aebf413dSaswift  } {1 {database is locked}}
1481aebf413dSaswift
1482aebf413dSaswift  db2 close
1483aebf413dSaswift  do_test pragma-16.8.1 {
1484aebf413dSaswift    execsql {
1485aebf413dSaswift      PRAGMA lock_proxy_file="yetanotherproxy";
1486aebf413dSaswift      PRAGMA lock_proxy_file;
1487aebf413dSaswift    }
1488aebf413dSaswift  } {yetanotherproxy}
1489aebf413dSaswift  do_test pragma-16.8.2 {
1490aebf413dSaswift    execsql {
1491aebf413dSaswift      create table mine(x);
1492aebf413dSaswift    }
1493aebf413dSaswift  } {}
1494aebf413dSaswift
1495aebf413dSaswift  db close
1496aebf413dSaswift  do_test pragma-16.9 {
1497aebf413dSaswift    sqlite3 db proxytest.db
1498aebf413dSaswift    set lockpath2 [execsql {
1499aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1500aebf413dSaswift      PRAGMA lock_proxy_file;
1501aebf413dSaswift    } db]
1502aebf413dSaswift    string match "*proxytest.db:auto:" $lockpath2
1503aebf413dSaswift  } {1}
1504aebf413dSaswift
1505aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1506aebf413dSaswift  set sqlite_hostid_num 0
1507aebf413dSaswift}
1508d2cb50b7Sdrh
1509d2cb50b7Sdrh# Parsing of auto_vacuum settings.
1510d2cb50b7Sdrh#
1511d2cb50b7Sdrhforeach {autovac_setting val} {
1512d2cb50b7Sdrh  0 0
1513d2cb50b7Sdrh  1 1
1514d2cb50b7Sdrh  2 2
1515d2cb50b7Sdrh  3 0
1516d2cb50b7Sdrh  -1 0
1517d2cb50b7Sdrh  none 0
1518d2cb50b7Sdrh  NONE 0
1519d2cb50b7Sdrh  NoNe 0
1520d2cb50b7Sdrh  full 1
1521d2cb50b7Sdrh  FULL 1
1522d2cb50b7Sdrh  incremental 2
1523d2cb50b7Sdrh  INCREMENTAL 2
1524d2cb50b7Sdrh  -1234 0
1525d2cb50b7Sdrh  1234 0
1526d2cb50b7Sdrh} {
1527d2cb50b7Sdrh  do_test pragma-17.1.$autovac_setting {
1528d2cb50b7Sdrh    catch {db close}
1529d2cb50b7Sdrh    sqlite3 db :memory:
1530d2cb50b7Sdrh    execsql "
1531d2cb50b7Sdrh      PRAGMA auto_vacuum=$::autovac_setting;
1532d2cb50b7Sdrh      PRAGMA auto_vacuum;
1533d2cb50b7Sdrh    "
1534d2cb50b7Sdrh  } $val
1535d2cb50b7Sdrh}
1536d2cb50b7Sdrh
1537d2cb50b7Sdrh# Parsing of temp_store settings.
1538d2cb50b7Sdrh#
1539d2cb50b7Sdrhforeach {temp_setting val} {
1540d2cb50b7Sdrh  0 0
1541d2cb50b7Sdrh  1 1
1542d2cb50b7Sdrh  2 2
1543d2cb50b7Sdrh  3 0
1544d2cb50b7Sdrh  -1 0
1545d2cb50b7Sdrh  file 1
1546d2cb50b7Sdrh  FILE 1
1547d2cb50b7Sdrh  fIlE 1
1548d2cb50b7Sdrh  memory 2
1549d2cb50b7Sdrh  MEMORY 2
1550d2cb50b7Sdrh  MeMoRy 2
1551d2cb50b7Sdrh} {
1552d2cb50b7Sdrh  do_test pragma-18.1.$temp_setting {
1553d2cb50b7Sdrh    catch {db close}
1554d2cb50b7Sdrh    sqlite3 db :memory:
1555d2cb50b7Sdrh    execsql "
1556d2cb50b7Sdrh      PRAGMA temp_store=$::temp_setting;
1557d2cb50b7Sdrh      PRAGMA temp_store=$::temp_setting;
1558d2cb50b7Sdrh      PRAGMA temp_store;
1559d2cb50b7Sdrh    "
1560d2cb50b7Sdrh  } $val
1561d2cb50b7Sdrh}
1562d2cb50b7Sdrh
156392c700dbSdrh# The SQLITE_FCNTL_PRAGMA logic, with error handling.
156492c700dbSdrh#
156592c700dbSdrhdb close
156692c700dbSdrhtestvfs tvfs
156792c700dbSdrhsqlite3 db test.db -vfs tvfs
156892c700dbSdrhdo_test pragma-19.1 {
156992c700dbSdrh  catchsql {PRAGMA error}
157092c700dbSdrh} {1 {SQL logic error or missing database}}
157192c700dbSdrhdo_test pragma-19.2 {
157292c700dbSdrh  catchsql {PRAGMA error='This is the error message'}
157392c700dbSdrh} {1 {This is the error message}}
157492c700dbSdrhdo_test pragma-19.3 {
157592c700dbSdrh  catchsql {PRAGMA error='7 This is the error message'}
157692c700dbSdrh} {1 {This is the error message}}
157792c700dbSdrhdo_test pragma-19.4 {
157892c700dbSdrh  catchsql {PRAGMA error=7}
157992c700dbSdrh} {1 {out of memory}}
1580c8517f61Sdrhdo_test pragma-19.5 {
15815b044545Smistachkin  file tail [lindex [execsql {PRAGMA filename}] 0]
1582c8517f61Sdrh} {test.db}
158392c700dbSdrh
1584cc71645bSdrhif {$tcl_platform(platform)=="windows"} {
1585a112d140Smistachkin# Test data_store_directory pragma
1586a112d140Smistachkin#
1587a112d140Smistachkindb close
1588a112d140Smistachkinsqlite3 db test.db
1589a112d140Smistachkinfile mkdir data_dir
1590a112d140Smistachkindo_test pragma-20.1 {
1591a112d140Smistachkin  catchsql {PRAGMA data_store_directory}
1592a112d140Smistachkin} {0 {}}
1593a112d140Smistachkindo_test pragma-20.2 {
1594a112d140Smistachkin  set pwd [string map {' ''} [file nativename [get_pwd]]]
1595a112d140Smistachkin  catchsql "PRAGMA data_store_directory='$pwd';"
1596a112d140Smistachkin} {0 {}}
1597a112d140Smistachkindo_test pragma-20.3 {
1598a112d140Smistachkin  catchsql {PRAGMA data_store_directory}
1599a112d140Smistachkin} [list 0 [list [file nativename [get_pwd]]]]
1600a112d140Smistachkindo_test pragma-20.4 {
1601a112d140Smistachkin  set pwd [string map {' ''} [file nativename \
1602a112d140Smistachkin    [file join [get_pwd] data_dir]]]
1603a112d140Smistachkin  catchsql "PRAGMA data_store_directory='$pwd';"
1604a112d140Smistachkin} {0 {}}
1605a112d140Smistachkindo_test pragma-20.5 {
1606a112d140Smistachkin  sqlite3 db2 test2.db
1607a112d140Smistachkin  catchsql "PRAGMA database_list;" db2
1608a112d140Smistachkin} [list 0 [list 0 main [file nativename \
1609a112d140Smistachkin    [file join [get_pwd] data_dir test2.db]]]]
1610a112d140Smistachkincatch {db2 close}
1611a112d140Smistachkindo_test pragma-20.6 {
1612a112d140Smistachkin  sqlite3 db2 [file join [get_pwd] test2.db]
1613a112d140Smistachkin  catchsql "PRAGMA database_list;" db2
1614a112d140Smistachkin} [list 0 [list 0 main [file nativename \
1615a112d140Smistachkin    [file join [get_pwd] test2.db]]]]
1616a112d140Smistachkincatch {db2 close}
1617a112d140Smistachkindo_test pragma-20.7 {
1618a112d140Smistachkin  catchsql "PRAGMA data_store_directory='';"
1619a112d140Smistachkin} {0 {}}
1620a112d140Smistachkindo_test pragma-20.8 {
1621a112d140Smistachkin  catchsql {PRAGMA data_store_directory}
1622a112d140Smistachkin} {0 {}}
162392c700dbSdrh
1624a112d140Smistachkinforcedelete data_dir
1625cc71645bSdrh} ;# endif windows
1626cd61c281Sdrh
16271fed5dabSdandatabase_may_be_corrupt
16281fed5dabSdan
16295885e762Sdando_test 21.1 {
16305885e762Sdan  # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
16315885e762Sdan  #
16325885e762Sdan  db close
16335885e762Sdan  forcedelete test.db
16345885e762Sdan  sqlite3 db test.db
16355885e762Sdan  execsql {
16365885e762Sdan    PRAGMA page_size = 1024;
16375885e762Sdan    PRAGMA auto_vacuum = 0;
16385885e762Sdan    CREATE TABLE t1(a PRIMARY KEY, b);
16395885e762Sdan    INSERT INTO t1 VALUES(1, 1);
16405885e762Sdan  }
16415885e762Sdan  for {set i 0} {$i < 10} {incr i} {
16425885e762Sdan    execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
16435885e762Sdan  }
16445885e762Sdan  db close
16455885e762Sdan  forcecopy test.db testerr.db
16465885e762Sdan  hexio_write testerr.db 15000 [string repeat 55 100]
16475885e762Sdan} {100}
16485885e762Sdan
16495885e762Sdanset mainerr {*** in database main ***
16505885e762SdanMultiple uses for byte 672 of page 15}
16515885e762Sdanset auxerr {*** in database aux ***
16525885e762SdanMultiple uses for byte 672 of page 15}
16535885e762Sdan
1654597515d7Sdanset mainerr {/{\*\*\* in database main \*\*\*
1655597515d7SdanMultiple uses for byte 672 of page 15}.*/}
1656597515d7Sdanset auxerr {/{\*\*\* in database aux \*\*\*
1657597515d7SdanMultiple uses for byte 672 of page 15}.*/}
1658597515d7Sdan
16595885e762Sdando_test 22.2 {
16605885e762Sdan  catch { db close }
16615885e762Sdan  sqlite3 db testerr.db
16625885e762Sdan  execsql { PRAGMA integrity_check }
1663597515d7Sdan} $mainerr
16645885e762Sdan
16655885e762Sdando_test 22.3.1 {
16665885e762Sdan  catch { db close }
16675885e762Sdan  sqlite3 db test.db
16685885e762Sdan  execsql {
16695885e762Sdan    ATTACH 'testerr.db' AS 'aux';
16705885e762Sdan    PRAGMA integrity_check;
16715885e762Sdan  }
1672597515d7Sdan} $auxerr
16735885e762Sdando_test 22.3.2 {
16745885e762Sdan  execsql { PRAGMA main.integrity_check; }
16755885e762Sdan} {ok}
16765885e762Sdando_test 22.3.3 {
16775885e762Sdan  execsql { PRAGMA aux.integrity_check; }
1678597515d7Sdan} $auxerr
16795885e762Sdan
16805885e762Sdando_test 22.4.1 {
16815885e762Sdan  catch { db close }
16825885e762Sdan  sqlite3 db testerr.db
16835885e762Sdan  execsql {
16845885e762Sdan    ATTACH 'test.db' AS 'aux';
16855885e762Sdan    PRAGMA integrity_check;
16865885e762Sdan  }
1687597515d7Sdan} $mainerr
16885885e762Sdando_test 22.4.2 {
16895885e762Sdan  execsql { PRAGMA main.integrity_check; }
1690597515d7Sdan} $mainerr
16915885e762Sdando_test 22.4.3 {
16925885e762Sdan  execsql { PRAGMA aux.integrity_check; }
16935885e762Sdan} {ok}
16945885e762Sdan
1695c95e01daSdrhdb close
1696c95e01daSdrhforcedelete test.db test.db-wal test.db-journal
1697c95e01daSdrhsqlite3 db test.db
1698c95e01daSdrhsqlite3 db2 test.db
1699c95e01daSdrhdo_test 23.1 {
1700c95e01daSdrh  db eval {
1701c95e01daSdrh    CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1702c95e01daSdrh    CREATE INDEX i1 ON t1(b,c);
1703c95e01daSdrh    CREATE INDEX i2 ON t1(c,d);
1704c95e01daSdrh    CREATE TABLE t2(x INTEGER REFERENCES t1);
1705c95e01daSdrh  }
1706c95e01daSdrh  db2 eval {SELECT name FROM sqlite_master}
1707c95e01daSdrh} {t1 i1 i2 t2}
1708c95e01daSdrhdo_test 23.2 {
1709c95e01daSdrh  db eval {
1710c95e01daSdrh    DROP INDEX i2;
1711c95e01daSdrh    CREATE INDEX i2 ON t1(c,d,b);
1712c95e01daSdrh  }
1713c95e01daSdrh  db2 eval {PRAGMA index_info(i2)}
1714c95e01daSdrh} {0 2 c 1 3 d 2 1 b}
1715c95e01daSdrhdo_test 23.3 {
1716c95e01daSdrh  db eval {
1717c95e01daSdrh    CREATE INDEX i3 ON t1(d,b,c);
1718c95e01daSdrh  }
1719c95e01daSdrh  db2 eval {PRAGMA index_list(t1)}
17203ef26156Sdrh} {0 i3 0 1 i2 0 2 i1 0}
1721c95e01daSdrhdo_test 23.4 {
1722c95e01daSdrh  db eval {
1723c95e01daSdrh    ALTER TABLE t1 ADD COLUMN e;
1724c95e01daSdrh  }
1725c95e01daSdrh  db2 eval {
1726c95e01daSdrh    PRAGMA table_info(t1);
1727c95e01daSdrh  }
1728c95e01daSdrh} {/4 e {} 0 {} 0/}
1729c95e01daSdrhdo_test 23.5 {
1730c95e01daSdrh  db eval {
1731c95e01daSdrh    DROP TABLE t2;
1732c95e01daSdrh    CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1733c95e01daSdrh  }
1734c95e01daSdrh  db2 eval {
1735c95e01daSdrh    PRAGMA foreign_key_list(t2);
1736c95e01daSdrh  }
1737c95e01daSdrh} {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
1738c95e01daSdrh
17391fed5dabSdandatabase_never_corrupt
1740cd61c281Sdrhfinish_test
1741