xref: /sqlite-3.40.0/test/pragma.test (revision 4bb8cb09)
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
54*c228be5bSdrh# Capture the output of a pragma in a TEMP table.
55*c228be5bSdrh#
56*c228be5bSdrhproc capture_pragma {db tabname sql} {
57*c228be5bSdrh  $db eval "DROP TABLE IF EXISTS temp.$tabname"
58*c228be5bSdrh  set once 1
59*c228be5bSdrh  $db eval $sql x {
60*c228be5bSdrh    if {$once} {
61*c228be5bSdrh      set once 0
62*c228be5bSdrh      set ins "INSERT INTO $tabname VALUES"
63*c228be5bSdrh      set crtab "CREATE TEMP TABLE $tabname "
64*c228be5bSdrh      set sep "("
65*c228be5bSdrh      foreach col $x(*) {
66*c228be5bSdrh        append ins ${sep}\$x($col)
67*c228be5bSdrh        append crtab ${sep}\"$col\"
68*c228be5bSdrh        set sep ,
69*c228be5bSdrh      }
70*c228be5bSdrh      append ins )
71*c228be5bSdrh      append crtab )
72*c228be5bSdrh      $db eval $crtab
73*c228be5bSdrh    }
74*c228be5bSdrh    $db eval $ins
75*c228be5bSdrh  }
76*c228be5bSdrh}
77*c228be5bSdrh
78cd61c281Sdrh# Delete the preexisting database to avoid the special setup
79cd61c281Sdrh# that the "all.test" script does.
80cd61c281Sdrh#
81cd61c281Sdrhdb close
82fda06befSmistachkindelete_file test.db test.db-journal
83fda06befSmistachkindelete_file test3.db test3.db-journal
84dddca286Sdrhsqlite3 db test.db; set DB [sqlite3_connection_pointer db]
85cd61c281Sdrh
861e9daa6aSdrh
87c7b4a44cSdanielk1977ifcapable pager_pragmas {
881e9daa6aSdrhset DFLT_CACHE_SZ [db one {PRAGMA default_cache_size}]
891e9daa6aSdrhset TEMP_CACHE_SZ [db one {PRAGMA temp.default_cache_size}]
90cd61c281Sdrhdo_test pragma-1.1 {
91cd61c281Sdrh  execsql {
92cd61c281Sdrh    PRAGMA cache_size;
93cd61c281Sdrh    PRAGMA default_cache_size;
94cd61c281Sdrh    PRAGMA synchronous;
95cd61c281Sdrh  }
961e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
97cd61c281Sdrhdo_test pragma-1.2 {
98cd61c281Sdrh  execsql {
99eb43e5c7Sdrh    PRAGMA synchronous=OFF;
100cd61c281Sdrh    PRAGMA cache_size=1234;
101cd61c281Sdrh    PRAGMA cache_size;
102cd61c281Sdrh    PRAGMA default_cache_size;
103cd61c281Sdrh    PRAGMA synchronous;
104cd61c281Sdrh  }
1051e9daa6aSdrh} [list 1234 $DFLT_CACHE_SZ 0]
106cd61c281Sdrhdo_test pragma-1.3 {
107cd61c281Sdrh  db close
108ef4ac8f9Sdrh  sqlite3 db test.db
109cd61c281Sdrh  execsql {
110cd61c281Sdrh    PRAGMA cache_size;
111cd61c281Sdrh    PRAGMA default_cache_size;
112cd61c281Sdrh    PRAGMA synchronous;
113cd61c281Sdrh  }
1141e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
115cd61c281Sdrhdo_test pragma-1.4 {
116cd61c281Sdrh  execsql {
117cd61c281Sdrh    PRAGMA synchronous=OFF;
118cd61c281Sdrh    PRAGMA cache_size;
119cd61c281Sdrh    PRAGMA default_cache_size;
120cd61c281Sdrh    PRAGMA synchronous;
121cd61c281Sdrh  }
1221e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 0]
123cd61c281Sdrhdo_test pragma-1.5 {
124cd61c281Sdrh  execsql {
125d2cb50b7Sdrh    PRAGMA cache_size=-4321;
126cd61c281Sdrh    PRAGMA cache_size;
127cd61c281Sdrh    PRAGMA default_cache_size;
128cd61c281Sdrh    PRAGMA synchronous;
129cd61c281Sdrh  }
1303b42abb3Sdrh} [list -4321 $DFLT_CACHE_SZ 0]
131cd61c281Sdrhdo_test pragma-1.6 {
132cd61c281Sdrh  execsql {
133cd61c281Sdrh    PRAGMA synchronous=ON;
134cd61c281Sdrh    PRAGMA cache_size;
135cd61c281Sdrh    PRAGMA default_cache_size;
136cd61c281Sdrh    PRAGMA synchronous;
137cd61c281Sdrh  }
1383b42abb3Sdrh} [list -4321 $DFLT_CACHE_SZ 1]
139cd61c281Sdrhdo_test pragma-1.7 {
140cd61c281Sdrh  db close
141ef4ac8f9Sdrh  sqlite3 db test.db
142cd61c281Sdrh  execsql {
143cd61c281Sdrh    PRAGMA cache_size;
144cd61c281Sdrh    PRAGMA default_cache_size;
145cd61c281Sdrh    PRAGMA synchronous;
146cd61c281Sdrh  }
1471e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
148cd61c281Sdrhdo_test pragma-1.8 {
149cd61c281Sdrh  execsql {
150d2cb50b7Sdrh    PRAGMA default_cache_size=-123;
151cd61c281Sdrh    PRAGMA cache_size;
152cd61c281Sdrh    PRAGMA default_cache_size;
153cd61c281Sdrh    PRAGMA synchronous;
154cd61c281Sdrh  }
15591cf71b0Sdanielk1977} {123 123 2}
156802d69a7Sdrhdo_test pragma-1.9.1 {
157cd61c281Sdrh  db close
158dddca286Sdrh  sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
159cd61c281Sdrh  execsql {
160cd61c281Sdrh    PRAGMA cache_size;
161cd61c281Sdrh    PRAGMA default_cache_size;
162cd61c281Sdrh    PRAGMA synchronous;
163cd61c281Sdrh  }
16491cf71b0Sdanielk1977} {123 123 2}
165802d69a7Sdrhifcapable vacuum {
166802d69a7Sdrh  do_test pragma-1.9.2 {
167802d69a7Sdrh    execsql {
168802d69a7Sdrh      VACUUM;
169802d69a7Sdrh      PRAGMA cache_size;
170802d69a7Sdrh      PRAGMA default_cache_size;
171802d69a7Sdrh      PRAGMA synchronous;
172802d69a7Sdrh    }
173802d69a7Sdrh  } {123 123 2}
174802d69a7Sdrh}
17591cf71b0Sdanielk1977do_test pragma-1.10 {
1765a38705eSdrh  execsql {
1774303feedSdrh    PRAGMA synchronous=NORMAL;
1784303feedSdrh    PRAGMA cache_size;
1794303feedSdrh    PRAGMA default_cache_size;
1804303feedSdrh    PRAGMA synchronous;
1814303feedSdrh  }
18291cf71b0Sdanielk1977} {123 123 1}
18391cf71b0Sdanielk1977do_test pragma-1.11 {
1844303feedSdrh  execsql {
1854303feedSdrh    PRAGMA synchronous=FULL;
1864303feedSdrh    PRAGMA cache_size;
1874303feedSdrh    PRAGMA default_cache_size;
1884303feedSdrh    PRAGMA synchronous;
1894303feedSdrh  }
19091cf71b0Sdanielk1977} {123 123 2}
19191cf71b0Sdanielk1977do_test pragma-1.12 {
1924303feedSdrh  db close
193dddca286Sdrh  sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
1944303feedSdrh  execsql {
1954303feedSdrh    PRAGMA cache_size;
1964303feedSdrh    PRAGMA default_cache_size;
1974303feedSdrh    PRAGMA synchronous;
1984303feedSdrh  }
19991cf71b0Sdanielk1977} {123 123 2}
20091cf71b0Sdanielk1977
2015260f7e9Sdrh# Make sure the pragma handler understands numeric values in addition
2025260f7e9Sdrh# to keywords like "off" and "full".
2035260f7e9Sdrh#
2045260f7e9Sdrhdo_test pragma-1.13 {
2055260f7e9Sdrh  execsql {
2065260f7e9Sdrh    PRAGMA synchronous=0;
2075260f7e9Sdrh    PRAGMA synchronous;
2085260f7e9Sdrh  }
2095260f7e9Sdrh} {0}
2105260f7e9Sdrhdo_test pragma-1.14 {
2115260f7e9Sdrh  execsql {
2125260f7e9Sdrh    PRAGMA synchronous=2;
2135260f7e9Sdrh    PRAGMA synchronous;
2145260f7e9Sdrh  }
2155260f7e9Sdrh} {2}
216c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
2175260f7e9Sdrh
2185260f7e9Sdrh# Test turning "flag" pragmas on and off.
2195260f7e9Sdrh#
2206338c762Sdanielk1977ifcapable debug {
2216338c762Sdanielk1977  # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
2226338c762Sdanielk1977  #
2235260f7e9Sdrh  do_test pragma-1.15 {
2245260f7e9Sdrh    execsql {
2255260f7e9Sdrh      PRAGMA vdbe_listing=YES;
2265260f7e9Sdrh      PRAGMA vdbe_listing;
2275260f7e9Sdrh    }
2285260f7e9Sdrh  } {1}
2295260f7e9Sdrh  do_test pragma-1.16 {
2305260f7e9Sdrh    execsql {
2315260f7e9Sdrh      PRAGMA vdbe_listing=NO;
2325260f7e9Sdrh      PRAGMA vdbe_listing;
2335260f7e9Sdrh    }
2345260f7e9Sdrh  } {0}
2356338c762Sdanielk1977}
2366338c762Sdanielk1977
2375260f7e9Sdrhdo_test pragma-1.17 {
2385260f7e9Sdrh  execsql {
2395260f7e9Sdrh    PRAGMA parser_trace=ON;
2405260f7e9Sdrh    PRAGMA parser_trace=OFF;
2415260f7e9Sdrh  }
2425260f7e9Sdrh} {}
2435260f7e9Sdrhdo_test pragma-1.18 {
2445260f7e9Sdrh  execsql {
2455260f7e9Sdrh    PRAGMA bogus = -1234;  -- Parsing of negative values
2465260f7e9Sdrh  }
2475260f7e9Sdrh} {}
2485260f7e9Sdrh
24991cf71b0Sdanielk1977# Test modifying the safety_level of an attached database.
2505a8f9374Sdanielk1977ifcapable pager_pragmas&&attach {
25191cf71b0Sdanielk1977  do_test pragma-2.1 {
252fda06befSmistachkin    forcedelete test2.db
253fda06befSmistachkin    forcedelete test2.db-journal
2544303feedSdrh    execsql {
25591cf71b0Sdanielk1977      ATTACH 'test2.db' AS aux;
2564303feedSdrh    }
25791cf71b0Sdanielk1977  } {}
25891cf71b0Sdanielk1977  do_test pragma-2.2 {
2594303feedSdrh    execsql {
26091cf71b0Sdanielk1977      pragma aux.synchronous;
2614303feedSdrh    }
26291cf71b0Sdanielk1977  } {2}
26391cf71b0Sdanielk1977  do_test pragma-2.3 {
26491cf71b0Sdanielk1977    execsql {
26591cf71b0Sdanielk1977      pragma aux.synchronous = OFF;
26691cf71b0Sdanielk1977      pragma aux.synchronous;
26791cf71b0Sdanielk1977      pragma synchronous;
26891cf71b0Sdanielk1977    }
26991cf71b0Sdanielk1977  } {0 2}
27091cf71b0Sdanielk1977  do_test pragma-2.4 {
27191cf71b0Sdanielk1977    execsql {
27291cf71b0Sdanielk1977      pragma aux.synchronous = ON;
27391cf71b0Sdanielk1977      pragma synchronous;
27491cf71b0Sdanielk1977      pragma aux.synchronous;
27591cf71b0Sdanielk1977    }
27691cf71b0Sdanielk1977  } {2 1}
277c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
2784303feedSdrh
279ed717fe3Sdrh# Construct a corrupted index and make sure the integrity_check
280ed717fe3Sdrh# pragma finds it.
281ed717fe3Sdrh#
28225d6543dSdrh# These tests won't work if the database is encrypted
28325d6543dSdrh#
284ed717fe3Sdrhdo_test pragma-3.1 {
2851e9daa6aSdrh  db close
286fda06befSmistachkin  forcedelete test.db test.db-journal
2871e9daa6aSdrh  sqlite3 db test.db
288ed717fe3Sdrh  execsql {
2891e9daa6aSdrh    PRAGMA auto_vacuum=OFF;
290ed717fe3Sdrh    BEGIN;
291ed717fe3Sdrh    CREATE TABLE t2(a,b,c);
292ed717fe3Sdrh    CREATE INDEX i2 ON t2(a);
293ed717fe3Sdrh    INSERT INTO t2 VALUES(11,2,3);
294ed717fe3Sdrh    INSERT INTO t2 VALUES(22,3,4);
295ed717fe3Sdrh    COMMIT;
296ed717fe3Sdrh    SELECT rowid, * from t2;
297ed717fe3Sdrh  }
298ed717fe3Sdrh} {1 11 2 3 2 22 3 4}
2995a8f9374Sdanielk1977ifcapable attach {
30040e016e4Sdrh  if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
301ed717fe3Sdrh    do_test pragma-3.2 {
302bb8a279eSdrh      db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
303bb8a279eSdrh      set pgsz [db eval {PRAGMA page_size}]
304bb8a279eSdrh      # overwrite the header on the rootpage of the index in order to
305bb8a279eSdrh      # make the index appear to be empty.
306bb8a279eSdrh      #
307bb8a279eSdrh      set offset [expr {$pgsz*($rootpage-1)}]
308bb8a279eSdrh      hexio_write test.db $offset 0a00000000040000000000
309bb8a279eSdrh      db close
310bb8a279eSdrh      sqlite3 db test.db
311ed717fe3Sdrh      execsql {PRAGMA integrity_check}
3126fbe41acSdrh    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
31391cf71b0Sdanielk1977    do_test pragma-3.3 {
3141dcdbc06Sdrh      execsql {PRAGMA integrity_check=1}
3156fbe41acSdrh    } {{row 1 missing from index i2}}
3161dcdbc06Sdrh    do_test pragma-3.4 {
31791cf71b0Sdanielk1977      execsql {
3181dcdbc06Sdrh        ATTACH DATABASE 'test.db' AS t2;
3191dcdbc06Sdrh        PRAGMA integrity_check
32091cf71b0Sdanielk1977      }
3216fbe41acSdrh    } {{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}}
3221dcdbc06Sdrh    do_test pragma-3.5 {
3231dcdbc06Sdrh      execsql {
324bb8a279eSdrh        PRAGMA integrity_check=4
3251dcdbc06Sdrh      }
3266fbe41acSdrh    } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2}}
3271dcdbc06Sdrh    do_test pragma-3.6 {
3281dcdbc06Sdrh      execsql {
3291dcdbc06Sdrh        PRAGMA integrity_check=xyz
3301dcdbc06Sdrh      }
3316fbe41acSdrh    } {{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}}
3321dcdbc06Sdrh    do_test pragma-3.7 {
3331dcdbc06Sdrh      execsql {
3341dcdbc06Sdrh        PRAGMA integrity_check=0
3351dcdbc06Sdrh      }
3366fbe41acSdrh    } {{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}}
3371dcdbc06Sdrh
3381dcdbc06Sdrh    # Add additional corruption by appending unused pages to the end of
3391dcdbc06Sdrh    # the database file testerr.db
3401dcdbc06Sdrh    #
3411dcdbc06Sdrh    do_test pragma-3.8 {
3421dcdbc06Sdrh      execsql {DETACH t2}
343fda06befSmistachkin      forcedelete testerr.db testerr.db-journal
344e3d559adSdrh      set out [open testerr.db w]
345e3d559adSdrh      fconfigure $out -translation binary
346e3d559adSdrh      set in [open test.db r]
347e3d559adSdrh      fconfigure $in -translation binary
3481dcdbc06Sdrh      puts -nonewline $out [read $in]
3491dcdbc06Sdrh      seek $in 0
3501dcdbc06Sdrh      puts -nonewline $out [read $in]
3511dcdbc06Sdrh      close $in
3521dcdbc06Sdrh      close $out
353dd3cd977Sdrh      hexio_write testerr.db 28 00000000
3541dcdbc06Sdrh      execsql {REINDEX t2}
3551dcdbc06Sdrh      execsql {PRAGMA integrity_check}
3561dcdbc06Sdrh    } {ok}
35741c58b78Sdanielk1977    do_test pragma-3.8.1 {
35841c58b78Sdanielk1977      execsql {PRAGMA quick_check}
35941c58b78Sdanielk1977    } {ok}
3605d16a9a6Sdrh    do_test pragma-3.8.2 {
3615d16a9a6Sdrh      execsql {PRAGMA QUICK_CHECK}
3625d16a9a6Sdrh    } {ok}
3631dcdbc06Sdrh    do_test pragma-3.9 {
3641dcdbc06Sdrh      execsql {
3651dcdbc06Sdrh        ATTACH 'testerr.db' AS t2;
3661dcdbc06Sdrh        PRAGMA integrity_check
3671dcdbc06Sdrh      }
3681dcdbc06Sdrh    } {{*** in database t2 ***
3691dcdbc06SdrhPage 4 is never used
3701dcdbc06SdrhPage 5 is never used
3716fbe41acSdrhPage 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
3721dcdbc06Sdrh    do_test pragma-3.10 {
3731dcdbc06Sdrh      execsql {
3741dcdbc06Sdrh        PRAGMA integrity_check=1
3751dcdbc06Sdrh      }
3761dcdbc06Sdrh    } {{*** in database t2 ***
3771dcdbc06SdrhPage 4 is never used}}
3781dcdbc06Sdrh    do_test pragma-3.11 {
3791dcdbc06Sdrh      execsql {
3801dcdbc06Sdrh        PRAGMA integrity_check=5
3811dcdbc06Sdrh      }
3821dcdbc06Sdrh    } {{*** in database t2 ***
3831dcdbc06SdrhPage 4 is never used
3841dcdbc06SdrhPage 5 is never used
3856fbe41acSdrhPage 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2}}
3861dcdbc06Sdrh    do_test pragma-3.12 {
3871dcdbc06Sdrh      execsql {
3881dcdbc06Sdrh        PRAGMA integrity_check=4
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}}
3941dcdbc06Sdrh    do_test pragma-3.13 {
3951dcdbc06Sdrh      execsql {
3961dcdbc06Sdrh        PRAGMA integrity_check=3
3971dcdbc06Sdrh      }
3981dcdbc06Sdrh    } {{*** in database t2 ***
3991dcdbc06SdrhPage 4 is never used
4001dcdbc06SdrhPage 5 is never used
4011dcdbc06SdrhPage 6 is never used}}
4021dcdbc06Sdrh    do_test pragma-3.14 {
4031dcdbc06Sdrh      execsql {
4041dcdbc06Sdrh        PRAGMA integrity_check(2)
4051dcdbc06Sdrh      }
4061dcdbc06Sdrh    } {{*** in database t2 ***
4071dcdbc06SdrhPage 4 is never used
4081dcdbc06SdrhPage 5 is never used}}
4091dcdbc06Sdrh    do_test pragma-3.15 {
4101dcdbc06Sdrh      execsql {
4111dcdbc06Sdrh        ATTACH 'testerr.db' AS t3;
4121dcdbc06Sdrh        PRAGMA integrity_check
4131dcdbc06Sdrh      }
4141dcdbc06Sdrh    } {{*** in database t2 ***
4151dcdbc06SdrhPage 4 is never used
4161dcdbc06SdrhPage 5 is never used
4176fbe41acSdrhPage 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 ***
4181dcdbc06SdrhPage 4 is never used
4191dcdbc06SdrhPage 5 is never used
4206fbe41acSdrhPage 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
4211dcdbc06Sdrh    do_test pragma-3.16 {
4221dcdbc06Sdrh      execsql {
423bb8a279eSdrh        PRAGMA integrity_check(10)
4241dcdbc06Sdrh      }
4251dcdbc06Sdrh    } {{*** in database t2 ***
4261dcdbc06SdrhPage 4 is never used
4271dcdbc06SdrhPage 5 is never used
4286fbe41acSdrhPage 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 ***
4291dcdbc06SdrhPage 4 is never used
4301dcdbc06SdrhPage 5 is never used
4316fbe41acSdrhPage 6 is never used} {row 1 missing from index i2}}
4321dcdbc06Sdrh    do_test pragma-3.17 {
4331dcdbc06Sdrh      execsql {
434bb8a279eSdrh        PRAGMA integrity_check=8
4351dcdbc06Sdrh      }
4361dcdbc06Sdrh    } {{*** in database t2 ***
4371dcdbc06SdrhPage 4 is never used
4381dcdbc06SdrhPage 5 is never used
4396fbe41acSdrhPage 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 ***
4401dcdbc06SdrhPage 4 is never used
4411dcdbc06SdrhPage 5 is never used}}
4421dcdbc06Sdrh    do_test pragma-3.18 {
4431dcdbc06Sdrh      execsql {
4441dcdbc06Sdrh        PRAGMA integrity_check=4
4451dcdbc06Sdrh      }
4461dcdbc06Sdrh    } {{*** in database t2 ***
4471dcdbc06SdrhPage 4 is never used
4481dcdbc06SdrhPage 5 is never used
4496fbe41acSdrhPage 6 is never used} {row 1 missing from index i2}}
4501dcdbc06Sdrh  }
451d2cb50b7Sdrh  do_test pragma-3.19 {
452d2cb50b7Sdrh    catch {db close}
453fda06befSmistachkin    forcedelete test.db test.db-journal
454d2cb50b7Sdrh    sqlite3 db test.db
455d2cb50b7Sdrh    db eval {PRAGMA integrity_check}
456d2cb50b7Sdrh  } {ok}
4575a8f9374Sdanielk1977}
458cefc87fcSdrh
459cefc87fcSdrh# Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
460cefc87fcSdrh# constraint violations.
461cefc87fcSdrh#
462cefc87fcSdrhdo_execsql_test pragma-3.20 {
463cefc87fcSdrh  CREATE TABLE t1(a,b);
464cefc87fcSdrh  CREATE INDEX t1a ON t1(a);
465cefc87fcSdrh  INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
466cefc87fcSdrh  PRAGMA writable_schema=ON;
467cefc87fcSdrh  UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
468cefc87fcSdrh   WHERE name='t1a';
469cefc87fcSdrh  UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
470cefc87fcSdrh   WHERE name='t1';
471cefc87fcSdrh  PRAGMA writable_schema=OFF;
472cefc87fcSdrh  ALTER TABLE t1 RENAME TO t1x;
473cefc87fcSdrh  PRAGMA integrity_check;
474cefc87fcSdrh} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
475cefc87fcSdrhdo_execsql_test pragma-3.21 {
476cefc87fcSdrh  PRAGMA integrity_check(3);
477cefc87fcSdrh} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
478cefc87fcSdrhdo_execsql_test pragma-3.22 {
479cefc87fcSdrh  PRAGMA integrity_check(2);
480cefc87fcSdrh} {{non-unique entry in index t1a} {NULL value in t1x.a}}
4817efa426eSdrhdo_execsql_test pragma-3.23 {
482cefc87fcSdrh  PRAGMA integrity_check(1);
483cefc87fcSdrh} {{non-unique entry in index t1a}}
48491cf71b0Sdanielk1977
4857efa426eSdrh# PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
4867efa426eSdrh# interface) used to leave index cursors in an inconsistent state
4877efa426eSdrh# which could result in an assertion fault in sqlite3BtreeKey()
4887efa426eSdrh# called from saveCursorPosition() if content is removed from the
4897efa426eSdrh# index while the integrity_check is still running.  This test verifies
4907efa426eSdrh# that problem has been fixed.
4917efa426eSdrh#
4927efa426eSdrhdo_test pragma-3.30 {
4937efa426eSdrh  db close
4947efa426eSdrh  delete_file test.db
4957efa426eSdrh  sqlite3 db test.db
4967efa426eSdrh  db eval {
4977efa426eSdrh    CREATE TABLE t1(a,b,c);
4987efa426eSdrh    WITH RECURSIVE
4997efa426eSdrh      c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
5007efa426eSdrh    INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
5017efa426eSdrh    CREATE INDEX t1a ON t1(a);
5027efa426eSdrh    CREATE INDEX t1bc ON t1(b,c);
5037efa426eSdrh  }
5047efa426eSdrh  db eval {PRAGMA integrity_check} {
5057efa426eSdrh     db eval {DELETE FROM t1}
5067efa426eSdrh  }
5077efa426eSdrh} {}
5087efa426eSdrh
50991cf71b0Sdanielk1977# Test modifying the cache_size of an attached database.
5105a8f9374Sdanielk1977ifcapable pager_pragmas&&attach {
51191cf71b0Sdanielk1977do_test pragma-4.1 {
51291cf71b0Sdanielk1977  execsql {
5131e9daa6aSdrh    ATTACH 'test2.db' AS aux;
51491cf71b0Sdanielk1977    pragma aux.cache_size;
51591cf71b0Sdanielk1977    pragma aux.default_cache_size;
51691cf71b0Sdanielk1977  }
5171e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
5181bdd9b57Sdrhdo_test pragma-4.2 {
5191bdd9b57Sdrh  execsql {
52091cf71b0Sdanielk1977    pragma aux.cache_size = 50;
52191cf71b0Sdanielk1977    pragma aux.cache_size;
52291cf71b0Sdanielk1977    pragma aux.default_cache_size;
5231bdd9b57Sdrh  }
5241e9daa6aSdrh} [list 50 $DFLT_CACHE_SZ]
5251bdd9b57Sdrhdo_test pragma-4.3 {
5261bdd9b57Sdrh  execsql {
52791cf71b0Sdanielk1977    pragma aux.default_cache_size = 456;
52891cf71b0Sdanielk1977    pragma aux.cache_size;
52991cf71b0Sdanielk1977    pragma aux.default_cache_size;
5301bdd9b57Sdrh  }
53191cf71b0Sdanielk1977} {456 456}
5321bdd9b57Sdrhdo_test pragma-4.4 {
5331bdd9b57Sdrh  execsql {
53491cf71b0Sdanielk1977    pragma cache_size;
53591cf71b0Sdanielk1977    pragma default_cache_size;
5361bdd9b57Sdrh  }
5371e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
5381bdd9b57Sdrhdo_test pragma-4.5 {
5391bdd9b57Sdrh  execsql {
54091cf71b0Sdanielk1977    DETACH aux;
54191cf71b0Sdanielk1977    ATTACH 'test3.db' AS aux;
54291cf71b0Sdanielk1977    pragma aux.cache_size;
54391cf71b0Sdanielk1977    pragma aux.default_cache_size;
5441bdd9b57Sdrh  }
5451e9daa6aSdrh} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
5461bdd9b57Sdrhdo_test pragma-4.6 {
5471bdd9b57Sdrh  execsql {
54891cf71b0Sdanielk1977    DETACH aux;
54991cf71b0Sdanielk1977    ATTACH 'test2.db' AS aux;
55091cf71b0Sdanielk1977    pragma aux.cache_size;
55191cf71b0Sdanielk1977    pragma aux.default_cache_size;
5521bdd9b57Sdrh  }
55391cf71b0Sdanielk1977} {456 456}
554c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
5551bdd9b57Sdrh
55691cf71b0Sdanielk1977# Test that modifying the sync-level in the middle of a transaction is
55791cf71b0Sdanielk1977# disallowed.
558c7b4a44cSdanielk1977ifcapable pager_pragmas {
55991cf71b0Sdanielk1977do_test pragma-5.0 {
5601bdd9b57Sdrh  execsql {
56191cf71b0Sdanielk1977    pragma synchronous;
5621bdd9b57Sdrh  }
56391cf71b0Sdanielk1977} {2}
56491cf71b0Sdanielk1977do_test pragma-5.1 {
5651bdd9b57Sdrh  catchsql {
5661bdd9b57Sdrh    BEGIN;
56791cf71b0Sdanielk1977    pragma synchronous = OFF;
5681bdd9b57Sdrh  }
56991cf71b0Sdanielk1977} {1 {Safety level may not be changed inside a transaction}}
57091cf71b0Sdanielk1977do_test pragma-5.2 {
57191cf71b0Sdanielk1977  execsql {
57291cf71b0Sdanielk1977    pragma synchronous;
57391cf71b0Sdanielk1977  }
57491cf71b0Sdanielk1977} {2}
5755260f7e9Sdrhcatchsql {COMMIT;}
576c7b4a44cSdanielk1977} ;# ifcapable pager_pragmas
5775260f7e9Sdrh
5785260f7e9Sdrh# Test schema-query pragmas
5795260f7e9Sdrh#
58027188fb5Sdanielk1977ifcapable schema_pragmas {
5815a8f9374Sdanielk1977ifcapable tempdb&&attach {
5825260f7e9Sdrh  do_test pragma-6.1 {
583956bc92cSdrh    set res {}
58440e016e4Sdrh    execsql {SELECT * FROM sqlite_temp_master}
5855260f7e9Sdrh    foreach {idx name file} [execsql {pragma database_list}] {
5865260f7e9Sdrh      lappend res $idx $name
5875260f7e9Sdrh    }
5885260f7e9Sdrh    set res
589a71aa001Sdrh  } {0 main 1 temp 2 aux}
59053c0f748Sdanielk1977}
5915260f7e9Sdrhdo_test pragma-6.2 {
5925260f7e9Sdrh  execsql {
593d2cb50b7Sdrh    CREATE TABLE t2(a,b,c);
5945260f7e9Sdrh    pragma table_info(t2)
5955260f7e9Sdrh  }
596bfa8b102Sdrh} {0 a {} 0 {} 0 1 b {} 0 {} 0 2 c {} 0 {} 0}
597d2cb50b7Sdrhdo_test pragma-6.2.1 {
598d2cb50b7Sdrh  execsql {
599d2cb50b7Sdrh    pragma table_info;
600d2cb50b7Sdrh  }
601d2cb50b7Sdrh} {}
602736c7d4bSdrhdb nullvalue <<NULL>>
603417ec638Sdrhdo_test pragma-6.2.2 {
604417ec638Sdrh  execsql {
605736c7d4bSdrh    CREATE TABLE t5(
606736c7d4bSdrh      a TEXT DEFAULT CURRENT_TIMESTAMP,
607736c7d4bSdrh      b DEFAULT (5+3),
608736c7d4bSdrh      c TEXT,
609736c7d4bSdrh      d INTEGER DEFAULT NULL,
610384b7fe2Sdrh      e TEXT DEFAULT '',
611384b7fe2Sdrh      UNIQUE(b,c,d),
612384b7fe2Sdrh      PRIMARY KEY(e,b,c)
613736c7d4bSdrh    );
614417ec638Sdrh    PRAGMA table_info(t5);
615417ec638Sdrh  }
616384b7fe2Sdrh} {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}
617736c7d4bSdrhdb nullvalue {}
618384b7fe2Sdrhdo_test pragma-6.2.3 {
619384b7fe2Sdrh  execsql {
620384b7fe2Sdrh    CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
621384b7fe2Sdrh    pragma table_info(t2_3)
622384b7fe2Sdrh  }
623384b7fe2Sdrh} {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
6246bf89570Sdrhifcapable {foreignkey} {
625d2cb50b7Sdrh  do_test pragma-6.3.1 {
6265260f7e9Sdrh    execsql {
6275260f7e9Sdrh      CREATE TABLE t3(a int references t2(b), b UNIQUE);
6285260f7e9Sdrh      pragma foreign_key_list(t3);
6295260f7e9Sdrh    }
6301da40a38Sdan  } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
631d2cb50b7Sdrh  do_test pragma-6.3.2 {
632d2cb50b7Sdrh    execsql {
633d2cb50b7Sdrh      pragma foreign_key_list;
634d2cb50b7Sdrh    }
635d2cb50b7Sdrh  } {}
636d2cb50b7Sdrh  do_test pragma-6.3.3 {
637d2cb50b7Sdrh    execsql {
638d2cb50b7Sdrh      pragma foreign_key_list(t3_bogus);
639d2cb50b7Sdrh    }
640d2cb50b7Sdrh  } {}
641d2cb50b7Sdrh  do_test pragma-6.3.4 {
642d2cb50b7Sdrh    execsql {
643d2cb50b7Sdrh      pragma foreign_key_list(t5);
644d2cb50b7Sdrh    }
645d2cb50b7Sdrh  } {}
6465260f7e9Sdrh  do_test pragma-6.4 {
647*c228be5bSdrh    capture_pragma db out {
6485260f7e9Sdrh      pragma index_list(t3);
6495260f7e9Sdrh    }
650*c228be5bSdrh    db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
6513ef26156Sdrh  } {0 sqlite_autoindex_t3_1 1}
6526bf89570Sdrh}
6536bf89570Sdrhifcapable {!foreignkey} {
6546bf89570Sdrh  execsql {CREATE TABLE t3(a,b UNIQUE)}
6556bf89570Sdrh}
656d2cb50b7Sdrhdo_test pragma-6.5.1 {
6575260f7e9Sdrh  execsql {
6585260f7e9Sdrh    CREATE INDEX t3i1 ON t3(a,b);
659*c228be5bSdrh  }
660*c228be5bSdrh  capture_pragma db out {
6615260f7e9Sdrh    pragma index_info(t3i1);
6625260f7e9Sdrh  }
663*c228be5bSdrh  db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
6645260f7e9Sdrh} {0 0 a 1 1 b}
665d2cb50b7Sdrhdo_test pragma-6.5.2 {
666d2cb50b7Sdrh  execsql {
667d2cb50b7Sdrh    pragma index_info(t3i1_bogus);
668d2cb50b7Sdrh  }
669d2cb50b7Sdrh} {}
670260d8a6aSdanielk1977
671260d8a6aSdanielk1977ifcapable tempdb {
672260d8a6aSdanielk1977  # Test for ticket #3320. When a temp table of the same name exists, make
673260d8a6aSdanielk1977  # sure the schema of the main table can still be queried using
674260d8a6aSdanielk1977  # "pragma table_info":
675260d8a6aSdanielk1977  do_test pragma-6.6.1 {
676260d8a6aSdanielk1977    execsql {
677260d8a6aSdanielk1977      CREATE TABLE trial(col_main);
678260d8a6aSdanielk1977      CREATE TEMP TABLE trial(col_temp);
679260d8a6aSdanielk1977    }
680260d8a6aSdanielk1977  } {}
681260d8a6aSdanielk1977  do_test pragma-6.6.2 {
682260d8a6aSdanielk1977    execsql {
683260d8a6aSdanielk1977      PRAGMA table_info(trial);
684260d8a6aSdanielk1977    }
685260d8a6aSdanielk1977  } {0 col_temp {} 0 {} 0}
686260d8a6aSdanielk1977  do_test pragma-6.6.3 {
687260d8a6aSdanielk1977    execsql {
688260d8a6aSdanielk1977      PRAGMA temp.table_info(trial);
689260d8a6aSdanielk1977    }
690260d8a6aSdanielk1977  } {0 col_temp {} 0 {} 0}
691260d8a6aSdanielk1977  do_test pragma-6.6.4 {
692260d8a6aSdanielk1977    execsql {
693260d8a6aSdanielk1977      PRAGMA main.table_info(trial);
694260d8a6aSdanielk1977    }
695260d8a6aSdanielk1977  } {0 col_main {} 0 {} 0}
696260d8a6aSdanielk1977}
697f96a3778Sdanielk1977
698f96a3778Sdanielk1977do_test pragma-6.7 {
699f96a3778Sdanielk1977  execsql {
700f96a3778Sdanielk1977    CREATE TABLE test_table(
701f96a3778Sdanielk1977      one INT NOT NULL DEFAULT -1,
702f96a3778Sdanielk1977      two text,
703f96a3778Sdanielk1977      three VARCHAR(45, 65) DEFAULT 'abcde',
704f96a3778Sdanielk1977      four REAL DEFAULT X'abcdef',
705f96a3778Sdanielk1977      five DEFAULT CURRENT_TIME
706f96a3778Sdanielk1977    );
707f96a3778Sdanielk1977  }
708*c228be5bSdrh  capture_pragma db out {PRAGMA table_info(test_table)}
709*c228be5bSdrh  db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
710*c228be5bSdrh            ORDER BY cid}
711f96a3778Sdanielk1977} [concat \
712f96a3778Sdanielk1977  {0 one INT 1 -1 0} \
713f96a3778Sdanielk1977  {1 two text 0 {} 0} \
714f96a3778Sdanielk1977  {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
715f96a3778Sdanielk1977  {3 four REAL 0 X'abcdef' 0} \
716f96a3778Sdanielk1977  {4 five {} 0 CURRENT_TIME 0} \
717f96a3778Sdanielk1977]
71827188fb5Sdanielk1977} ;# ifcapable schema_pragmas
7195260f7e9Sdrh# Miscellaneous tests
7205260f7e9Sdrh#
72127188fb5Sdanielk1977ifcapable schema_pragmas {
722d2cb50b7Sdrhdo_test pragma-7.1.1 {
7235260f7e9Sdrh  # Make sure a pragma knows to read the schema if it needs to
7245260f7e9Sdrh  db close
7255260f7e9Sdrh  sqlite3 db test.db
726*c228be5bSdrh  capture_pragma db out "PRAGMA index_list(t3)"
727*c228be5bSdrh  db eval {SELECT name, "origin" FROM out ORDER BY name DESC}
728*c228be5bSdrh} {t3i1 c sqlite_autoindex_t3_1 u}
729d2cb50b7Sdrhdo_test pragma-7.1.2 {
730d2cb50b7Sdrh  execsql {
731d2cb50b7Sdrh    pragma index_list(t3_bogus);
732d2cb50b7Sdrh  }
733d2cb50b7Sdrh} {}
73427188fb5Sdanielk1977} ;# ifcapable schema_pragmas
7356c62608fSdrhifcapable {utf16} {
736cb354603Sdan  if {[permutation] == ""} {
7375260f7e9Sdrh    do_test pragma-7.2 {
7385260f7e9Sdrh      db close
7395260f7e9Sdrh      sqlite3 db test.db
7405260f7e9Sdrh      catchsql {
7415260f7e9Sdrh        pragma encoding=bogus;
7425260f7e9Sdrh      }
7435260f7e9Sdrh    } {1 {unsupported encoding: bogus}}
7446c62608fSdrh  }
745cb354603Sdan}
74653c0f748Sdanielk1977ifcapable tempdb {
7475260f7e9Sdrh  do_test pragma-7.3 {
7485260f7e9Sdrh    db close
7495260f7e9Sdrh    sqlite3 db test.db
7505260f7e9Sdrh    execsql {
7515260f7e9Sdrh      pragma lock_status;
7525260f7e9Sdrh    }
7535260f7e9Sdrh  } {main unlocked temp closed}
75453c0f748Sdanielk1977} else {
75553c0f748Sdanielk1977  do_test pragma-7.3 {
75653c0f748Sdanielk1977    db close
75753c0f748Sdanielk1977    sqlite3 db test.db
75853c0f748Sdanielk1977    execsql {
75953c0f748Sdanielk1977      pragma lock_status;
76053c0f748Sdanielk1977    }
76153c0f748Sdanielk1977  } {main unlocked}
76253c0f748Sdanielk1977}
7635260f7e9Sdrh
7645260f7e9Sdrh
765dae2495bSdanielk1977#----------------------------------------------------------------------
766b92b70bbSdanielk1977# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
767b92b70bbSdanielk1977# user_version" statements.
768dae2495bSdanielk1977#
769b92b70bbSdanielk1977# pragma-8.1: PRAGMA schema_version
770b92b70bbSdanielk1977# pragma-8.2: PRAGMA user_version
771dae2495bSdanielk1977#
772dae2495bSdanielk1977
77311cf9fbcSdanielk1977ifcapable schema_version {
77411cf9fbcSdanielk1977
775b92b70bbSdanielk1977# First check that we can set the schema version and then retrieve the
776dae2495bSdanielk1977# same value.
777dae2495bSdanielk1977do_test pragma-8.1.1 {
778dae2495bSdanielk1977  execsql {
779b92b70bbSdanielk1977    PRAGMA schema_version = 105;
780dae2495bSdanielk1977  }
781dae2495bSdanielk1977} {}
782dae2495bSdanielk1977do_test pragma-8.1.2 {
78325403650Sdrh  execsql2 {
784b92b70bbSdanielk1977    PRAGMA schema_version;
785dae2495bSdanielk1977  }
78625403650Sdrh} {schema_version 105}
787dae2495bSdanielk1977do_test pragma-8.1.3 {
788dae2495bSdanielk1977  execsql {
789b92b70bbSdanielk1977    PRAGMA schema_version = 106;
790dae2495bSdanielk1977  }
791dae2495bSdanielk1977} {}
792dae2495bSdanielk1977do_test pragma-8.1.4 {
793dae2495bSdanielk1977  execsql {
794b92b70bbSdanielk1977    PRAGMA schema_version;
795dae2495bSdanielk1977  }
796dae2495bSdanielk1977} 106
797dae2495bSdanielk1977
798b92b70bbSdanielk1977# Check that creating a table modifies the schema-version (this is really
799b92b70bbSdanielk1977# to verify that the value being read is in fact the schema version).
800dae2495bSdanielk1977do_test pragma-8.1.5 {
801dae2495bSdanielk1977  execsql {
802dae2495bSdanielk1977    CREATE TABLE t4(a, b, c);
803dae2495bSdanielk1977    INSERT INTO t4 VALUES(1, 2, 3);
804dae2495bSdanielk1977    SELECT * FROM t4;
805dae2495bSdanielk1977  }
806dae2495bSdanielk1977} {1 2 3}
807dae2495bSdanielk1977do_test pragma-8.1.6 {
808dae2495bSdanielk1977  execsql {
809b92b70bbSdanielk1977    PRAGMA schema_version;
810dae2495bSdanielk1977  }
811dae2495bSdanielk1977} 107
812dae2495bSdanielk1977
813dae2495bSdanielk1977# Now open a second connection to the database. Ensure that changing the
814b92b70bbSdanielk1977# schema-version using the first connection forces the second connection
815dae2495bSdanielk1977# to reload the schema. This has to be done using the C-API test functions,
816dae2495bSdanielk1977# because the TCL API accounts for SCHEMA_ERROR and retries the query.
817dae2495bSdanielk1977do_test pragma-8.1.7 {
818dddca286Sdrh  sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
819dae2495bSdanielk1977  execsql {
820dae2495bSdanielk1977    SELECT * FROM t4;
821dae2495bSdanielk1977  } db2
822dae2495bSdanielk1977} {1 2 3}
823dae2495bSdanielk1977do_test pragma-8.1.8 {
824dae2495bSdanielk1977  execsql {
825b92b70bbSdanielk1977    PRAGMA schema_version = 108;
826dae2495bSdanielk1977  }
827dae2495bSdanielk1977} {}
828dae2495bSdanielk1977do_test pragma-8.1.9 {
829dae2495bSdanielk1977  set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
830dae2495bSdanielk1977  sqlite3_step $::STMT
831dae2495bSdanielk1977} SQLITE_ERROR
832dae2495bSdanielk1977do_test pragma-8.1.10 {
833dae2495bSdanielk1977  sqlite3_finalize $::STMT
834dae2495bSdanielk1977} SQLITE_SCHEMA
835dae2495bSdanielk1977
836b92b70bbSdanielk1977# Make sure the schema-version can be manipulated in an attached database.
837fda06befSmistachkinforcedelete test2.db
838fda06befSmistachkinforcedelete test2.db-journal
8395a8f9374Sdanielk1977ifcapable attach {
840dae2495bSdanielk1977  do_test pragma-8.1.11 {
841dae2495bSdanielk1977    execsql {
842dae2495bSdanielk1977      ATTACH 'test2.db' AS aux;
843dae2495bSdanielk1977      CREATE TABLE aux.t1(a, b, c);
844b92b70bbSdanielk1977      PRAGMA aux.schema_version = 205;
845dae2495bSdanielk1977    }
846dae2495bSdanielk1977  } {}
847dae2495bSdanielk1977  do_test pragma-8.1.12 {
848dae2495bSdanielk1977    execsql {
849b92b70bbSdanielk1977      PRAGMA aux.schema_version;
850dae2495bSdanielk1977    }
851dae2495bSdanielk1977  } 205
8525a8f9374Sdanielk1977}
853dae2495bSdanielk1977do_test pragma-8.1.13 {
854dae2495bSdanielk1977  execsql {
855b92b70bbSdanielk1977    PRAGMA schema_version;
856dae2495bSdanielk1977  }
857dae2495bSdanielk1977} 108
858dae2495bSdanielk1977
859b92b70bbSdanielk1977# And check that modifying the schema-version in an attached database
860dae2495bSdanielk1977# forces the second connection to reload the schema.
8615a8f9374Sdanielk1977ifcapable attach {
862dae2495bSdanielk1977  do_test pragma-8.1.14 {
863dddca286Sdrh    sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
864dae2495bSdanielk1977    execsql {
865dae2495bSdanielk1977      ATTACH 'test2.db' AS aux;
866dae2495bSdanielk1977      SELECT * FROM aux.t1;
867dae2495bSdanielk1977    } db2
868dae2495bSdanielk1977  } {}
869dae2495bSdanielk1977  do_test pragma-8.1.15 {
870dae2495bSdanielk1977    execsql {
871b92b70bbSdanielk1977      PRAGMA aux.schema_version = 206;
872dae2495bSdanielk1977    }
873dae2495bSdanielk1977  } {}
874dae2495bSdanielk1977  do_test pragma-8.1.16 {
875dae2495bSdanielk1977    set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
876dae2495bSdanielk1977    sqlite3_step $::STMT
877dae2495bSdanielk1977  } SQLITE_ERROR
878dae2495bSdanielk1977  do_test pragma-8.1.17 {
879dae2495bSdanielk1977    sqlite3_finalize $::STMT
880dae2495bSdanielk1977  } SQLITE_SCHEMA
881dae2495bSdanielk1977  do_test pragma-8.1.18 {
882dae2495bSdanielk1977    db2 close
883dae2495bSdanielk1977  } {}
8845a8f9374Sdanielk1977}
885dae2495bSdanielk1977
886b92b70bbSdanielk1977# Now test that the user-version can be read and written (and that we aren't
887b92b70bbSdanielk1977# accidentally manipulating the schema-version instead).
888dae2495bSdanielk1977do_test pragma-8.2.1 {
88925403650Sdrh  execsql2 {
890b92b70bbSdanielk1977    PRAGMA user_version;
891dae2495bSdanielk1977  }
89225403650Sdrh} {user_version 0}
893dae2495bSdanielk1977do_test pragma-8.2.2 {
894dae2495bSdanielk1977  execsql {
895b92b70bbSdanielk1977    PRAGMA user_version = 2;
896dae2495bSdanielk1977  }
897dae2495bSdanielk1977} {}
898802d69a7Sdrhdo_test pragma-8.2.3.1 {
89925403650Sdrh  execsql2 {
900b92b70bbSdanielk1977    PRAGMA user_version;
901dae2495bSdanielk1977  }
90225403650Sdrh} {user_version 2}
903802d69a7Sdrhdo_test pragma-8.2.3.2 {
904802d69a7Sdrh  db close
905802d69a7Sdrh  sqlite3 db test.db
906802d69a7Sdrh  execsql {
907802d69a7Sdrh    PRAGMA user_version;
908802d69a7Sdrh  }
909802d69a7Sdrh} {2}
910802d69a7Sdrhdo_test pragma-8.2.4.1 {
911dae2495bSdanielk1977  execsql {
912b92b70bbSdanielk1977    PRAGMA schema_version;
913dae2495bSdanielk1977  }
914dae2495bSdanielk1977} {108}
915802d69a7Sdrhifcapable vacuum {
916802d69a7Sdrh  do_test pragma-8.2.4.2 {
917802d69a7Sdrh    execsql {
918802d69a7Sdrh      VACUUM;
919802d69a7Sdrh      PRAGMA user_version;
920802d69a7Sdrh    }
921802d69a7Sdrh  } {2}
922802d69a7Sdrh  do_test pragma-8.2.4.3 {
923802d69a7Sdrh    execsql {
924802d69a7Sdrh      PRAGMA schema_version;
925802d69a7Sdrh    }
926802d69a7Sdrh  } {109}
927802d69a7Sdrh}
9285a8f9374Sdanielk1977
9295a8f9374Sdanielk1977ifcapable attach {
930802d69a7Sdrh  db eval {ATTACH 'test2.db' AS aux}
931dae2495bSdanielk1977
932b92b70bbSdanielk1977  # Check that the user-version in the auxilary database can be manipulated (
933dae2495bSdanielk1977  # and that we aren't accidentally manipulating the same in the main db).
934dae2495bSdanielk1977  do_test pragma-8.2.5 {
935dae2495bSdanielk1977    execsql {
936b92b70bbSdanielk1977      PRAGMA aux.user_version;
937dae2495bSdanielk1977    }
938dae2495bSdanielk1977  } {0}
939dae2495bSdanielk1977  do_test pragma-8.2.6 {
940dae2495bSdanielk1977    execsql {
941b92b70bbSdanielk1977      PRAGMA aux.user_version = 3;
942dae2495bSdanielk1977    }
943dae2495bSdanielk1977  } {}
944dae2495bSdanielk1977  do_test pragma-8.2.7 {
945dae2495bSdanielk1977    execsql {
946b92b70bbSdanielk1977      PRAGMA aux.user_version;
947dae2495bSdanielk1977    }
948dae2495bSdanielk1977  } {3}
949dae2495bSdanielk1977  do_test pragma-8.2.8 {
950dae2495bSdanielk1977    execsql {
951b92b70bbSdanielk1977      PRAGMA main.user_version;
952dae2495bSdanielk1977    }
953dae2495bSdanielk1977  } {2}
954dae2495bSdanielk1977
955b92b70bbSdanielk1977  # Now check that a ROLLBACK resets the user-version if it has been modified
956dae2495bSdanielk1977  # within a transaction.
957dae2495bSdanielk1977  do_test pragma-8.2.9 {
958dae2495bSdanielk1977    execsql {
959dae2495bSdanielk1977      BEGIN;
960b92b70bbSdanielk1977      PRAGMA aux.user_version = 10;
961b92b70bbSdanielk1977      PRAGMA user_version = 11;
962dae2495bSdanielk1977    }
963dae2495bSdanielk1977  } {}
964dae2495bSdanielk1977  do_test pragma-8.2.10 {
965dae2495bSdanielk1977    execsql {
966b92b70bbSdanielk1977      PRAGMA aux.user_version;
967dae2495bSdanielk1977    }
968dae2495bSdanielk1977  } {10}
969dae2495bSdanielk1977  do_test pragma-8.2.11 {
970dae2495bSdanielk1977    execsql {
971b92b70bbSdanielk1977      PRAGMA main.user_version;
972dae2495bSdanielk1977    }
973dae2495bSdanielk1977  } {11}
974dae2495bSdanielk1977  do_test pragma-8.2.12 {
975dae2495bSdanielk1977    execsql {
976dae2495bSdanielk1977      ROLLBACK;
977b92b70bbSdanielk1977      PRAGMA aux.user_version;
978dae2495bSdanielk1977    }
979dae2495bSdanielk1977  } {3}
980dae2495bSdanielk1977  do_test pragma-8.2.13 {
981dae2495bSdanielk1977    execsql {
982b92b70bbSdanielk1977      PRAGMA main.user_version;
983dae2495bSdanielk1977    }
984dae2495bSdanielk1977  } {2}
9855a8f9374Sdanielk1977}
986dae2495bSdanielk1977
987b92b70bbSdanielk1977# Try a negative value for the user-version
988dae2495bSdanielk1977do_test pragma-8.2.14 {
989dae2495bSdanielk1977  execsql {
990b92b70bbSdanielk1977    PRAGMA user_version = -450;
991dae2495bSdanielk1977  }
992dae2495bSdanielk1977} {}
993dae2495bSdanielk1977do_test pragma-8.2.15 {
994dae2495bSdanielk1977  execsql {
995b92b70bbSdanielk1977    PRAGMA user_version;
996dae2495bSdanielk1977  }
997dae2495bSdanielk1977} {-450}
998d9c847dcSdanielk1977} ; # ifcapable schema_version
999d9c847dcSdanielk1977
1000d19744f3Sdrh# Check to see if TEMP_STORE is memory or disk.  Return strings
1001d19744f3Sdrh# "memory" or "disk" as appropriate.
1002d19744f3Sdrh#
1003d19744f3Sdrhproc check_temp_store {} {
1004d19744f3Sdrh  db eval {CREATE TEMP TABLE IF NOT EXISTS a(b)}
1005d19744f3Sdrh  db eval {PRAGMA database_list} {
1006d19744f3Sdrh    if {$name=="temp"} {
100717b90b53Sdanielk1977      set bt [btree_from_db db 1]
100817b90b53Sdanielk1977      if {[btree_ismemdb $bt]} {
1009d19744f3Sdrh        return "memory"
1010d19744f3Sdrh      }
101117b90b53Sdanielk1977      return "disk"
1012d19744f3Sdrh    }
1013d19744f3Sdrh  }
1014d19744f3Sdrh  return "unknown"
1015d19744f3Sdrh}
1016d19744f3Sdrh
10174ee09b4bSdrh# Application_ID
10184ee09b4bSdrh#
10194ee09b4bSdrhdo_test pragma-8.3.1 {
10204ee09b4bSdrh  execsql {
10214ee09b4bSdrh    PRAGMA application_id;
10224ee09b4bSdrh  }
10234ee09b4bSdrh} {0}
10244ee09b4bSdrhdo_test pragma-8.3.2 {
10254ee09b4bSdrh  execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
10264ee09b4bSdrh} {12345}
10279a09a3caStpoindex
10289a09a3caStpoindex# Test temp_store and temp_store_directory pragmas
10299a09a3caStpoindex#
1030268283bcSdrhifcapable pager_pragmas {
10319a09a3caStpoindexdo_test pragma-9.1 {
10329a09a3caStpoindex  db close
10339a09a3caStpoindex  sqlite3 db test.db
10349a09a3caStpoindex  execsql {
10359a09a3caStpoindex    PRAGMA temp_store;
10369a09a3caStpoindex  }
10379a09a3caStpoindex} {0}
1038d19744f3Sdrhif {$TEMP_STORE<=1} {
1039d19744f3Sdrh  do_test pragma-9.1.1 {
1040d19744f3Sdrh    check_temp_store
1041d19744f3Sdrh  } {disk}
1042d19744f3Sdrh} else {
1043d19744f3Sdrh  do_test pragma-9.1.1 {
1044d19744f3Sdrh    check_temp_store
1045d19744f3Sdrh  } {memory}
1046d19744f3Sdrh}
1047d19744f3Sdrh
10489a09a3caStpoindexdo_test pragma-9.2 {
1049d19744f3Sdrh  db close
1050d19744f3Sdrh  sqlite3 db test.db
10519a09a3caStpoindex  execsql {
10529a09a3caStpoindex    PRAGMA temp_store=file;
10539a09a3caStpoindex    PRAGMA temp_store;
10549a09a3caStpoindex  }
10559a09a3caStpoindex} {1}
1056d19744f3Sdrhif {$TEMP_STORE==3} {
1057d19744f3Sdrh  # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1058d19744f3Sdrh  do_test pragma-9.2.1 {
1059d19744f3Sdrh    check_temp_store
1060d19744f3Sdrh  } {memory}
1061d19744f3Sdrh} else {
1062d19744f3Sdrh  do_test pragma-9.2.1 {
1063d19744f3Sdrh    check_temp_store
1064d19744f3Sdrh  } {disk}
1065d19744f3Sdrh}
1066d19744f3Sdrh
10679a09a3caStpoindexdo_test pragma-9.3 {
1068d19744f3Sdrh  db close
1069d19744f3Sdrh  sqlite3 db test.db
10709a09a3caStpoindex  execsql {
10719a09a3caStpoindex    PRAGMA temp_store=memory;
10729a09a3caStpoindex    PRAGMA temp_store;
10739a09a3caStpoindex  }
10749a09a3caStpoindex} {2}
1075d19744f3Sdrhif {$TEMP_STORE==0} {
1076d19744f3Sdrh  # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1077d19744f3Sdrh  do_test pragma-9.3.1 {
1078d19744f3Sdrh    check_temp_store
1079d19744f3Sdrh  } {disk}
1080d19744f3Sdrh} else {
1081d19744f3Sdrh  do_test pragma-9.3.1 {
1082d19744f3Sdrh    check_temp_store
1083d19744f3Sdrh  } {memory}
1084d19744f3Sdrh}
1085d19744f3Sdrh
10869a09a3caStpoindexdo_test pragma-9.4 {
10879a09a3caStpoindex  execsql {
10889a09a3caStpoindex    PRAGMA temp_store_directory;
10899a09a3caStpoindex  }
10909a09a3caStpoindex} {}
109178f82d1eSdrhifcapable wsd {
10929a09a3caStpoindex  do_test pragma-9.5 {
1093f8a78464Smistachkin    set pwd [string map {' ''} [file nativename [get_pwd]]]
1094268283bcSdrh    execsql "
1095268283bcSdrh      PRAGMA temp_store_directory='$pwd';
10969a09a3caStpoindex    "
10979a09a3caStpoindex  } {}
10989a09a3caStpoindex  do_test pragma-9.6 {
10999a09a3caStpoindex    execsql {
11009a09a3caStpoindex      PRAGMA temp_store_directory;
11019a09a3caStpoindex    }
1102f8a78464Smistachkin  } [list [file nativename [get_pwd]]]
11039a09a3caStpoindex  do_test pragma-9.7 {
1104268283bcSdrh    catchsql {
11059a09a3caStpoindex      PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
11069a09a3caStpoindex    }
1107268283bcSdrh  } {1 {not a writable directory}}
11089a09a3caStpoindex  do_test pragma-9.8 {
11099a09a3caStpoindex    execsql {
11109a09a3caStpoindex      PRAGMA temp_store_directory='';
11119a09a3caStpoindex    }
11129a09a3caStpoindex  } {}
11131e9daa6aSdrh  if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
111453c0f748Sdanielk1977    ifcapable tempdb {
11159a09a3caStpoindex      do_test pragma-9.9 {
11169a09a3caStpoindex        execsql {
11179a09a3caStpoindex          PRAGMA temp_store_directory;
11189a09a3caStpoindex          PRAGMA temp_store=FILE;
11199a09a3caStpoindex          CREATE TEMP TABLE temp_store_directory_test(a integer);
11209a09a3caStpoindex          INSERT INTO temp_store_directory_test values (2);
11219a09a3caStpoindex          SELECT * FROM temp_store_directory_test;
11229a09a3caStpoindex        }
11239a09a3caStpoindex      } {2}
11249a09a3caStpoindex      do_test pragma-9.10 {
1125268283bcSdrh        catchsql "
1126268283bcSdrh          PRAGMA temp_store_directory='$pwd';
11279a09a3caStpoindex          SELECT * FROM temp_store_directory_test;
11289a09a3caStpoindex        "
1129268283bcSdrh      } {1 {no such table: temp_store_directory_test}}
11301e9daa6aSdrh    }
11311e9daa6aSdrh  }
113278f82d1eSdrh}
113395b289b6Sdanielk1977do_test pragma-9.11 {
113495b289b6Sdanielk1977  execsql {
113595b289b6Sdanielk1977    PRAGMA temp_store = 0;
113695b289b6Sdanielk1977    PRAGMA temp_store;
113795b289b6Sdanielk1977  }
113895b289b6Sdanielk1977} {0}
113995b289b6Sdanielk1977do_test pragma-9.12 {
114095b289b6Sdanielk1977  execsql {
114195b289b6Sdanielk1977    PRAGMA temp_store = 1;
114295b289b6Sdanielk1977    PRAGMA temp_store;
114395b289b6Sdanielk1977  }
114495b289b6Sdanielk1977} {1}
114595b289b6Sdanielk1977do_test pragma-9.13 {
114695b289b6Sdanielk1977  execsql {
114795b289b6Sdanielk1977    PRAGMA temp_store = 2;
114895b289b6Sdanielk1977    PRAGMA temp_store;
114995b289b6Sdanielk1977  }
115095b289b6Sdanielk1977} {2}
115195b289b6Sdanielk1977do_test pragma-9.14 {
115295b289b6Sdanielk1977  execsql {
115395b289b6Sdanielk1977    PRAGMA temp_store = 3;
115495b289b6Sdanielk1977    PRAGMA temp_store;
115595b289b6Sdanielk1977  }
115695b289b6Sdanielk1977} {0}
115795b289b6Sdanielk1977do_test pragma-9.15 {
115895b289b6Sdanielk1977  catchsql {
115995b289b6Sdanielk1977    BEGIN EXCLUSIVE;
116095b289b6Sdanielk1977    CREATE TEMP TABLE temp_table(t);
116195b289b6Sdanielk1977    INSERT INTO temp_table VALUES('valuable data');
116295b289b6Sdanielk1977    PRAGMA temp_store = 1;
116395b289b6Sdanielk1977  }
116495b289b6Sdanielk1977} {1 {temporary storage cannot be changed from within a transaction}}
116595b289b6Sdanielk1977do_test pragma-9.16 {
116695b289b6Sdanielk1977  execsql {
116795b289b6Sdanielk1977    SELECT * FROM temp_table;
116895b289b6Sdanielk1977    COMMIT;
116995b289b6Sdanielk1977  }
117095b289b6Sdanielk1977} {{valuable data}}
1171983e2309Sdanielk1977
1172983e2309Sdanielk1977do_test pragma-9.17 {
1173983e2309Sdanielk1977  execsql {
1174983e2309Sdanielk1977    INSERT INTO temp_table VALUES('valuable data II');
1175983e2309Sdanielk1977    SELECT * FROM temp_table;
1176983e2309Sdanielk1977  }
1177983e2309Sdanielk1977} {{valuable data} {valuable data II}}
1178983e2309Sdanielk1977
1179983e2309Sdanielk1977do_test pragma-9.18 {
1180983e2309Sdanielk1977  set rc [catch {
1181983e2309Sdanielk1977    db eval {SELECT t FROM temp_table} {
1182983e2309Sdanielk1977      execsql {pragma temp_store = 1}
1183983e2309Sdanielk1977    }
1184983e2309Sdanielk1977  } msg]
1185983e2309Sdanielk1977  list $rc $msg
1186983e2309Sdanielk1977} {1 {temporary storage cannot be changed from within a transaction}}
1187983e2309Sdanielk1977
1188268283bcSdrh} ;# ifcapable pager_pragmas
11899a09a3caStpoindex
1190cc6bd383Sdanielk1977ifcapable trigger {
1191cc6bd383Sdanielk1977
1192cc6bd383Sdanielk1977do_test pragma-10.0 {
1193cc6bd383Sdanielk1977  catchsql {
1194cc6bd383Sdanielk1977    DROP TABLE main.t1;
1195cc6bd383Sdanielk1977  }
1196cc6bd383Sdanielk1977  execsql {
1197cc6bd383Sdanielk1977    PRAGMA count_changes = 1;
1198cc6bd383Sdanielk1977
1199cc6bd383Sdanielk1977    CREATE TABLE t1(a PRIMARY KEY);
1200cc6bd383Sdanielk1977    CREATE TABLE t1_mirror(a);
1201cc6bd383Sdanielk1977    CREATE TABLE t1_mirror2(a);
1202cc6bd383Sdanielk1977    CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1203cc6bd383Sdanielk1977      INSERT INTO t1_mirror VALUES(new.a);
1204cc6bd383Sdanielk1977    END;
1205cc6bd383Sdanielk1977    CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1206cc6bd383Sdanielk1977      INSERT INTO t1_mirror2 VALUES(new.a);
1207cc6bd383Sdanielk1977    END;
1208cc6bd383Sdanielk1977    CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1209cc6bd383Sdanielk1977      UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1210cc6bd383Sdanielk1977    END;
1211cc6bd383Sdanielk1977    CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1212cc6bd383Sdanielk1977      UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1213cc6bd383Sdanielk1977    END;
1214cc6bd383Sdanielk1977    CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1215cc6bd383Sdanielk1977      DELETE FROM t1_mirror WHERE a = old.a;
1216cc6bd383Sdanielk1977    END;
1217cc6bd383Sdanielk1977    CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1218cc6bd383Sdanielk1977      DELETE FROM t1_mirror2 WHERE a = old.a;
1219cc6bd383Sdanielk1977    END;
1220cc6bd383Sdanielk1977  }
1221cc6bd383Sdanielk1977} {}
1222cc6bd383Sdanielk1977
1223cc6bd383Sdanielk1977do_test pragma-10.1 {
1224cc6bd383Sdanielk1977  execsql {
1225cc6bd383Sdanielk1977    INSERT INTO t1 VALUES(randstr(10,10));
1226cc6bd383Sdanielk1977  }
1227cc6bd383Sdanielk1977} {1}
1228cc6bd383Sdanielk1977do_test pragma-10.2 {
1229cc6bd383Sdanielk1977  execsql {
1230cc6bd383Sdanielk1977    UPDATE t1 SET a = randstr(10,10);
1231cc6bd383Sdanielk1977  }
1232cc6bd383Sdanielk1977} {1}
1233cc6bd383Sdanielk1977do_test pragma-10.3 {
1234cc6bd383Sdanielk1977  execsql {
1235cc6bd383Sdanielk1977    DELETE FROM t1;
1236cc6bd383Sdanielk1977  }
1237cc6bd383Sdanielk1977} {1}
1238cc6bd383Sdanielk1977
1239cc6bd383Sdanielk1977} ;# ifcapable trigger
1240cc6bd383Sdanielk1977
124148af65aeSdanielk1977ifcapable schema_pragmas {
124248af65aeSdanielk1977  do_test pragma-11.1 {
124348af65aeSdanielk1977    execsql2 {
124448af65aeSdanielk1977      pragma collation_list;
124548af65aeSdanielk1977    }
12462d823315Sdrh  } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
124748af65aeSdanielk1977  do_test pragma-11.2 {
124848af65aeSdanielk1977    db collate New_Collation blah...
124948af65aeSdanielk1977    execsql {
125048af65aeSdanielk1977      pragma collation_list;
125148af65aeSdanielk1977    }
12522d823315Sdrh  } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
125348af65aeSdanielk1977}
125448af65aeSdanielk1977
1255ddfb2f03Sdanielk1977ifcapable schema_pragmas&&tempdb {
1256ddfb2f03Sdanielk1977  do_test pragma-12.1 {
1257ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1258ddfb2f03Sdanielk1977    execsql {
1259ddfb2f03Sdanielk1977      PRAGMA temp.table_info('abc');
1260ddfb2f03Sdanielk1977    } db2
1261ddfb2f03Sdanielk1977  } {}
1262ddfb2f03Sdanielk1977  db2 close
1263ddfb2f03Sdanielk1977
1264ddfb2f03Sdanielk1977  do_test pragma-12.2 {
1265ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1266ddfb2f03Sdanielk1977    execsql {
1267ddfb2f03Sdanielk1977      PRAGMA temp.default_cache_size = 200;
1268ddfb2f03Sdanielk1977      PRAGMA temp.default_cache_size;
1269ddfb2f03Sdanielk1977    } db2
1270ddfb2f03Sdanielk1977  } {200}
1271ddfb2f03Sdanielk1977  db2 close
1272ddfb2f03Sdanielk1977
1273ddfb2f03Sdanielk1977  do_test pragma-12.3 {
1274ddfb2f03Sdanielk1977    sqlite3 db2 test.db
1275ddfb2f03Sdanielk1977    execsql {
1276ddfb2f03Sdanielk1977      PRAGMA temp.cache_size = 400;
1277ddfb2f03Sdanielk1977      PRAGMA temp.cache_size;
1278ddfb2f03Sdanielk1977    } db2
1279ddfb2f03Sdanielk1977  } {400}
1280ddfb2f03Sdanielk1977  db2 close
1281ddfb2f03Sdanielk1977}
1282ddfb2f03Sdanielk1977
12834b2688abSdanielk1977ifcapable bloblit {
12844b2688abSdanielk1977
128505a82983Sdrhdo_test pragma-13.1 {
128605a82983Sdrh  execsql {
128705a82983Sdrh    DROP TABLE IF EXISTS t4;
128805a82983Sdrh    PRAGMA vdbe_trace=on;
128905a82983Sdrh    PRAGMA vdbe_listing=on;
129005a82983Sdrh    PRAGMA sql_trace=on;
129105a82983Sdrh    CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
129205a82983Sdrh    INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
129305a82983Sdrh    INSERT INTO t4(b) VALUES(randstr(30,30));
129405a82983Sdrh    INSERT INTO t4(b) VALUES(1.23456);
129505a82983Sdrh    INSERT INTO t4(b) VALUES(NULL);
129605a82983Sdrh    INSERT INTO t4(b) VALUES(0);
129705a82983Sdrh    INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
129805a82983Sdrh    SELECT * FROM t4;
129905a82983Sdrh  }
130005a82983Sdrh  execsql {
130105a82983Sdrh    PRAGMA vdbe_trace=off;
130205a82983Sdrh    PRAGMA vdbe_listing=off;
130305a82983Sdrh    PRAGMA sql_trace=off;
130405a82983Sdrh  }
130505a82983Sdrh} {}
130605a82983Sdrh
13074b2688abSdanielk1977} ;# ifcapable bloblit
13084b2688abSdanielk1977
130959a93791Sdanielk1977ifcapable pager_pragmas {
131059a93791Sdanielk1977  db close
1311fda06befSmistachkin  forcedelete test.db
131259a93791Sdanielk1977  sqlite3 db test.db
131359a93791Sdanielk1977
131459a93791Sdanielk1977  do_test pragma-14.1 {
131559a93791Sdanielk1977    execsql { pragma auto_vacuum = 0 }
131659a93791Sdanielk1977    execsql { pragma page_count }
131759a93791Sdanielk1977  } {0}
131859a93791Sdanielk1977
131959a93791Sdanielk1977  do_test pragma-14.2 {
132059a93791Sdanielk1977    execsql {
132159a93791Sdanielk1977      CREATE TABLE abc(a, b, c);
132259a93791Sdanielk1977      PRAGMA page_count;
132359a93791Sdanielk1977    }
132459a93791Sdanielk1977  } {2}
13255d16a9a6Sdrh  do_test pragma-14.2uc {
13265d16a9a6Sdrh    execsql {pragma PAGE_COUNT}
13275d16a9a6Sdrh  } {2}
132859a93791Sdanielk1977
132959a93791Sdanielk1977  do_test pragma-14.3 {
133059a93791Sdanielk1977    execsql {
133159a93791Sdanielk1977      BEGIN;
133259a93791Sdanielk1977      CREATE TABLE def(a, b, c);
133359a93791Sdanielk1977      PRAGMA page_count;
133459a93791Sdanielk1977    }
133559a93791Sdanielk1977  } {3}
13365d16a9a6Sdrh  do_test pragma-14.3uc {
13375d16a9a6Sdrh    execsql {pragma PAGE_COUNT}
13385d16a9a6Sdrh  } {3}
133959a93791Sdanielk1977
134059a93791Sdanielk1977  do_test pragma-14.4 {
134159a93791Sdanielk1977    set page_size [db one {pragma page_size}]
134259a93791Sdanielk1977    expr [file size test.db] / $page_size
134359a93791Sdanielk1977  } {2}
134459a93791Sdanielk1977
134559a93791Sdanielk1977  do_test pragma-14.5 {
134659a93791Sdanielk1977    execsql {
134759a93791Sdanielk1977      ROLLBACK;
134859a93791Sdanielk1977      PRAGMA page_count;
134959a93791Sdanielk1977    }
135059a93791Sdanielk1977  } {2}
135159a93791Sdanielk1977
135259a93791Sdanielk1977  do_test pragma-14.6 {
1353fda06befSmistachkin    forcedelete test2.db
135459a93791Sdanielk1977    sqlite3 db2 test2.db
135559a93791Sdanielk1977    execsql {
135659a93791Sdanielk1977      PRAGMA auto_vacuum = 0;
135759a93791Sdanielk1977      CREATE TABLE t1(a, b, c);
135859a93791Sdanielk1977      CREATE TABLE t2(a, b, c);
135959a93791Sdanielk1977      CREATE TABLE t3(a, b, c);
136059a93791Sdanielk1977      CREATE TABLE t4(a, b, c);
136159a93791Sdanielk1977    } db2
136259a93791Sdanielk1977    db2 close
136359a93791Sdanielk1977    execsql {
136459a93791Sdanielk1977      ATTACH 'test2.db' AS aux;
136559a93791Sdanielk1977      PRAGMA aux.page_count;
136659a93791Sdanielk1977    }
136759a93791Sdanielk1977  } {5}
13685d16a9a6Sdrh  do_test pragma-14.6uc {
13695d16a9a6Sdrh    execsql {pragma AUX.PAGE_COUNT}
13705d16a9a6Sdrh  } {5}
137159a93791Sdanielk1977}
137259a93791Sdanielk1977
13738cf6c554Sdanielk1977# Test that the value set using the cache_size pragma is not reset when the
13748cf6c554Sdanielk1977# schema is reloaded.
13758cf6c554Sdanielk1977#
13768cf6c554Sdanielk1977ifcapable pager_pragmas {
13778cf6c554Sdanielk1977  db close
13788cf6c554Sdanielk1977  sqlite3 db test.db
13798cf6c554Sdanielk1977  do_test pragma-15.1 {
13808cf6c554Sdanielk1977    execsql {
13818cf6c554Sdanielk1977      PRAGMA cache_size=59;
13828cf6c554Sdanielk1977      PRAGMA cache_size;
13838cf6c554Sdanielk1977    }
13848cf6c554Sdanielk1977  } {59}
13858cf6c554Sdanielk1977  do_test pragma-15.2 {
13868cf6c554Sdanielk1977    sqlite3 db2 test.db
13878cf6c554Sdanielk1977    execsql {
13888cf6c554Sdanielk1977      CREATE TABLE newtable(a, b, c);
13898cf6c554Sdanielk1977    } db2
13908cf6c554Sdanielk1977    db2 close
13918cf6c554Sdanielk1977  } {}
13928cf6c554Sdanielk1977  do_test pragma-15.3 {
13938cf6c554Sdanielk1977    # Evaluating this statement will cause the schema to be reloaded (because
13948cf6c554Sdanielk1977    # the schema was changed by another connection in pragma-15.2). At one
13958cf6c554Sdanielk1977    # point there was a bug that reset the cache_size to its default value
13968cf6c554Sdanielk1977    # when this happened.
13978cf6c554Sdanielk1977    execsql { SELECT * FROM sqlite_master }
13988cf6c554Sdanielk1977    execsql { PRAGMA cache_size }
13998cf6c554Sdanielk1977  } {59}
14008cf6c554Sdanielk1977}
14018cf6c554Sdanielk1977
14025558a8a6Sdanielk1977# Reset the sqlite3_temp_directory variable for the next run of tests:
14035558a8a6Sdanielk1977sqlite3 dbX :memory:
14045558a8a6Sdanielk1977dbX eval {PRAGMA temp_store_directory = ""}
14055558a8a6Sdanielk1977dbX close
14065558a8a6Sdanielk1977
1407838cce43Sdanielk1977ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
1408aebf413dSaswift  set sqlite_hostid_num 1
1409aebf413dSaswift
1410aebf413dSaswift  set using_proxy 0
1411aebf413dSaswift  foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1412aebf413dSaswift    set using_proxy $value
1413aebf413dSaswift  }
1414aebf413dSaswift
1415aebf413dSaswift  # Test the lock_proxy_file pragmas.
1416aebf413dSaswift  #
1417aebf413dSaswift  db close
1418aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1419aebf413dSaswift
1420aebf413dSaswift  sqlite3 db test.db
1421aebf413dSaswift  do_test pragma-16.1 {
1422aebf413dSaswift    execsql {
1423aebf413dSaswift      PRAGMA lock_proxy_file="mylittleproxy";
1424aebf413dSaswift      select * from sqlite_master;
1425aebf413dSaswift    }
1426aebf413dSaswift    execsql {
1427aebf413dSaswift      PRAGMA lock_proxy_file;
1428aebf413dSaswift    }
1429aebf413dSaswift  } {mylittleproxy}
1430aebf413dSaswift
1431aebf413dSaswift  do_test pragma-16.2 {
1432aebf413dSaswift    sqlite3 db2 test.db
1433aebf413dSaswift    execsql {
1434aebf413dSaswift      PRAGMA lock_proxy_file="mylittleproxy";
1435aebf413dSaswift    } db2
1436aebf413dSaswift  } {}
1437aebf413dSaswift
1438aebf413dSaswift  db2 close
1439aebf413dSaswift  do_test pragma-16.2.1 {
1440aebf413dSaswift    sqlite3 db2 test.db
1441aebf413dSaswift    execsql {
1442aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1443aebf413dSaswift      select * from sqlite_master;
1444aebf413dSaswift    } db2
1445aebf413dSaswift    execsql {
1446aebf413dSaswift      PRAGMA lock_proxy_file;
1447aebf413dSaswift    } db2
1448aebf413dSaswift  } {mylittleproxy}
1449aebf413dSaswift
1450aebf413dSaswift  db2 close
1451aebf413dSaswift  do_test pragma-16.3 {
1452aebf413dSaswift    sqlite3 db2 test.db
1453aebf413dSaswift    execsql {
1454aebf413dSaswift      PRAGMA lock_proxy_file="myotherproxy";
1455aebf413dSaswift    } db2
1456aebf413dSaswift    catchsql {
1457aebf413dSaswift      select * from sqlite_master;
1458aebf413dSaswift    } db2
1459aebf413dSaswift  } {1 {database is locked}}
1460aebf413dSaswift
1461aebf413dSaswift  do_test pragma-16.4 {
1462aebf413dSaswift    db2 close
1463aebf413dSaswift    db close
1464aebf413dSaswift    sqlite3 db2 test.db
1465aebf413dSaswift    execsql {
1466aebf413dSaswift      PRAGMA lock_proxy_file="myoriginalproxy";
1467aebf413dSaswift      PRAGMA lock_proxy_file="myotherproxy";
1468aebf413dSaswift      PRAGMA lock_proxy_file;
1469aebf413dSaswift    } db2
1470aebf413dSaswift  } {myotherproxy}
1471aebf413dSaswift
1472aebf413dSaswift  db2 close
1473aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1474aebf413dSaswift  do_test pragma-16.5 {
1475aebf413dSaswift    sqlite3 db2 test.db
1476aebf413dSaswift    execsql {
1477aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1478aebf413dSaswift      PRAGMA lock_proxy_file;
1479aebf413dSaswift    } db2
1480aebf413dSaswift  } {myotherproxy}
1481aebf413dSaswift
1482aebf413dSaswift  do_test pragma-16.6 {
1483aebf413dSaswift    db2 close
1484aebf413dSaswift    sqlite3 db2 test2.db
1485aebf413dSaswift    set lockpath [execsql {
1486aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1487aebf413dSaswift      PRAGMA lock_proxy_file;
1488aebf413dSaswift    } db2]
1489aebf413dSaswift    string match "*test2.db:auto:" $lockpath
1490aebf413dSaswift  } {1}
1491aebf413dSaswift
1492aebf413dSaswift  set sqlite_hostid_num 2
1493aebf413dSaswift  do_test pragma-16.7 {
149414d14607Sdan    list [catch {
1495aebf413dSaswift      sqlite3 db test2.db
1496aebf413dSaswift      execsql {
1497aebf413dSaswift        PRAGMA lock_proxy_file=":auto:";
1498aebf413dSaswift        select * from sqlite_master;
1499aebf413dSaswift      }
150014d14607Sdan    } msg] $msg
1501aebf413dSaswift  } {1 {database is locked}}
1502aebf413dSaswift  db close
1503aebf413dSaswift
1504aebf413dSaswift  do_test pragma-16.8 {
150514d14607Sdan    list [catch {
1506aebf413dSaswift      sqlite3 db test2.db
150714d14607Sdan      execsql { select * from sqlite_master }
150814d14607Sdan    } msg] $msg
1509aebf413dSaswift  } {1 {database is locked}}
1510aebf413dSaswift
1511aebf413dSaswift  db2 close
1512aebf413dSaswift  do_test pragma-16.8.1 {
1513aebf413dSaswift    execsql {
1514aebf413dSaswift      PRAGMA lock_proxy_file="yetanotherproxy";
1515aebf413dSaswift      PRAGMA lock_proxy_file;
1516aebf413dSaswift    }
1517aebf413dSaswift  } {yetanotherproxy}
1518aebf413dSaswift  do_test pragma-16.8.2 {
1519aebf413dSaswift    execsql {
1520aebf413dSaswift      create table mine(x);
1521aebf413dSaswift    }
1522aebf413dSaswift  } {}
1523aebf413dSaswift
1524aebf413dSaswift  db close
1525aebf413dSaswift  do_test pragma-16.9 {
1526aebf413dSaswift    sqlite3 db proxytest.db
1527aebf413dSaswift    set lockpath2 [execsql {
1528aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
1529aebf413dSaswift      PRAGMA lock_proxy_file;
1530aebf413dSaswift    } db]
1531aebf413dSaswift    string match "*proxytest.db:auto:" $lockpath2
1532aebf413dSaswift  } {1}
1533aebf413dSaswift
1534aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1535aebf413dSaswift  set sqlite_hostid_num 0
1536aebf413dSaswift}
1537d2cb50b7Sdrh
1538d2cb50b7Sdrh# Parsing of auto_vacuum settings.
1539d2cb50b7Sdrh#
1540d2cb50b7Sdrhforeach {autovac_setting val} {
1541d2cb50b7Sdrh  0 0
1542d2cb50b7Sdrh  1 1
1543d2cb50b7Sdrh  2 2
1544d2cb50b7Sdrh  3 0
1545d2cb50b7Sdrh  -1 0
1546d2cb50b7Sdrh  none 0
1547d2cb50b7Sdrh  NONE 0
1548d2cb50b7Sdrh  NoNe 0
1549d2cb50b7Sdrh  full 1
1550d2cb50b7Sdrh  FULL 1
1551d2cb50b7Sdrh  incremental 2
1552d2cb50b7Sdrh  INCREMENTAL 2
1553d2cb50b7Sdrh  -1234 0
1554d2cb50b7Sdrh  1234 0
1555d2cb50b7Sdrh} {
1556d2cb50b7Sdrh  do_test pragma-17.1.$autovac_setting {
1557d2cb50b7Sdrh    catch {db close}
1558d2cb50b7Sdrh    sqlite3 db :memory:
1559d2cb50b7Sdrh    execsql "
1560d2cb50b7Sdrh      PRAGMA auto_vacuum=$::autovac_setting;
1561d2cb50b7Sdrh      PRAGMA auto_vacuum;
1562d2cb50b7Sdrh    "
1563d2cb50b7Sdrh  } $val
1564d2cb50b7Sdrh}
1565d2cb50b7Sdrh
1566d2cb50b7Sdrh# Parsing of temp_store settings.
1567d2cb50b7Sdrh#
1568d2cb50b7Sdrhforeach {temp_setting val} {
1569d2cb50b7Sdrh  0 0
1570d2cb50b7Sdrh  1 1
1571d2cb50b7Sdrh  2 2
1572d2cb50b7Sdrh  3 0
1573d2cb50b7Sdrh  -1 0
1574d2cb50b7Sdrh  file 1
1575d2cb50b7Sdrh  FILE 1
1576d2cb50b7Sdrh  fIlE 1
1577d2cb50b7Sdrh  memory 2
1578d2cb50b7Sdrh  MEMORY 2
1579d2cb50b7Sdrh  MeMoRy 2
1580d2cb50b7Sdrh} {
1581d2cb50b7Sdrh  do_test pragma-18.1.$temp_setting {
1582d2cb50b7Sdrh    catch {db close}
1583d2cb50b7Sdrh    sqlite3 db :memory:
1584d2cb50b7Sdrh    execsql "
1585d2cb50b7Sdrh      PRAGMA temp_store=$::temp_setting;
1586d2cb50b7Sdrh      PRAGMA temp_store=$::temp_setting;
1587d2cb50b7Sdrh      PRAGMA temp_store;
1588d2cb50b7Sdrh    "
1589d2cb50b7Sdrh  } $val
1590d2cb50b7Sdrh}
1591d2cb50b7Sdrh
159292c700dbSdrh# The SQLITE_FCNTL_PRAGMA logic, with error handling.
159392c700dbSdrh#
159492c700dbSdrhdb close
159592c700dbSdrhtestvfs tvfs
159692c700dbSdrhsqlite3 db test.db -vfs tvfs
159792c700dbSdrhdo_test pragma-19.1 {
159892c700dbSdrh  catchsql {PRAGMA error}
159992c700dbSdrh} {1 {SQL logic error or missing database}}
160092c700dbSdrhdo_test pragma-19.2 {
160192c700dbSdrh  catchsql {PRAGMA error='This is the error message'}
160292c700dbSdrh} {1 {This is the error message}}
160392c700dbSdrhdo_test pragma-19.3 {
160492c700dbSdrh  catchsql {PRAGMA error='7 This is the error message'}
160592c700dbSdrh} {1 {This is the error message}}
160692c700dbSdrhdo_test pragma-19.4 {
160792c700dbSdrh  catchsql {PRAGMA error=7}
160892c700dbSdrh} {1 {out of memory}}
1609c8517f61Sdrhdo_test pragma-19.5 {
16105b044545Smistachkin  file tail [lindex [execsql {PRAGMA filename}] 0]
1611c8517f61Sdrh} {test.db}
161292c700dbSdrh
1613cc71645bSdrhif {$tcl_platform(platform)=="windows"} {
1614a112d140Smistachkin# Test data_store_directory pragma
1615a112d140Smistachkin#
1616a112d140Smistachkindb close
1617a112d140Smistachkinsqlite3 db test.db
1618a112d140Smistachkinfile mkdir data_dir
1619a112d140Smistachkindo_test pragma-20.1 {
1620a112d140Smistachkin  catchsql {PRAGMA data_store_directory}
1621a112d140Smistachkin} {0 {}}
1622a112d140Smistachkindo_test pragma-20.2 {
1623a112d140Smistachkin  set pwd [string map {' ''} [file nativename [get_pwd]]]
1624a112d140Smistachkin  catchsql "PRAGMA data_store_directory='$pwd';"
1625a112d140Smistachkin} {0 {}}
1626a112d140Smistachkindo_test pragma-20.3 {
1627a112d140Smistachkin  catchsql {PRAGMA data_store_directory}
1628a112d140Smistachkin} [list 0 [list [file nativename [get_pwd]]]]
1629a112d140Smistachkindo_test pragma-20.4 {
1630a112d140Smistachkin  set pwd [string map {' ''} [file nativename \
1631a112d140Smistachkin    [file join [get_pwd] data_dir]]]
1632a112d140Smistachkin  catchsql "PRAGMA data_store_directory='$pwd';"
1633a112d140Smistachkin} {0 {}}
1634a112d140Smistachkindo_test pragma-20.5 {
1635a112d140Smistachkin  sqlite3 db2 test2.db
1636a112d140Smistachkin  catchsql "PRAGMA database_list;" db2
1637a112d140Smistachkin} [list 0 [list 0 main [file nativename \
1638a112d140Smistachkin    [file join [get_pwd] data_dir test2.db]]]]
1639a112d140Smistachkincatch {db2 close}
1640a112d140Smistachkindo_test pragma-20.6 {
1641a112d140Smistachkin  sqlite3 db2 [file join [get_pwd] test2.db]
1642a112d140Smistachkin  catchsql "PRAGMA database_list;" db2
1643a112d140Smistachkin} [list 0 [list 0 main [file nativename \
1644a112d140Smistachkin    [file join [get_pwd] test2.db]]]]
1645a112d140Smistachkincatch {db2 close}
1646a112d140Smistachkindo_test pragma-20.7 {
1647a112d140Smistachkin  catchsql "PRAGMA data_store_directory='';"
1648a112d140Smistachkin} {0 {}}
1649a112d140Smistachkindo_test pragma-20.8 {
1650a112d140Smistachkin  catchsql {PRAGMA data_store_directory}
1651a112d140Smistachkin} {0 {}}
165292c700dbSdrh
1653a112d140Smistachkinforcedelete data_dir
1654cc71645bSdrh} ;# endif windows
1655cd61c281Sdrh
16561fed5dabSdandatabase_may_be_corrupt
16571fed5dabSdan
16585885e762Sdando_test 21.1 {
16595885e762Sdan  # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
16605885e762Sdan  #
16615885e762Sdan  db close
16625885e762Sdan  forcedelete test.db
16635885e762Sdan  sqlite3 db test.db
16645885e762Sdan  execsql {
16655885e762Sdan    PRAGMA page_size = 1024;
16665885e762Sdan    PRAGMA auto_vacuum = 0;
16675885e762Sdan    CREATE TABLE t1(a PRIMARY KEY, b);
16685885e762Sdan    INSERT INTO t1 VALUES(1, 1);
16695885e762Sdan  }
16705885e762Sdan  for {set i 0} {$i < 10} {incr i} {
16715885e762Sdan    execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
16725885e762Sdan  }
16735885e762Sdan  db close
16745885e762Sdan  forcecopy test.db testerr.db
16755885e762Sdan  hexio_write testerr.db 15000 [string repeat 55 100]
16765885e762Sdan} {100}
16775885e762Sdan
16785885e762Sdanset mainerr {*** in database main ***
16795885e762SdanMultiple uses for byte 672 of page 15}
16805885e762Sdanset auxerr {*** in database aux ***
16815885e762SdanMultiple uses for byte 672 of page 15}
16825885e762Sdan
1683597515d7Sdanset mainerr {/{\*\*\* in database main \*\*\*
1684597515d7SdanMultiple uses for byte 672 of page 15}.*/}
1685597515d7Sdanset auxerr {/{\*\*\* in database aux \*\*\*
1686597515d7SdanMultiple uses for byte 672 of page 15}.*/}
1687597515d7Sdan
16885885e762Sdando_test 22.2 {
16895885e762Sdan  catch { db close }
16905885e762Sdan  sqlite3 db testerr.db
16915885e762Sdan  execsql { PRAGMA integrity_check }
1692597515d7Sdan} $mainerr
16935885e762Sdan
16945885e762Sdando_test 22.3.1 {
16955885e762Sdan  catch { db close }
16965885e762Sdan  sqlite3 db test.db
16975885e762Sdan  execsql {
16985885e762Sdan    ATTACH 'testerr.db' AS 'aux';
16995885e762Sdan    PRAGMA integrity_check;
17005885e762Sdan  }
1701597515d7Sdan} $auxerr
17025885e762Sdando_test 22.3.2 {
17035885e762Sdan  execsql { PRAGMA main.integrity_check; }
17045885e762Sdan} {ok}
17055885e762Sdando_test 22.3.3 {
17065885e762Sdan  execsql { PRAGMA aux.integrity_check; }
1707597515d7Sdan} $auxerr
17085885e762Sdan
17095885e762Sdando_test 22.4.1 {
17105885e762Sdan  catch { db close }
17115885e762Sdan  sqlite3 db testerr.db
17125885e762Sdan  execsql {
17135885e762Sdan    ATTACH 'test.db' AS 'aux';
17145885e762Sdan    PRAGMA integrity_check;
17155885e762Sdan  }
1716597515d7Sdan} $mainerr
17175885e762Sdando_test 22.4.2 {
17185885e762Sdan  execsql { PRAGMA main.integrity_check; }
1719597515d7Sdan} $mainerr
17205885e762Sdando_test 22.4.3 {
17215885e762Sdan  execsql { PRAGMA aux.integrity_check; }
17225885e762Sdan} {ok}
17235885e762Sdan
1724c95e01daSdrhdb close
1725c95e01daSdrhforcedelete test.db test.db-wal test.db-journal
1726c95e01daSdrhsqlite3 db test.db
1727c95e01daSdrhsqlite3 db2 test.db
1728c95e01daSdrhdo_test 23.1 {
1729c95e01daSdrh  db eval {
1730c95e01daSdrh    CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1731c95e01daSdrh    CREATE INDEX i1 ON t1(b,c);
1732c95e01daSdrh    CREATE INDEX i2 ON t1(c,d);
1733c95e01daSdrh    CREATE TABLE t2(x INTEGER REFERENCES t1);
1734c95e01daSdrh  }
1735c95e01daSdrh  db2 eval {SELECT name FROM sqlite_master}
1736c95e01daSdrh} {t1 i1 i2 t2}
1737*c228be5bSdrhdo_test 23.2a {
1738c95e01daSdrh  db eval {
1739c95e01daSdrh    DROP INDEX i2;
1740c95e01daSdrh    CREATE INDEX i2 ON t1(c,d,b);
1741c95e01daSdrh  }
1742*c228be5bSdrh  capture_pragma db2 out {PRAGMA index_info(i2)}
1743*c228be5bSdrh  db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1744*c228be5bSdrh} {2 c 0 BINARY 1 | 3 d 0 BINARY 1 | 1 b 0 BINARY 1 |}
1745*c228be5bSdrhdo_test 23.2b {
17464bb8cb09Sdrhbreakpoint;
1747*c228be5bSdrh  capture_pragma db2 out {PRAGMA index_xinfo(i2)}
1748*c228be5bSdrh  db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1749*c228be5bSdrh} {2 c 0 BINARY 1 | 3 d 0 BINARY 1 | 1 b 0 BINARY 1 | -1 {} 0 BINARY 0 |}
1750c95e01daSdrhdo_test 23.3 {
1751c95e01daSdrh  db eval {
1752c95e01daSdrh    CREATE INDEX i3 ON t1(d,b,c);
1753c95e01daSdrh  }
1754*c228be5bSdrh  capture_pragma db2 out {PRAGMA index_list(t1)}
1755*c228be5bSdrh  db2 eval {SELECT name, "unique", origin FROM out ORDER BY seq}
1756*c228be5bSdrh} {i3 0 c i2 0 c i1 0 c}
1757c95e01daSdrhdo_test 23.4 {
1758c95e01daSdrh  db eval {
1759c95e01daSdrh    ALTER TABLE t1 ADD COLUMN e;
1760c95e01daSdrh  }
1761c95e01daSdrh  db2 eval {
1762c95e01daSdrh    PRAGMA table_info(t1);
1763c95e01daSdrh  }
1764c95e01daSdrh} {/4 e {} 0 {} 0/}
1765c95e01daSdrhdo_test 23.5 {
1766c95e01daSdrh  db eval {
1767c95e01daSdrh    DROP TABLE t2;
1768c95e01daSdrh    CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1769c95e01daSdrh  }
1770c95e01daSdrh  db2 eval {
1771c95e01daSdrh    PRAGMA foreign_key_list(t2);
1772c95e01daSdrh  }
1773c95e01daSdrh} {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
1774c95e01daSdrh
17751fed5dabSdandatabase_never_corrupt
1776cd61c281Sdrhfinish_test
1777