blob: 90cd7e2dfe5ea80d88ab9d2932e4d1bb57d520bb [file] [log] [blame]
drhcd61c282002-03-06 22:01:34 +00001# 2002 March 6
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library.
12#
13# This file implements tests for the PRAGMA command.
14#
danielk1977838cce42009-01-12 14:01:45 +000015# $Id: pragma.test,v 1.73 2009/01/12 14:01:45 danielk1977 Exp $
drhcd61c282002-03-06 22:01:34 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
dan5885e762012-07-16 10:06:12 +000019set testprefix pragma
drhcd61c282002-03-06 22:01:34 +000020
dan68928b62010-06-22 13:46:43 +000021# Do not use a codec for tests in this file, as the database file is
22# manipulated directly using tcl scripts (using the [hexio_write] command).
23#
24do_not_use_codec
25
danielk197791cf71b2004-06-26 06:37:06 +000026# Test organization:
27#
28# pragma-1.*: Test cache_size, default_cache_size and synchronous on main db.
29# pragma-2.*: Test synchronous on attached db.
30# pragma-3.*: Test detection of table/index inconsistency by integrity_check.
31# pragma-4.*: Test cache_size and default_cache_size on attached db.
32# pragma-5.*: Test that pragma synchronous may not be used inside of a
33# transaction.
danielk1977cc6bd382005-01-10 02:48:49 +000034# pragma-6.*: Test schema-query pragmas.
35# pragma-7.*: Miscellaneous tests.
36# pragma-8.*: Test user_version and schema_version pragmas.
tpoindex9a09a3c2004-12-20 19:01:32 +000037# pragma-9.*: Test temp_store and temp_store_directory.
danielk1977cc6bd382005-01-10 02:48:49 +000038# pragma-10.*: Test the count_changes pragma in the presence of triggers.
danielk197748af65a2005-02-09 03:20:37 +000039# pragma-11.*: Test the collation_list pragma.
danielk197759a93792008-05-15 17:48:20 +000040# pragma-14.*: Test the page_count pragma.
danielk19778cf6c552008-06-23 16:53:46 +000041# pragma-15.*: Test that the value set using the cache_size pragma is not
42# reset when the schema is reloaded.
aswiftaebf4132008-11-21 00:10:35 +000043# pragma-16.*: Test proxy locking
mistachkina112d142012-03-14 00:44:01 +000044# pragma-20.*: Test data_store_directory.
dan5885e762012-07-16 10:06:12 +000045# pragma-22.*: Test that "PRAGMA [db].integrity_check" respects the "db"
46# directive - if it is present.
danielk197791cf71b2004-06-26 06:37:06 +000047#
48
drh05a82982006-03-19 13:00:25 +000049ifcapable !pragma {
50 finish_test
51 return
52}
53
drhc228be52015-01-31 02:00:01 +000054# Capture the output of a pragma in a TEMP table.
55#
56proc capture_pragma {db tabname sql} {
57 $db eval "DROP TABLE IF EXISTS temp.$tabname"
58 set once 1
59 $db eval $sql x {
60 if {$once} {
61 set once 0
62 set ins "INSERT INTO $tabname VALUES"
63 set crtab "CREATE TEMP TABLE $tabname "
64 set sep "("
65 foreach col $x(*) {
66 append ins ${sep}\$x($col)
67 append crtab ${sep}\"$col\"
68 set sep ,
69 }
70 append ins )
71 append crtab )
72 $db eval $crtab
73 }
74 $db eval $ins
75 }
76}
77
drhcd61c282002-03-06 22:01:34 +000078# Delete the preexisting database to avoid the special setup
79# that the "all.test" script does.
80#
81db close
mistachkinfda06be2011-08-02 00:57:34 +000082delete_file test.db test.db-journal
83delete_file test3.db test3.db-journal
drhdddca282006-01-03 00:33:50 +000084sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +000085
drhb3366b92015-09-11 20:54:44 +000086# EVIDENCE-OF: R-13861-56665 PRAGMA schema.cache_size; PRAGMA
87# schema.cache_size = pages; PRAGMA schema.cache_size = -kibibytes;
drh9d356fb2015-02-27 20:28:08 +000088# Query or change the suggested maximum number of database disk pages
89# that SQLite will hold in memory at once per open database file.
90#
danielk1977c7b4a442004-11-23 10:52:51 +000091ifcapable pager_pragmas {
drh1e9daa62007-04-06 21:42:22 +000092set DFLT_CACHE_SZ [db one {PRAGMA default_cache_size}]
93set TEMP_CACHE_SZ [db one {PRAGMA temp.default_cache_size}]
drhcd61c282002-03-06 22:01:34 +000094do_test pragma-1.1 {
95 execsql {
96 PRAGMA cache_size;
97 PRAGMA default_cache_size;
98 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +000099 }
drh1e9daa62007-04-06 21:42:22 +0000100} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
drhcd61c282002-03-06 22:01:34 +0000101do_test pragma-1.2 {
drh9d356fb2015-02-27 20:28:08 +0000102 # EVIDENCE-OF: R-42059-47211 If the argument N is positive then the
103 # suggested cache size is set to N.
drhcd61c282002-03-06 22:01:34 +0000104 execsql {
drheb43e5c2005-05-22 20:30:39 +0000105 PRAGMA synchronous=OFF;
drhcd61c282002-03-06 22:01:34 +0000106 PRAGMA cache_size=1234;
107 PRAGMA cache_size;
108 PRAGMA default_cache_size;
109 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000110 }
drh1e9daa62007-04-06 21:42:22 +0000111} [list 1234 $DFLT_CACHE_SZ 0]
drhcd61c282002-03-06 22:01:34 +0000112do_test pragma-1.3 {
113 db close
drhef4ac8f2004-06-19 00:16:31 +0000114 sqlite3 db test.db
drhcd61c282002-03-06 22:01:34 +0000115 execsql {
116 PRAGMA cache_size;
117 PRAGMA default_cache_size;
118 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000119 }
drh1e9daa62007-04-06 21:42:22 +0000120} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
drhcd61c282002-03-06 22:01:34 +0000121do_test pragma-1.4 {
122 execsql {
123 PRAGMA synchronous=OFF;
124 PRAGMA cache_size;
125 PRAGMA default_cache_size;
126 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000127 }
drh1e9daa62007-04-06 21:42:22 +0000128} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 0]
drhcd61c282002-03-06 22:01:34 +0000129do_test pragma-1.5 {
130 execsql {
drhd2cb50b2009-01-09 21:41:17 +0000131 PRAGMA cache_size=-4321;
drhcd61c282002-03-06 22:01:34 +0000132 PRAGMA cache_size;
133 PRAGMA default_cache_size;
134 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000135 }
drh3b42abb2011-11-09 14:23:04 +0000136} [list -4321 $DFLT_CACHE_SZ 0]
drhcd61c282002-03-06 22:01:34 +0000137do_test pragma-1.6 {
138 execsql {
139 PRAGMA synchronous=ON;
140 PRAGMA cache_size;
141 PRAGMA default_cache_size;
142 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000143 }
drh3b42abb2011-11-09 14:23:04 +0000144} [list -4321 $DFLT_CACHE_SZ 1]
drhcd61c282002-03-06 22:01:34 +0000145do_test pragma-1.7 {
146 db close
drhef4ac8f2004-06-19 00:16:31 +0000147 sqlite3 db test.db
drhcd61c282002-03-06 22:01:34 +0000148 execsql {
149 PRAGMA cache_size;
150 PRAGMA default_cache_size;
151 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000152 }
drh1e9daa62007-04-06 21:42:22 +0000153} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ 2]
drhcd61c282002-03-06 22:01:34 +0000154do_test pragma-1.8 {
155 execsql {
drhd2cb50b2009-01-09 21:41:17 +0000156 PRAGMA default_cache_size=-123;
drhcd61c282002-03-06 22:01:34 +0000157 PRAGMA cache_size;
158 PRAGMA default_cache_size;
159 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000160 }
danielk197791cf71b2004-06-26 06:37:06 +0000161} {123 123 2}
drh802d69a2005-02-13 23:34:24 +0000162do_test pragma-1.9.1 {
drhcd61c282002-03-06 22:01:34 +0000163 db close
drhdddca282006-01-03 00:33:50 +0000164 sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +0000165 execsql {
166 PRAGMA cache_size;
167 PRAGMA default_cache_size;
168 PRAGMA synchronous;
drhcd61c282002-03-06 22:01:34 +0000169 }
danielk197791cf71b2004-06-26 06:37:06 +0000170} {123 123 2}
drh802d69a2005-02-13 23:34:24 +0000171ifcapable vacuum {
172 do_test pragma-1.9.2 {
173 execsql {
174 VACUUM;
175 PRAGMA cache_size;
176 PRAGMA default_cache_size;
177 PRAGMA synchronous;
178 }
179 } {123 123 2}
180}
danielk197791cf71b2004-06-26 06:37:06 +0000181do_test pragma-1.10 {
drh5a387052003-01-11 14:19:51 +0000182 execsql {
drh4303fee2003-02-15 23:09:17 +0000183 PRAGMA synchronous=NORMAL;
184 PRAGMA cache_size;
185 PRAGMA default_cache_size;
186 PRAGMA synchronous;
drh4303fee2003-02-15 23:09:17 +0000187 }
danielk197791cf71b2004-06-26 06:37:06 +0000188} {123 123 1}
drh6841b1c2016-02-03 19:20:15 +0000189do_test pragma-1.11.1 {
190 execsql {
191 PRAGMA synchronous=EXTRA;
192 PRAGMA cache_size;
193 PRAGMA default_cache_size;
194 PRAGMA synchronous;
195 }
196} {123 123 3}
197do_test pragma-1.11.2 {
drh4303fee2003-02-15 23:09:17 +0000198 execsql {
199 PRAGMA synchronous=FULL;
200 PRAGMA cache_size;
201 PRAGMA default_cache_size;
202 PRAGMA synchronous;
drh4303fee2003-02-15 23:09:17 +0000203 }
danielk197791cf71b2004-06-26 06:37:06 +0000204} {123 123 2}
205do_test pragma-1.12 {
drh4303fee2003-02-15 23:09:17 +0000206 db close
drhdddca282006-01-03 00:33:50 +0000207 sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
drh4303fee2003-02-15 23:09:17 +0000208 execsql {
209 PRAGMA cache_size;
210 PRAGMA default_cache_size;
211 PRAGMA synchronous;
drh4303fee2003-02-15 23:09:17 +0000212 }
danielk197791cf71b2004-06-26 06:37:06 +0000213} {123 123 2}
214
drh5260f7e2004-06-26 19:35:29 +0000215# Make sure the pragma handler understands numeric values in addition
216# to keywords like "off" and "full".
217#
218do_test pragma-1.13 {
219 execsql {
220 PRAGMA synchronous=0;
221 PRAGMA synchronous;
222 }
223} {0}
224do_test pragma-1.14 {
225 execsql {
226 PRAGMA synchronous=2;
227 PRAGMA synchronous;
228 }
229} {2}
drh59ac6552015-04-16 16:04:39 +0000230do_test pragma-1.14.1 {
231 execsql {
232 PRAGMA synchronous=4;
233 PRAGMA synchronous;
234 }
drh6841b1c2016-02-03 19:20:15 +0000235} {4}
drh59ac6552015-04-16 16:04:39 +0000236do_test pragma-1.14.2 {
237 execsql {
drhd99d2832015-04-17 15:58:33 +0000238 PRAGMA synchronous=3;
239 PRAGMA synchronous;
240 }
drh6841b1c2016-02-03 19:20:15 +0000241} {3}
drhd99d2832015-04-17 15:58:33 +0000242do_test pragma-1.14.3 {
243 execsql {
drh6841b1c2016-02-03 19:20:15 +0000244 PRAGMA synchronous=8;
245 PRAGMA synchronous;
246 }
247} {0}
248do_test pragma-1.14.4 {
249 execsql {
drh59ac6552015-04-16 16:04:39 +0000250 PRAGMA synchronous=10;
251 PRAGMA synchronous;
252 }
253} {2}
danielk1977c7b4a442004-11-23 10:52:51 +0000254} ;# ifcapable pager_pragmas
drh5260f7e2004-06-26 19:35:29 +0000255
256# Test turning "flag" pragmas on and off.
257#
danielk19776338c762007-05-17 16:38:30 +0000258ifcapable debug {
259 # Pragma "vdbe_listing" is only available if compiled with SQLITE_DEBUG
260 #
261 do_test pragma-1.15 {
262 execsql {
263 PRAGMA vdbe_listing=YES;
264 PRAGMA vdbe_listing;
265 }
266 } {1}
267 do_test pragma-1.16 {
268 execsql {
269 PRAGMA vdbe_listing=NO;
270 PRAGMA vdbe_listing;
271 }
272 } {0}
273}
274
drh5260f7e2004-06-26 19:35:29 +0000275do_test pragma-1.17 {
276 execsql {
277 PRAGMA parser_trace=ON;
278 PRAGMA parser_trace=OFF;
279 }
280} {}
281do_test pragma-1.18 {
282 execsql {
283 PRAGMA bogus = -1234; -- Parsing of negative values
284 }
285} {}
286
danielk197791cf71b2004-06-26 06:37:06 +0000287# Test modifying the safety_level of an attached database.
danielk19775a8f9372007-10-09 08:29:32 +0000288ifcapable pager_pragmas&&attach {
289 do_test pragma-2.1 {
mistachkinfda06be2011-08-02 00:57:34 +0000290 forcedelete test2.db
291 forcedelete test2.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000292 execsql {
293 ATTACH 'test2.db' AS aux;
294 }
295 } {}
296 do_test pragma-2.2 {
297 execsql {
298 pragma aux.synchronous;
299 }
300 } {2}
301 do_test pragma-2.3 {
302 execsql {
303 pragma aux.synchronous = OFF;
304 pragma aux.synchronous;
305 pragma synchronous;
306 }
307 } {0 2}
308 do_test pragma-2.4 {
309 execsql {
310 pragma aux.synchronous = ON;
311 pragma synchronous;
312 pragma aux.synchronous;
313 }
314 } {2 1}
danielk1977c7b4a442004-11-23 10:52:51 +0000315} ;# ifcapable pager_pragmas
drh4303fee2003-02-15 23:09:17 +0000316
drhed717fe2003-06-15 23:42:24 +0000317# Construct a corrupted index and make sure the integrity_check
318# pragma finds it.
319#
drh25d65432004-07-22 15:02:25 +0000320# These tests won't work if the database is encrypted
321#
drhed717fe2003-06-15 23:42:24 +0000322do_test pragma-3.1 {
drh1e9daa62007-04-06 21:42:22 +0000323 db close
mistachkinfda06be2011-08-02 00:57:34 +0000324 forcedelete test.db test.db-journal
drh1e9daa62007-04-06 21:42:22 +0000325 sqlite3 db test.db
drhed717fe2003-06-15 23:42:24 +0000326 execsql {
drh1e9daa62007-04-06 21:42:22 +0000327 PRAGMA auto_vacuum=OFF;
drhed717fe2003-06-15 23:42:24 +0000328 BEGIN;
329 CREATE TABLE t2(a,b,c);
330 CREATE INDEX i2 ON t2(a);
331 INSERT INTO t2 VALUES(11,2,3);
332 INSERT INTO t2 VALUES(22,3,4);
333 COMMIT;
334 SELECT rowid, * from t2;
335 }
336} {1 11 2 3 2 22 3 4}
danielk19775a8f9372007-10-09 08:29:32 +0000337ifcapable attach {
338 if {![sqlite3 -has-codec] && $sqlite_options(integrityck)} {
339 do_test pragma-3.2 {
drhbb8a2792008-03-19 00:21:30 +0000340 db eval {SELECT rootpage FROM sqlite_master WHERE name='i2'} break
341 set pgsz [db eval {PRAGMA page_size}]
342 # overwrite the header on the rootpage of the index in order to
343 # make the index appear to be empty.
344 #
345 set offset [expr {$pgsz*($rootpage-1)}]
346 hexio_write test.db $offset 0a00000000040000000000
347 db close
348 sqlite3 db test.db
danielk19775a8f9372007-10-09 08:29:32 +0000349 execsql {PRAGMA integrity_check}
drh6fbe41a2013-10-30 20:22:55 +0000350 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000351 do_test pragma-3.3 {
352 execsql {PRAGMA integrity_check=1}
drh6fbe41a2013-10-30 20:22:55 +0000353 } {{row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000354 do_test pragma-3.4 {
355 execsql {
356 ATTACH DATABASE 'test.db' AS t2;
357 PRAGMA integrity_check
358 }
drh6fbe41a2013-10-30 20:22:55 +0000359 } {{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}}
danielk19775a8f9372007-10-09 08:29:32 +0000360 do_test pragma-3.5 {
361 execsql {
drhbb8a2792008-03-19 00:21:30 +0000362 PRAGMA integrity_check=4
danielk19775a8f9372007-10-09 08:29:32 +0000363 }
drh6fbe41a2013-10-30 20:22:55 +0000364 } {{row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000365 do_test pragma-3.6 {
366 execsql {
367 PRAGMA integrity_check=xyz
368 }
drh6fbe41a2013-10-30 20:22:55 +0000369 } {{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}}
danielk19775a8f9372007-10-09 08:29:32 +0000370 do_test pragma-3.7 {
371 execsql {
372 PRAGMA integrity_check=0
373 }
drh6fbe41a2013-10-30 20:22:55 +0000374 } {{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}}
danielk19775a8f9372007-10-09 08:29:32 +0000375
376 # Add additional corruption by appending unused pages to the end of
377 # the database file testerr.db
378 #
379 do_test pragma-3.8 {
380 execsql {DETACH t2}
mistachkinfda06be2011-08-02 00:57:34 +0000381 forcedelete testerr.db testerr.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000382 set out [open testerr.db w]
383 fconfigure $out -translation binary
384 set in [open test.db r]
385 fconfigure $in -translation binary
386 puts -nonewline $out [read $in]
387 seek $in 0
388 puts -nonewline $out [read $in]
389 close $in
390 close $out
drhdd3cd972010-03-27 17:12:36 +0000391 hexio_write testerr.db 28 00000000
danielk19775a8f9372007-10-09 08:29:32 +0000392 execsql {REINDEX t2}
393 execsql {PRAGMA integrity_check}
394 } {ok}
danielk197741c58b72007-12-29 13:39:19 +0000395 do_test pragma-3.8.1 {
396 execsql {PRAGMA quick_check}
397 } {ok}
drh5d16a9a2011-10-13 14:41:22 +0000398 do_test pragma-3.8.2 {
399 execsql {PRAGMA QUICK_CHECK}
400 } {ok}
danielk19775a8f9372007-10-09 08:29:32 +0000401 do_test pragma-3.9 {
402 execsql {
403 ATTACH 'testerr.db' AS t2;
404 PRAGMA integrity_check
405 }
406 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000407Page 4 is never used
408Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000409Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000410 do_test pragma-3.10 {
411 execsql {
412 PRAGMA integrity_check=1
413 }
414 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000415Page 4 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000416 do_test pragma-3.11 {
417 execsql {
418 PRAGMA integrity_check=5
419 }
420 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000421Page 4 is never used
422Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000423Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000424 do_test pragma-3.12 {
425 execsql {
426 PRAGMA integrity_check=4
427 }
428 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000429Page 4 is never used
430Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000431Page 6 is never used} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000432 do_test pragma-3.13 {
433 execsql {
434 PRAGMA integrity_check=3
435 }
436 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000437Page 4 is never used
438Page 5 is never used
439Page 6 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000440 do_test pragma-3.14 {
441 execsql {
442 PRAGMA integrity_check(2)
443 }
444 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000445Page 4 is never used
446Page 5 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000447 do_test pragma-3.15 {
448 execsql {
449 ATTACH 'testerr.db' AS t3;
450 PRAGMA integrity_check
451 }
452 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000453Page 4 is never used
454Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000455Page 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 ***
drh1dcdbc02007-01-27 02:24:54 +0000456Page 4 is never used
457Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000458Page 6 is never used} {row 1 missing from index i2} {row 2 missing from index i2} {wrong # of entries in index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000459 do_test pragma-3.16 {
460 execsql {
drhbb8a2792008-03-19 00:21:30 +0000461 PRAGMA integrity_check(10)
danielk19775a8f9372007-10-09 08:29:32 +0000462 }
463 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000464Page 4 is never used
465Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000466Page 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 ***
drh1dcdbc02007-01-27 02:24:54 +0000467Page 4 is never used
468Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000469Page 6 is never used} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000470 do_test pragma-3.17 {
471 execsql {
drhbb8a2792008-03-19 00:21:30 +0000472 PRAGMA integrity_check=8
danielk19775a8f9372007-10-09 08:29:32 +0000473 }
474 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000475Page 4 is never used
476Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000477Page 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 ***
drh1dcdbc02007-01-27 02:24:54 +0000478Page 4 is never used
479Page 5 is never used}}
danielk19775a8f9372007-10-09 08:29:32 +0000480 do_test pragma-3.18 {
481 execsql {
482 PRAGMA integrity_check=4
483 }
484 } {{*** in database t2 ***
drh1dcdbc02007-01-27 02:24:54 +0000485Page 4 is never used
486Page 5 is never used
drh6fbe41a2013-10-30 20:22:55 +0000487Page 6 is never used} {row 1 missing from index i2}}
danielk19775a8f9372007-10-09 08:29:32 +0000488 }
drhd2cb50b2009-01-09 21:41:17 +0000489 do_test pragma-3.19 {
490 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000491 forcedelete test.db test.db-journal
drhd2cb50b2009-01-09 21:41:17 +0000492 sqlite3 db test.db
493 db eval {PRAGMA integrity_check}
494 } {ok}
drh25d65432004-07-22 15:02:25 +0000495}
drhcefc87f2014-08-01 01:40:33 +0000496
497# Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
498# constraint violations.
499#
500do_execsql_test pragma-3.20 {
501 CREATE TABLE t1(a,b);
502 CREATE INDEX t1a ON t1(a);
503 INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
504 PRAGMA writable_schema=ON;
505 UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
506 WHERE name='t1a';
507 UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
508 WHERE name='t1';
509 PRAGMA writable_schema=OFF;
510 ALTER TABLE t1 RENAME TO t1x;
511 PRAGMA integrity_check;
512} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
513do_execsql_test pragma-3.21 {
514 PRAGMA integrity_check(3);
515} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
516do_execsql_test pragma-3.22 {
517 PRAGMA integrity_check(2);
518} {{non-unique entry in index t1a} {NULL value in t1x.a}}
drh7efa4262014-12-16 00:08:31 +0000519do_execsql_test pragma-3.23 {
drhcefc87f2014-08-01 01:40:33 +0000520 PRAGMA integrity_check(1);
521} {{non-unique entry in index t1a}}
danielk197791cf71b2004-06-26 06:37:06 +0000522
drh7efa4262014-12-16 00:08:31 +0000523# PRAGMA integrity check (or more specifically the sqlite3BtreeCount()
524# interface) used to leave index cursors in an inconsistent state
525# which could result in an assertion fault in sqlite3BtreeKey()
526# called from saveCursorPosition() if content is removed from the
527# index while the integrity_check is still running. This test verifies
528# that problem has been fixed.
529#
530do_test pragma-3.30 {
531 db close
532 delete_file test.db
533 sqlite3 db test.db
534 db eval {
535 CREATE TABLE t1(a,b,c);
536 WITH RECURSIVE
537 c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<100)
538 INSERT INTO t1(a,b,c) SELECT i, printf('xyz%08x',i), 2000-i FROM c;
539 CREATE INDEX t1a ON t1(a);
540 CREATE INDEX t1bc ON t1(b,c);
541 }
542 db eval {PRAGMA integrity_check} {
543 db eval {DELETE FROM t1}
544 }
545} {}
546
danielk197791cf71b2004-06-26 06:37:06 +0000547# Test modifying the cache_size of an attached database.
danielk19775a8f9372007-10-09 08:29:32 +0000548ifcapable pager_pragmas&&attach {
danielk197791cf71b2004-06-26 06:37:06 +0000549do_test pragma-4.1 {
550 execsql {
drh1e9daa62007-04-06 21:42:22 +0000551 ATTACH 'test2.db' AS aux;
danielk197791cf71b2004-06-26 06:37:06 +0000552 pragma aux.cache_size;
553 pragma aux.default_cache_size;
554 }
drh1e9daa62007-04-06 21:42:22 +0000555} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000556do_test pragma-4.2 {
557 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000558 pragma aux.cache_size = 50;
559 pragma aux.cache_size;
560 pragma aux.default_cache_size;
561 }
drh1e9daa62007-04-06 21:42:22 +0000562} [list 50 $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000563do_test pragma-4.3 {
564 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000565 pragma aux.default_cache_size = 456;
566 pragma aux.cache_size;
567 pragma aux.default_cache_size;
568 }
569} {456 456}
drh1bdd9b52004-04-23 17:04:44 +0000570do_test pragma-4.4 {
571 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000572 pragma cache_size;
573 pragma default_cache_size;
574 }
drh1e9daa62007-04-06 21:42:22 +0000575} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000576do_test pragma-4.5 {
577 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000578 DETACH aux;
579 ATTACH 'test3.db' AS aux;
580 pragma aux.cache_size;
581 pragma aux.default_cache_size;
582 }
drh1e9daa62007-04-06 21:42:22 +0000583} [list $DFLT_CACHE_SZ $DFLT_CACHE_SZ]
drh1bdd9b52004-04-23 17:04:44 +0000584do_test pragma-4.6 {
585 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000586 DETACH aux;
587 ATTACH 'test2.db' AS aux;
588 pragma aux.cache_size;
589 pragma aux.default_cache_size;
590 }
591} {456 456}
danielk1977c7b4a442004-11-23 10:52:51 +0000592} ;# ifcapable pager_pragmas
drh1bdd9b52004-04-23 17:04:44 +0000593
danielk197791cf71b2004-06-26 06:37:06 +0000594# Test that modifying the sync-level in the middle of a transaction is
595# disallowed.
danielk1977c7b4a442004-11-23 10:52:51 +0000596ifcapable pager_pragmas {
danielk197791cf71b2004-06-26 06:37:06 +0000597do_test pragma-5.0 {
drh1bdd9b52004-04-23 17:04:44 +0000598 execsql {
danielk197791cf71b2004-06-26 06:37:06 +0000599 pragma synchronous;
600 }
601} {2}
602do_test pragma-5.1 {
drh1bdd9b52004-04-23 17:04:44 +0000603 catchsql {
604 BEGIN;
danielk197791cf71b2004-06-26 06:37:06 +0000605 pragma synchronous = OFF;
606 }
607} {1 {Safety level may not be changed inside a transaction}}
608do_test pragma-5.2 {
609 execsql {
610 pragma synchronous;
611 }
612} {2}
drh5260f7e2004-06-26 19:35:29 +0000613catchsql {COMMIT;}
danielk1977c7b4a442004-11-23 10:52:51 +0000614} ;# ifcapable pager_pragmas
drh5260f7e2004-06-26 19:35:29 +0000615
616# Test schema-query pragmas
617#
danielk197727188fb2004-11-23 10:13:03 +0000618ifcapable schema_pragmas {
danielk19775a8f9372007-10-09 08:29:32 +0000619ifcapable tempdb&&attach {
danielk197753c0f742005-03-29 03:10:59 +0000620 do_test pragma-6.1 {
621 set res {}
622 execsql {SELECT * FROM sqlite_temp_master}
623 foreach {idx name file} [execsql {pragma database_list}] {
624 lappend res $idx $name
625 }
626 set res
627 } {0 main 1 temp 2 aux}
628}
drh5260f7e2004-06-26 19:35:29 +0000629do_test pragma-6.2 {
630 execsql {
drha6dddd92016-04-18 15:46:14 +0000631 CREATE TABLE t2(a TYPE_X, b [TYPE_Y], c "TYPE_Z");
drh5260f7e2004-06-26 19:35:29 +0000632 pragma table_info(t2)
633 }
drha6dddd92016-04-18 15:46:14 +0000634} {0 a TYPE_X 0 {} 0 1 b TYPE_Y 0 {} 0 2 c TYPE_Z 0 {} 0}
drhd2cb50b2009-01-09 21:41:17 +0000635do_test pragma-6.2.1 {
636 execsql {
637 pragma table_info;
638 }
639} {}
drh736c7d42006-11-30 13:06:37 +0000640db nullvalue <<NULL>>
drh417ec632006-08-14 14:23:41 +0000641do_test pragma-6.2.2 {
642 execsql {
drh736c7d42006-11-30 13:06:37 +0000643 CREATE TABLE t5(
644 a TEXT DEFAULT CURRENT_TIMESTAMP,
645 b DEFAULT (5+3),
646 c TEXT,
647 d INTEGER DEFAULT NULL,
drh384b7fe2013-01-01 13:55:31 +0000648 e TEXT DEFAULT '',
649 UNIQUE(b,c,d),
650 PRIMARY KEY(e,b,c)
drh736c7d42006-11-30 13:06:37 +0000651 );
drh417ec632006-08-14 14:23:41 +0000652 PRAGMA table_info(t5);
653 }
drh384b7fe2013-01-01 13:55:31 +0000654} {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}
drh736c7d42006-11-30 13:06:37 +0000655db nullvalue {}
drh384b7fe2013-01-01 13:55:31 +0000656do_test pragma-6.2.3 {
657 execsql {
658 CREATE TABLE t2_3(a,b INTEGER PRIMARY KEY,c);
659 pragma table_info(t2_3)
660 }
661} {0 a {} 0 {} 0 1 b INTEGER 0 {} 1 2 c {} 0 {} 0}
drh6bf89572004-11-03 16:27:01 +0000662ifcapable {foreignkey} {
drhd2cb50b2009-01-09 21:41:17 +0000663 do_test pragma-6.3.1 {
drh6bf89572004-11-03 16:27:01 +0000664 execsql {
665 CREATE TABLE t3(a int references t2(b), b UNIQUE);
666 pragma foreign_key_list(t3);
667 }
dan1da40a32009-09-19 17:00:31 +0000668 } {0 0 t2 a b {NO ACTION} {NO ACTION} NONE}
drhd2cb50b2009-01-09 21:41:17 +0000669 do_test pragma-6.3.2 {
670 execsql {
671 pragma foreign_key_list;
672 }
673 } {}
674 do_test pragma-6.3.3 {
675 execsql {
676 pragma foreign_key_list(t3_bogus);
677 }
678 } {}
679 do_test pragma-6.3.4 {
680 execsql {
681 pragma foreign_key_list(t5);
682 }
683 } {}
drh6bf89572004-11-03 16:27:01 +0000684 do_test pragma-6.4 {
drhc228be52015-01-31 02:00:01 +0000685 capture_pragma db out {
drh6bf89572004-11-03 16:27:01 +0000686 pragma index_list(t3);
687 }
drhc228be52015-01-31 02:00:01 +0000688 db eval {SELECT seq, "name", "unique" FROM out ORDER BY seq}
drh3ef26152013-10-12 20:22:00 +0000689 } {0 sqlite_autoindex_t3_1 1}
drh6bf89572004-11-03 16:27:01 +0000690}
691ifcapable {!foreignkey} {
692 execsql {CREATE TABLE t3(a,b UNIQUE)}
693}
drhd2cb50b2009-01-09 21:41:17 +0000694do_test pragma-6.5.1 {
drh5260f7e2004-06-26 19:35:29 +0000695 execsql {
696 CREATE INDEX t3i1 ON t3(a,b);
drhc228be52015-01-31 02:00:01 +0000697 }
698 capture_pragma db out {
drh5260f7e2004-06-26 19:35:29 +0000699 pragma index_info(t3i1);
700 }
drhc228be52015-01-31 02:00:01 +0000701 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
drh5260f7e2004-06-26 19:35:29 +0000702} {0 0 a 1 1 b}
drh7be0fd92015-03-05 15:34:15 +0000703
704# EVIDENCE-OF: R-23114-21695 The auxiliary index-columns are not shown
705# by the index_info pragma, but they are listed by the index_xinfo
706# pragma.
707#
708do_test pragma-6.5.1b {
709 capture_pragma db out {PRAGMA index_xinfo(t3i1)}
710 db eval {SELECT seqno, cid, name FROM out ORDER BY seqno}
711} {0 0 a 1 1 b 2 -1 {}}
712
713
drhb3366b92015-09-11 20:54:44 +0000714# EVIDENCE-OF: R-29448-60346 PRAGMA schema.index_info(index-name); This
715# pragma returns one row for each key column in the named index.
drh7be0fd92015-03-05 15:34:15 +0000716#
717# (The first column of output from PRAGMA index_info is...)
718# EVIDENCE-OF: R-34186-52914 The rank of the column within the index. (0
719# means left-most.)
720#
721# (The second column of output from PRAGMA index_info is...)
722# EVIDENCE-OF: R-65019-08383 The rank of the column within the table
723# being indexed.
724#
725# (The third column of output from PRAGMA index_info is...)
726# EVIDENCE-OF: R-09773-34266 The name of the column being indexed.
727#
728do_execsql_test pragma-6.5.1c {
729 CREATE INDEX t3i2 ON t3(b,a);
730 PRAGMA index_info='t3i2';
731 DROP INDEX t3i2;
732} {0 1 b 1 0 a}
733
drhd2cb50b2009-01-09 21:41:17 +0000734do_test pragma-6.5.2 {
735 execsql {
736 pragma index_info(t3i1_bogus);
737 }
738} {}
danielk1977260d8a62008-08-20 16:34:24 +0000739
740ifcapable tempdb {
741 # Test for ticket #3320. When a temp table of the same name exists, make
742 # sure the schema of the main table can still be queried using
743 # "pragma table_info":
744 do_test pragma-6.6.1 {
745 execsql {
746 CREATE TABLE trial(col_main);
747 CREATE TEMP TABLE trial(col_temp);
748 }
749 } {}
750 do_test pragma-6.6.2 {
751 execsql {
752 PRAGMA table_info(trial);
753 }
754 } {0 col_temp {} 0 {} 0}
755 do_test pragma-6.6.3 {
756 execsql {
757 PRAGMA temp.table_info(trial);
758 }
759 } {0 col_temp {} 0 {} 0}
760 do_test pragma-6.6.4 {
761 execsql {
762 PRAGMA main.table_info(trial);
763 }
764 } {0 col_main {} 0 {} 0}
765}
danielk1977f96a3772008-10-23 05:45:07 +0000766
danielk1977f96a3772008-10-23 05:45:07 +0000767do_test pragma-6.7 {
768 execsql {
769 CREATE TABLE test_table(
770 one INT NOT NULL DEFAULT -1,
771 two text,
772 three VARCHAR(45, 65) DEFAULT 'abcde',
773 four REAL DEFAULT X'abcdef',
774 five DEFAULT CURRENT_TIME
775 );
danielk1977f96a3772008-10-23 05:45:07 +0000776 }
drhc228be52015-01-31 02:00:01 +0000777 capture_pragma db out {PRAGMA table_info(test_table)}
778 db eval {SELECT cid, "name", type, "notnull", dflt_value, pk FROM out
779 ORDER BY cid}
danielk1977f96a3772008-10-23 05:45:07 +0000780} [concat \
781 {0 one INT 1 -1 0} \
782 {1 two text 0 {} 0} \
783 {2 three {VARCHAR(45, 65)} 0 'abcde' 0} \
784 {3 four REAL 0 X'abcdef' 0} \
785 {4 five {} 0 CURRENT_TIME 0} \
786]
drh1b678962015-04-15 07:19:27 +0000787do_test pragma-6.8 {
788 execsql {
789 CREATE TABLE t68(a,b,c,PRIMARY KEY(a,b,a,c));
790 PRAGMA table_info(t68);
791 }
792} [concat \
793 {0 a {} 0 {} 1} \
794 {1 b {} 0 {} 2} \
795 {2 c {} 0 {} 4} \
796]
danielk197727188fb2004-11-23 10:13:03 +0000797} ;# ifcapable schema_pragmas
drh5260f7e2004-06-26 19:35:29 +0000798# Miscellaneous tests
799#
danielk197727188fb2004-11-23 10:13:03 +0000800ifcapable schema_pragmas {
drhb3366b92015-09-11 20:54:44 +0000801# EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
802# pragma returns one row for each index associated with the given table.
803#
drhd2cb50b2009-01-09 21:41:17 +0000804do_test pragma-7.1.1 {
drh5260f7e2004-06-26 19:35:29 +0000805 # Make sure a pragma knows to read the schema if it needs to
806 db close
807 sqlite3 db test.db
drhc228be52015-01-31 02:00:01 +0000808 capture_pragma db out "PRAGMA index_list(t3)"
809 db eval {SELECT name, "origin" FROM out ORDER BY name DESC}
810} {t3i1 c sqlite_autoindex_t3_1 u}
drhd2cb50b2009-01-09 21:41:17 +0000811do_test pragma-7.1.2 {
812 execsql {
813 pragma index_list(t3_bogus);
814 }
815} {}
danielk197727188fb2004-11-23 10:13:03 +0000816} ;# ifcapable schema_pragmas
drh6c626082004-11-14 21:56:29 +0000817ifcapable {utf16} {
dancb354602010-07-08 09:44:42 +0000818 if {[permutation] == ""} {
819 do_test pragma-7.2 {
820 db close
821 sqlite3 db test.db
822 catchsql {
823 pragma encoding=bogus;
824 }
825 } {1 {unsupported encoding: bogus}}
826 }
drh6c626082004-11-14 21:56:29 +0000827}
danielk197753c0f742005-03-29 03:10:59 +0000828ifcapable tempdb {
829 do_test pragma-7.3 {
830 db close
831 sqlite3 db test.db
832 execsql {
833 pragma lock_status;
834 }
835 } {main unlocked temp closed}
836} else {
837 do_test pragma-7.3 {
838 db close
839 sqlite3 db test.db
840 execsql {
841 pragma lock_status;
842 }
843 } {main unlocked}
844}
drh5260f7e2004-06-26 19:35:29 +0000845
846
danielk1977dae24952004-11-11 05:10:43 +0000847#----------------------------------------------------------------------
danielk1977b92b70b2004-11-12 16:11:59 +0000848# Test cases pragma-8.* test the "PRAGMA schema_version" and "PRAGMA
849# user_version" statements.
danielk1977dae24952004-11-11 05:10:43 +0000850#
danielk1977b92b70b2004-11-12 16:11:59 +0000851# pragma-8.1: PRAGMA schema_version
852# pragma-8.2: PRAGMA user_version
danielk1977dae24952004-11-11 05:10:43 +0000853#
854
danielk197711cf9fb2004-11-23 11:16:42 +0000855ifcapable schema_version {
856
danielk1977b92b70b2004-11-12 16:11:59 +0000857# First check that we can set the schema version and then retrieve the
danielk1977dae24952004-11-11 05:10:43 +0000858# same value.
859do_test pragma-8.1.1 {
860 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000861 PRAGMA schema_version = 105;
danielk1977dae24952004-11-11 05:10:43 +0000862 }
863} {}
864do_test pragma-8.1.2 {
drh25403652007-01-04 22:13:41 +0000865 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000866 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000867 }
drh25403652007-01-04 22:13:41 +0000868} {schema_version 105}
danielk1977dae24952004-11-11 05:10:43 +0000869do_test pragma-8.1.3 {
870 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000871 PRAGMA schema_version = 106;
danielk1977dae24952004-11-11 05:10:43 +0000872 }
873} {}
874do_test pragma-8.1.4 {
875 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000876 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000877 }
878} 106
879
danielk1977b92b70b2004-11-12 16:11:59 +0000880# Check that creating a table modifies the schema-version (this is really
881# to verify that the value being read is in fact the schema version).
danielk1977dae24952004-11-11 05:10:43 +0000882do_test pragma-8.1.5 {
883 execsql {
884 CREATE TABLE t4(a, b, c);
885 INSERT INTO t4 VALUES(1, 2, 3);
886 SELECT * FROM t4;
887 }
888} {1 2 3}
889do_test pragma-8.1.6 {
890 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000891 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000892 }
893} 107
894
895# Now open a second connection to the database. Ensure that changing the
danielk1977b92b70b2004-11-12 16:11:59 +0000896# schema-version using the first connection forces the second connection
danielk1977dae24952004-11-11 05:10:43 +0000897# to reload the schema. This has to be done using the C-API test functions,
898# because the TCL API accounts for SCHEMA_ERROR and retries the query.
899do_test pragma-8.1.7 {
drhdddca282006-01-03 00:33:50 +0000900 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
danielk1977dae24952004-11-11 05:10:43 +0000901 execsql {
902 SELECT * FROM t4;
903 } db2
904} {1 2 3}
905do_test pragma-8.1.8 {
906 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000907 PRAGMA schema_version = 108;
danielk1977dae24952004-11-11 05:10:43 +0000908 }
909} {}
910do_test pragma-8.1.9 {
911 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM t4" -1 DUMMY]
912 sqlite3_step $::STMT
913} SQLITE_ERROR
914do_test pragma-8.1.10 {
915 sqlite3_finalize $::STMT
916} SQLITE_SCHEMA
917
danielk1977b92b70b2004-11-12 16:11:59 +0000918# Make sure the schema-version can be manipulated in an attached database.
mistachkinfda06be2011-08-02 00:57:34 +0000919forcedelete test2.db
920forcedelete test2.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000921ifcapable attach {
922 do_test pragma-8.1.11 {
923 execsql {
924 ATTACH 'test2.db' AS aux;
925 CREATE TABLE aux.t1(a, b, c);
926 PRAGMA aux.schema_version = 205;
927 }
928 } {}
929 do_test pragma-8.1.12 {
930 execsql {
931 PRAGMA aux.schema_version;
932 }
933 } 205
934}
danielk1977dae24952004-11-11 05:10:43 +0000935do_test pragma-8.1.13 {
936 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000937 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000938 }
939} 108
940
danielk1977b92b70b2004-11-12 16:11:59 +0000941# And check that modifying the schema-version in an attached database
danielk1977dae24952004-11-11 05:10:43 +0000942# forces the second connection to reload the schema.
danielk19775a8f9372007-10-09 08:29:32 +0000943ifcapable attach {
944 do_test pragma-8.1.14 {
945 sqlite3 db2 test.db; set ::DB2 [sqlite3_connection_pointer db2]
946 execsql {
947 ATTACH 'test2.db' AS aux;
948 SELECT * FROM aux.t1;
949 } db2
950 } {}
951 do_test pragma-8.1.15 {
952 execsql {
953 PRAGMA aux.schema_version = 206;
954 }
955 } {}
956 do_test pragma-8.1.16 {
957 set ::STMT [sqlite3_prepare $::DB2 "SELECT * FROM aux.t1" -1 DUMMY]
958 sqlite3_step $::STMT
959 } SQLITE_ERROR
960 do_test pragma-8.1.17 {
961 sqlite3_finalize $::STMT
962 } SQLITE_SCHEMA
963 do_test pragma-8.1.18 {
964 db2 close
965 } {}
966}
danielk1977dae24952004-11-11 05:10:43 +0000967
danielk1977b92b70b2004-11-12 16:11:59 +0000968# Now test that the user-version can be read and written (and that we aren't
969# accidentally manipulating the schema-version instead).
danielk1977dae24952004-11-11 05:10:43 +0000970do_test pragma-8.2.1 {
drh25403652007-01-04 22:13:41 +0000971 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000972 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +0000973 }
drh25403652007-01-04 22:13:41 +0000974} {user_version 0}
danielk1977dae24952004-11-11 05:10:43 +0000975do_test pragma-8.2.2 {
976 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000977 PRAGMA user_version = 2;
danielk1977dae24952004-11-11 05:10:43 +0000978 }
979} {}
drh802d69a2005-02-13 23:34:24 +0000980do_test pragma-8.2.3.1 {
drh25403652007-01-04 22:13:41 +0000981 execsql2 {
danielk1977b92b70b2004-11-12 16:11:59 +0000982 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +0000983 }
drh25403652007-01-04 22:13:41 +0000984} {user_version 2}
drh802d69a2005-02-13 23:34:24 +0000985do_test pragma-8.2.3.2 {
986 db close
987 sqlite3 db test.db
988 execsql {
989 PRAGMA user_version;
990 }
991} {2}
992do_test pragma-8.2.4.1 {
danielk1977dae24952004-11-11 05:10:43 +0000993 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +0000994 PRAGMA schema_version;
danielk1977dae24952004-11-11 05:10:43 +0000995 }
996} {108}
drh802d69a2005-02-13 23:34:24 +0000997ifcapable vacuum {
998 do_test pragma-8.2.4.2 {
999 execsql {
1000 VACUUM;
1001 PRAGMA user_version;
1002 }
1003 } {2}
1004 do_test pragma-8.2.4.3 {
1005 execsql {
1006 PRAGMA schema_version;
1007 }
1008 } {109}
1009}
danielk1977dae24952004-11-11 05:10:43 +00001010
danielk19775a8f9372007-10-09 08:29:32 +00001011ifcapable attach {
1012 db eval {ATTACH 'test2.db' AS aux}
1013
1014 # Check that the user-version in the auxilary database can be manipulated (
1015 # and that we aren't accidentally manipulating the same in the main db).
1016 do_test pragma-8.2.5 {
1017 execsql {
1018 PRAGMA aux.user_version;
1019 }
1020 } {0}
1021 do_test pragma-8.2.6 {
1022 execsql {
1023 PRAGMA aux.user_version = 3;
1024 }
1025 } {}
1026 do_test pragma-8.2.7 {
1027 execsql {
1028 PRAGMA aux.user_version;
1029 }
1030 } {3}
1031 do_test pragma-8.2.8 {
1032 execsql {
1033 PRAGMA main.user_version;
1034 }
1035 } {2}
1036
1037 # Now check that a ROLLBACK resets the user-version if it has been modified
1038 # within a transaction.
1039 do_test pragma-8.2.9 {
1040 execsql {
1041 BEGIN;
1042 PRAGMA aux.user_version = 10;
1043 PRAGMA user_version = 11;
1044 }
1045 } {}
1046 do_test pragma-8.2.10 {
1047 execsql {
1048 PRAGMA aux.user_version;
1049 }
1050 } {10}
1051 do_test pragma-8.2.11 {
1052 execsql {
1053 PRAGMA main.user_version;
1054 }
1055 } {11}
1056 do_test pragma-8.2.12 {
1057 execsql {
1058 ROLLBACK;
1059 PRAGMA aux.user_version;
1060 }
1061 } {3}
1062 do_test pragma-8.2.13 {
1063 execsql {
1064 PRAGMA main.user_version;
1065 }
1066 } {2}
1067}
danielk1977dae24952004-11-11 05:10:43 +00001068
danielk1977b92b70b2004-11-12 16:11:59 +00001069# Try a negative value for the user-version
danielk1977dae24952004-11-11 05:10:43 +00001070do_test pragma-8.2.14 {
1071 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +00001072 PRAGMA user_version = -450;
danielk1977dae24952004-11-11 05:10:43 +00001073 }
1074} {}
1075do_test pragma-8.2.15 {
1076 execsql {
danielk1977b92b70b2004-11-12 16:11:59 +00001077 PRAGMA user_version;
danielk1977dae24952004-11-11 05:10:43 +00001078 }
1079} {-450}
danielk1977d9c847d2005-01-07 10:42:48 +00001080} ; # ifcapable schema_version
1081
drhd19744f2008-03-18 13:46:53 +00001082# Check to see if TEMP_STORE is memory or disk. Return strings
1083# "memory" or "disk" as appropriate.
1084#
1085proc check_temp_store {} {
dan9131ab92016-04-06 18:20:51 +00001086 db eval {
1087 PRAGMA temp.cache_size = 1;
1088 CREATE TEMP TABLE IF NOT EXISTS a(b);
1089 DELETE FROM a;
1090 INSERT INTO a VALUES(randomblob(1000));
1091 INSERT INTO a SELECT * FROM a;
1092 INSERT INTO a SELECT * FROM a;
1093 INSERT INTO a SELECT * FROM a;
1094 INSERT INTO a SELECT * FROM a;
1095 INSERT INTO a SELECT * FROM a;
1096 INSERT INTO a SELECT * FROM a;
1097 INSERT INTO a SELECT * FROM a;
1098 INSERT INTO a SELECT * FROM a;
1099 }
drhd19744f2008-03-18 13:46:53 +00001100 db eval {PRAGMA database_list} {
1101 if {$name=="temp"} {
danielk197717b90b52008-06-06 11:11:25 +00001102 set bt [btree_from_db db 1]
1103 if {[btree_ismemdb $bt]} {
drhd19744f2008-03-18 13:46:53 +00001104 return "memory"
drhd19744f2008-03-18 13:46:53 +00001105 }
danielk197717b90b52008-06-06 11:11:25 +00001106 return "disk"
drhd19744f2008-03-18 13:46:53 +00001107 }
1108 }
1109 return "unknown"
1110}
1111
drh4ee09b42013-05-01 19:49:27 +00001112# Application_ID
1113#
1114do_test pragma-8.3.1 {
1115 execsql {
1116 PRAGMA application_id;
1117 }
1118} {0}
1119do_test pragma-8.3.2 {
1120 execsql {PRAGMA Application_ID(12345); PRAGMA application_id;}
1121} {12345}
tpoindex9a09a3c2004-12-20 19:01:32 +00001122
1123# Test temp_store and temp_store_directory pragmas
1124#
drh268283b2005-01-08 15:44:25 +00001125ifcapable pager_pragmas {
tpoindex9a09a3c2004-12-20 19:01:32 +00001126do_test pragma-9.1 {
1127 db close
1128 sqlite3 db test.db
1129 execsql {
1130 PRAGMA temp_store;
1131 }
1132} {0}
drhd19744f2008-03-18 13:46:53 +00001133if {$TEMP_STORE<=1} {
1134 do_test pragma-9.1.1 {
1135 check_temp_store
1136 } {disk}
1137} else {
1138 do_test pragma-9.1.1 {
1139 check_temp_store
1140 } {memory}
1141}
1142
tpoindex9a09a3c2004-12-20 19:01:32 +00001143do_test pragma-9.2 {
drhd19744f2008-03-18 13:46:53 +00001144 db close
1145 sqlite3 db test.db
tpoindex9a09a3c2004-12-20 19:01:32 +00001146 execsql {
1147 PRAGMA temp_store=file;
1148 PRAGMA temp_store;
1149 }
1150} {1}
drhd19744f2008-03-18 13:46:53 +00001151if {$TEMP_STORE==3} {
1152 # When TEMP_STORE is 3, always use memory regardless of pragma settings.
1153 do_test pragma-9.2.1 {
1154 check_temp_store
1155 } {memory}
1156} else {
1157 do_test pragma-9.2.1 {
1158 check_temp_store
1159 } {disk}
1160}
1161
tpoindex9a09a3c2004-12-20 19:01:32 +00001162do_test pragma-9.3 {
drhd19744f2008-03-18 13:46:53 +00001163 db close
1164 sqlite3 db test.db
tpoindex9a09a3c2004-12-20 19:01:32 +00001165 execsql {
1166 PRAGMA temp_store=memory;
1167 PRAGMA temp_store;
1168 }
1169} {2}
drhd19744f2008-03-18 13:46:53 +00001170if {$TEMP_STORE==0} {
1171 # When TEMP_STORE is 0, always use the disk regardless of pragma settings.
1172 do_test pragma-9.3.1 {
1173 check_temp_store
1174 } {disk}
1175} else {
1176 do_test pragma-9.3.1 {
1177 check_temp_store
1178 } {memory}
1179}
1180
tpoindex9a09a3c2004-12-20 19:01:32 +00001181do_test pragma-9.4 {
1182 execsql {
1183 PRAGMA temp_store_directory;
1184 }
1185} {}
drh78f82d12008-09-02 00:52:52 +00001186ifcapable wsd {
1187 do_test pragma-9.5 {
mistachkinf8a78462012-03-08 20:00:36 +00001188 set pwd [string map {' ''} [file nativename [get_pwd]]]
drh78f82d12008-09-02 00:52:52 +00001189 execsql "
1190 PRAGMA temp_store_directory='$pwd';
1191 "
1192 } {}
1193 do_test pragma-9.6 {
1194 execsql {
1195 PRAGMA temp_store_directory;
1196 }
mistachkinf8a78462012-03-08 20:00:36 +00001197 } [list [file nativename [get_pwd]]]
drh78f82d12008-09-02 00:52:52 +00001198 do_test pragma-9.7 {
1199 catchsql {
1200 PRAGMA temp_store_directory='/NON/EXISTENT/PATH/FOOBAR';
1201 }
1202 } {1 {not a writable directory}}
1203 do_test pragma-9.8 {
1204 execsql {
1205 PRAGMA temp_store_directory='';
1206 }
1207 } {}
1208 if {![info exists TEMP_STORE] || $TEMP_STORE<=1} {
1209 ifcapable tempdb {
1210 do_test pragma-9.9 {
1211 execsql {
1212 PRAGMA temp_store_directory;
1213 PRAGMA temp_store=FILE;
1214 CREATE TEMP TABLE temp_store_directory_test(a integer);
1215 INSERT INTO temp_store_directory_test values (2);
1216 SELECT * FROM temp_store_directory_test;
1217 }
1218 } {2}
1219 do_test pragma-9.10 {
1220 catchsql "
1221 PRAGMA temp_store_directory='$pwd';
1222 SELECT * FROM temp_store_directory_test;
1223 "
1224 } {1 {no such table: temp_store_directory_test}}
1225 }
tpoindex9a09a3c2004-12-20 19:01:32 +00001226 }
drh78f82d12008-09-02 00:52:52 +00001227}
danielk197795b289b2007-03-30 17:11:12 +00001228do_test pragma-9.11 {
1229 execsql {
1230 PRAGMA temp_store = 0;
1231 PRAGMA temp_store;
1232 }
1233} {0}
1234do_test pragma-9.12 {
1235 execsql {
1236 PRAGMA temp_store = 1;
1237 PRAGMA temp_store;
1238 }
1239} {1}
1240do_test pragma-9.13 {
1241 execsql {
1242 PRAGMA temp_store = 2;
1243 PRAGMA temp_store;
1244 }
1245} {2}
1246do_test pragma-9.14 {
1247 execsql {
1248 PRAGMA temp_store = 3;
1249 PRAGMA temp_store;
1250 }
1251} {0}
danielk197795b289b2007-03-30 17:11:12 +00001252do_test pragma-9.15 {
1253 catchsql {
1254 BEGIN EXCLUSIVE;
1255 CREATE TEMP TABLE temp_table(t);
1256 INSERT INTO temp_table VALUES('valuable data');
1257 PRAGMA temp_store = 1;
1258 }
1259} {1 {temporary storage cannot be changed from within a transaction}}
1260do_test pragma-9.16 {
1261 execsql {
1262 SELECT * FROM temp_table;
1263 COMMIT;
1264 }
1265} {{valuable data}}
danielk1977983e2302008-07-08 07:35:51 +00001266
1267do_test pragma-9.17 {
1268 execsql {
1269 INSERT INTO temp_table VALUES('valuable data II');
1270 SELECT * FROM temp_table;
1271 }
1272} {{valuable data} {valuable data II}}
1273
1274do_test pragma-9.18 {
1275 set rc [catch {
1276 db eval {SELECT t FROM temp_table} {
1277 execsql {pragma temp_store = 1}
1278 }
1279 } msg]
1280 list $rc $msg
1281} {1 {temporary storage cannot be changed from within a transaction}}
1282
drh268283b2005-01-08 15:44:25 +00001283} ;# ifcapable pager_pragmas
tpoindex9a09a3c2004-12-20 19:01:32 +00001284
danielk1977cc6bd382005-01-10 02:48:49 +00001285ifcapable trigger {
1286
1287do_test pragma-10.0 {
1288 catchsql {
1289 DROP TABLE main.t1;
1290 }
1291 execsql {
1292 PRAGMA count_changes = 1;
1293
1294 CREATE TABLE t1(a PRIMARY KEY);
1295 CREATE TABLE t1_mirror(a);
1296 CREATE TABLE t1_mirror2(a);
1297 CREATE TRIGGER t1_bi BEFORE INSERT ON t1 BEGIN
1298 INSERT INTO t1_mirror VALUES(new.a);
1299 END;
1300 CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
1301 INSERT INTO t1_mirror2 VALUES(new.a);
1302 END;
1303 CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
1304 UPDATE t1_mirror SET a = new.a WHERE a = old.a;
1305 END;
1306 CREATE TRIGGER t1_au AFTER UPDATE ON t1 BEGIN
1307 UPDATE t1_mirror2 SET a = new.a WHERE a = old.a;
1308 END;
1309 CREATE TRIGGER t1_bd BEFORE DELETE ON t1 BEGIN
1310 DELETE FROM t1_mirror WHERE a = old.a;
1311 END;
1312 CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
1313 DELETE FROM t1_mirror2 WHERE a = old.a;
1314 END;
1315 }
1316} {}
1317
1318do_test pragma-10.1 {
1319 execsql {
1320 INSERT INTO t1 VALUES(randstr(10,10));
1321 }
1322} {1}
1323do_test pragma-10.2 {
1324 execsql {
1325 UPDATE t1 SET a = randstr(10,10);
1326 }
1327} {1}
1328do_test pragma-10.3 {
1329 execsql {
1330 DELETE FROM t1;
1331 }
1332} {1}
1333
1334} ;# ifcapable trigger
1335
danielk197748af65a2005-02-09 03:20:37 +00001336ifcapable schema_pragmas {
1337 do_test pragma-11.1 {
1338 execsql2 {
1339 pragma collation_list;
1340 }
drh2d823312014-11-20 23:11:30 +00001341 } {seq 0 name RTRIM seq 1 name NOCASE seq 2 name BINARY}
danielk197748af65a2005-02-09 03:20:37 +00001342 do_test pragma-11.2 {
1343 db collate New_Collation blah...
1344 execsql {
1345 pragma collation_list;
1346 }
drh2d823312014-11-20 23:11:30 +00001347 } {0 New_Collation 1 RTRIM 2 NOCASE 3 BINARY}
danielk197748af65a2005-02-09 03:20:37 +00001348}
1349
danielk1977ddfb2f02006-02-17 12:25:14 +00001350ifcapable schema_pragmas&&tempdb {
1351 do_test pragma-12.1 {
1352 sqlite3 db2 test.db
1353 execsql {
1354 PRAGMA temp.table_info('abc');
1355 } db2
1356 } {}
1357 db2 close
1358
1359 do_test pragma-12.2 {
1360 sqlite3 db2 test.db
1361 execsql {
1362 PRAGMA temp.default_cache_size = 200;
1363 PRAGMA temp.default_cache_size;
1364 } db2
1365 } {200}
1366 db2 close
1367
1368 do_test pragma-12.3 {
1369 sqlite3 db2 test.db
1370 execsql {
1371 PRAGMA temp.cache_size = 400;
1372 PRAGMA temp.cache_size;
1373 } db2
1374 } {400}
1375 db2 close
1376}
1377
danielk19774b2688a2006-06-20 11:01:07 +00001378ifcapable bloblit {
1379
drh05a82982006-03-19 13:00:25 +00001380do_test pragma-13.1 {
1381 execsql {
1382 DROP TABLE IF EXISTS t4;
1383 PRAGMA vdbe_trace=on;
1384 PRAGMA vdbe_listing=on;
1385 PRAGMA sql_trace=on;
1386 CREATE TABLE t4(a INTEGER PRIMARY KEY,b);
1387 INSERT INTO t4(b) VALUES(x'0123456789abcdef0123456789abcdef0123456789');
1388 INSERT INTO t4(b) VALUES(randstr(30,30));
1389 INSERT INTO t4(b) VALUES(1.23456);
1390 INSERT INTO t4(b) VALUES(NULL);
1391 INSERT INTO t4(b) VALUES(0);
1392 INSERT INTO t4(b) SELECT b||b||b||b FROM t4;
1393 SELECT * FROM t4;
1394 }
1395 execsql {
1396 PRAGMA vdbe_trace=off;
1397 PRAGMA vdbe_listing=off;
1398 PRAGMA sql_trace=off;
1399 }
1400} {}
1401
danielk19774b2688a2006-06-20 11:01:07 +00001402} ;# ifcapable bloblit
1403
danielk197759a93792008-05-15 17:48:20 +00001404ifcapable pager_pragmas {
1405 db close
mistachkinfda06be2011-08-02 00:57:34 +00001406 forcedelete test.db
danielk197759a93792008-05-15 17:48:20 +00001407 sqlite3 db test.db
drh51a74d42015-02-28 01:04:27 +00001408
drhb3366b92015-09-11 20:54:44 +00001409 # EVIDENCE-OF: R-15672-33611 PRAGMA schema.page_count; Return the total
1410 # number of pages in the database file.
drh51a74d42015-02-28 01:04:27 +00001411 #
danielk197759a93792008-05-15 17:48:20 +00001412 do_test pragma-14.1 {
1413 execsql { pragma auto_vacuum = 0 }
drh51a74d42015-02-28 01:04:27 +00001414 execsql { pragma page_count; pragma main.page_count }
1415 } {0 0}
danielk197759a93792008-05-15 17:48:20 +00001416
1417 do_test pragma-14.2 {
1418 execsql {
1419 CREATE TABLE abc(a, b, c);
1420 PRAGMA page_count;
drh51a74d42015-02-28 01:04:27 +00001421 PRAGMA main.page_count;
1422 PRAGMA temp.page_count;
danielk197759a93792008-05-15 17:48:20 +00001423 }
drh51a74d42015-02-28 01:04:27 +00001424 } {2 2 0}
drh5d16a9a2011-10-13 14:41:22 +00001425 do_test pragma-14.2uc {
1426 execsql {pragma PAGE_COUNT}
1427 } {2}
danielk197759a93792008-05-15 17:48:20 +00001428
1429 do_test pragma-14.3 {
1430 execsql {
1431 BEGIN;
1432 CREATE TABLE def(a, b, c);
1433 PRAGMA page_count;
1434 }
1435 } {3}
drh5d16a9a2011-10-13 14:41:22 +00001436 do_test pragma-14.3uc {
1437 execsql {pragma PAGE_COUNT}
1438 } {3}
danielk197759a93792008-05-15 17:48:20 +00001439
1440 do_test pragma-14.4 {
1441 set page_size [db one {pragma page_size}]
1442 expr [file size test.db] / $page_size
1443 } {2}
1444
1445 do_test pragma-14.5 {
1446 execsql {
1447 ROLLBACK;
1448 PRAGMA page_count;
1449 }
1450 } {2}
1451
1452 do_test pragma-14.6 {
mistachkinfda06be2011-08-02 00:57:34 +00001453 forcedelete test2.db
danielk197759a93792008-05-15 17:48:20 +00001454 sqlite3 db2 test2.db
1455 execsql {
1456 PRAGMA auto_vacuum = 0;
1457 CREATE TABLE t1(a, b, c);
1458 CREATE TABLE t2(a, b, c);
1459 CREATE TABLE t3(a, b, c);
1460 CREATE TABLE t4(a, b, c);
1461 } db2
1462 db2 close
1463 execsql {
1464 ATTACH 'test2.db' AS aux;
1465 PRAGMA aux.page_count;
1466 }
1467 } {5}
drh5d16a9a2011-10-13 14:41:22 +00001468 do_test pragma-14.6uc {
1469 execsql {pragma AUX.PAGE_COUNT}
1470 } {5}
danielk197759a93792008-05-15 17:48:20 +00001471}
1472
danielk19778cf6c552008-06-23 16:53:46 +00001473# Test that the value set using the cache_size pragma is not reset when the
1474# schema is reloaded.
1475#
1476ifcapable pager_pragmas {
1477 db close
1478 sqlite3 db test.db
1479 do_test pragma-15.1 {
1480 execsql {
1481 PRAGMA cache_size=59;
1482 PRAGMA cache_size;
1483 }
1484 } {59}
1485 do_test pragma-15.2 {
1486 sqlite3 db2 test.db
1487 execsql {
1488 CREATE TABLE newtable(a, b, c);
1489 } db2
1490 db2 close
1491 } {}
1492 do_test pragma-15.3 {
1493 # Evaluating this statement will cause the schema to be reloaded (because
1494 # the schema was changed by another connection in pragma-15.2). At one
1495 # point there was a bug that reset the cache_size to its default value
1496 # when this happened.
1497 execsql { SELECT * FROM sqlite_master }
1498 execsql { PRAGMA cache_size }
1499 } {59}
1500}
1501
danielk19775558a8a2005-01-17 07:53:44 +00001502# Reset the sqlite3_temp_directory variable for the next run of tests:
1503sqlite3 dbX :memory:
1504dbX eval {PRAGMA temp_store_directory = ""}
1505dbX close
1506
danielk1977838cce42009-01-12 14:01:45 +00001507ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
aswiftaebf4132008-11-21 00:10:35 +00001508 set sqlite_hostid_num 1
1509
1510 set using_proxy 0
1511 foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
1512 set using_proxy $value
1513 }
1514
1515 # Test the lock_proxy_file pragmas.
1516 #
1517 db close
1518 set env(SQLITE_FORCE_PROXY_LOCKING) "0"
1519
1520 sqlite3 db test.db
1521 do_test pragma-16.1 {
1522 execsql {
1523 PRAGMA lock_proxy_file="mylittleproxy";
1524 select * from sqlite_master;
1525 }
1526 execsql {
1527 PRAGMA lock_proxy_file;
1528 }
1529 } {mylittleproxy}
1530
1531 do_test pragma-16.2 {
1532 sqlite3 db2 test.db
1533 execsql {
1534 PRAGMA lock_proxy_file="mylittleproxy";
1535 } db2
1536 } {}
1537
1538 db2 close
1539 do_test pragma-16.2.1 {
1540 sqlite3 db2 test.db
1541 execsql {
1542 PRAGMA lock_proxy_file=":auto:";
1543 select * from sqlite_master;
1544 } db2
1545 execsql {
1546 PRAGMA lock_proxy_file;
1547 } db2
1548 } {mylittleproxy}
1549
1550 db2 close
1551 do_test pragma-16.3 {
1552 sqlite3 db2 test.db
1553 execsql {
1554 PRAGMA lock_proxy_file="myotherproxy";
1555 } db2
1556 catchsql {
1557 select * from sqlite_master;
1558 } db2
1559 } {1 {database is locked}}
1560
1561 do_test pragma-16.4 {
1562 db2 close
1563 db close
1564 sqlite3 db2 test.db
1565 execsql {
1566 PRAGMA lock_proxy_file="myoriginalproxy";
1567 PRAGMA lock_proxy_file="myotherproxy";
1568 PRAGMA lock_proxy_file;
1569 } db2
1570 } {myotherproxy}
1571
1572 db2 close
1573 set env(SQLITE_FORCE_PROXY_LOCKING) "1"
1574 do_test pragma-16.5 {
1575 sqlite3 db2 test.db
1576 execsql {
1577 PRAGMA lock_proxy_file=":auto:";
1578 PRAGMA lock_proxy_file;
1579 } db2
1580 } {myotherproxy}
1581
1582 do_test pragma-16.6 {
1583 db2 close
1584 sqlite3 db2 test2.db
1585 set lockpath [execsql {
1586 PRAGMA lock_proxy_file=":auto:";
1587 PRAGMA lock_proxy_file;
1588 } db2]
1589 string match "*test2.db:auto:" $lockpath
1590 } {1}
1591
1592 set sqlite_hostid_num 2
1593 do_test pragma-16.7 {
dan14d14602010-10-06 16:42:52 +00001594 list [catch {
1595 sqlite3 db test2.db
1596 execsql {
1597 PRAGMA lock_proxy_file=":auto:";
1598 select * from sqlite_master;
1599 }
1600 } msg] $msg
aswiftaebf4132008-11-21 00:10:35 +00001601 } {1 {database is locked}}
1602 db close
1603
1604 do_test pragma-16.8 {
dan14d14602010-10-06 16:42:52 +00001605 list [catch {
1606 sqlite3 db test2.db
1607 execsql { select * from sqlite_master }
1608 } msg] $msg
aswiftaebf4132008-11-21 00:10:35 +00001609 } {1 {database is locked}}
1610
1611 db2 close
1612 do_test pragma-16.8.1 {
1613 execsql {
1614 PRAGMA lock_proxy_file="yetanotherproxy";
1615 PRAGMA lock_proxy_file;
1616 }
1617 } {yetanotherproxy}
1618 do_test pragma-16.8.2 {
1619 execsql {
1620 create table mine(x);
1621 }
1622 } {}
1623
1624 db close
1625 do_test pragma-16.9 {
1626 sqlite3 db proxytest.db
1627 set lockpath2 [execsql {
1628 PRAGMA lock_proxy_file=":auto:";
1629 PRAGMA lock_proxy_file;
1630 } db]
1631 string match "*proxytest.db:auto:" $lockpath2
1632 } {1}
1633
1634 set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
1635 set sqlite_hostid_num 0
1636}
drhd2cb50b2009-01-09 21:41:17 +00001637
1638# Parsing of auto_vacuum settings.
1639#
1640foreach {autovac_setting val} {
1641 0 0
1642 1 1
1643 2 2
1644 3 0
1645 -1 0
1646 none 0
1647 NONE 0
1648 NoNe 0
1649 full 1
1650 FULL 1
1651 incremental 2
1652 INCREMENTAL 2
1653 -1234 0
1654 1234 0
1655} {
1656 do_test pragma-17.1.$autovac_setting {
1657 catch {db close}
1658 sqlite3 db :memory:
1659 execsql "
1660 PRAGMA auto_vacuum=$::autovac_setting;
1661 PRAGMA auto_vacuum;
1662 "
1663 } $val
1664}
1665
1666# Parsing of temp_store settings.
1667#
1668foreach {temp_setting val} {
1669 0 0
1670 1 1
1671 2 2
1672 3 0
1673 -1 0
1674 file 1
1675 FILE 1
1676 fIlE 1
1677 memory 2
1678 MEMORY 2
1679 MeMoRy 2
1680} {
1681 do_test pragma-18.1.$temp_setting {
1682 catch {db close}
1683 sqlite3 db :memory:
1684 execsql "
1685 PRAGMA temp_store=$::temp_setting;
1686 PRAGMA temp_store=$::temp_setting;
1687 PRAGMA temp_store;
1688 "
1689 } $val
1690}
1691
drh92c700d2012-02-22 19:56:17 +00001692# The SQLITE_FCNTL_PRAGMA logic, with error handling.
1693#
1694db close
1695testvfs tvfs
1696sqlite3 db test.db -vfs tvfs
1697do_test pragma-19.1 {
1698 catchsql {PRAGMA error}
drha690ff32017-07-07 19:43:23 +00001699} {1 {SQL logic error}}
drh92c700d2012-02-22 19:56:17 +00001700do_test pragma-19.2 {
1701 catchsql {PRAGMA error='This is the error message'}
1702} {1 {This is the error message}}
1703do_test pragma-19.3 {
1704 catchsql {PRAGMA error='7 This is the error message'}
1705} {1 {This is the error message}}
1706do_test pragma-19.4 {
1707 catchsql {PRAGMA error=7}
1708} {1 {out of memory}}
drhc8517f62012-02-22 20:08:49 +00001709do_test pragma-19.5 {
mistachkin5b044542012-03-02 22:41:06 +00001710 file tail [lindex [execsql {PRAGMA filename}] 0]
drhc8517f62012-02-22 20:08:49 +00001711} {test.db}
drh92c700d2012-02-22 19:56:17 +00001712
drhcc716452012-06-06 23:23:23 +00001713if {$tcl_platform(platform)=="windows"} {
mistachkina112d142012-03-14 00:44:01 +00001714# Test data_store_directory pragma
1715#
1716db close
1717sqlite3 db test.db
1718file mkdir data_dir
1719do_test pragma-20.1 {
1720 catchsql {PRAGMA data_store_directory}
1721} {0 {}}
1722do_test pragma-20.2 {
1723 set pwd [string map {' ''} [file nativename [get_pwd]]]
1724 catchsql "PRAGMA data_store_directory='$pwd';"
1725} {0 {}}
1726do_test pragma-20.3 {
1727 catchsql {PRAGMA data_store_directory}
1728} [list 0 [list [file nativename [get_pwd]]]]
1729do_test pragma-20.4 {
1730 set pwd [string map {' ''} [file nativename \
1731 [file join [get_pwd] data_dir]]]
1732 catchsql "PRAGMA data_store_directory='$pwd';"
1733} {0 {}}
1734do_test pragma-20.5 {
1735 sqlite3 db2 test2.db
1736 catchsql "PRAGMA database_list;" db2
1737} [list 0 [list 0 main [file nativename \
1738 [file join [get_pwd] data_dir test2.db]]]]
1739catch {db2 close}
1740do_test pragma-20.6 {
1741 sqlite3 db2 [file join [get_pwd] test2.db]
1742 catchsql "PRAGMA database_list;" db2
1743} [list 0 [list 0 main [file nativename \
1744 [file join [get_pwd] test2.db]]]]
1745catch {db2 close}
1746do_test pragma-20.7 {
1747 catchsql "PRAGMA data_store_directory='';"
1748} {0 {}}
1749do_test pragma-20.8 {
1750 catchsql {PRAGMA data_store_directory}
1751} {0 {}}
drh92c700d2012-02-22 19:56:17 +00001752
mistachkina112d142012-03-14 00:44:01 +00001753forcedelete data_dir
drhcc716452012-06-06 23:23:23 +00001754} ;# endif windows
drhcd61c282002-03-06 22:01:34 +00001755
dan1fed5da2014-02-25 21:01:25 +00001756database_may_be_corrupt
drh7da56b42016-03-14 18:34:42 +00001757if {![nonzero_reserved_bytes]} {
dan1fed5da2014-02-25 21:01:25 +00001758
drh7da56b42016-03-14 18:34:42 +00001759 do_test 21.1 {
1760 # Create a corrupt database in testerr.db. And a non-corrupt at test.db.
1761 #
1762 db close
1763 forcedelete test.db
1764 sqlite3 db test.db
1765 execsql {
1766 PRAGMA page_size = 1024;
1767 PRAGMA auto_vacuum = 0;
1768 CREATE TABLE t1(a PRIMARY KEY, b);
1769 INSERT INTO t1 VALUES(1, 1);
1770 }
1771 for {set i 0} {$i < 10} {incr i} {
1772 execsql { INSERT INTO t1 SELECT a + (1 << $i), b + (1 << $i) FROM t1 }
1773 }
1774 db close
1775 forcecopy test.db testerr.db
1776 hexio_write testerr.db 15000 [string repeat 55 100]
1777 } {100}
1778
1779 set mainerr {*** in database main ***
dan5885e762012-07-16 10:06:12 +00001780Multiple uses for byte 672 of page 15}
drh7da56b42016-03-14 18:34:42 +00001781 set auxerr {*** in database aux ***
dan5885e762012-07-16 10:06:12 +00001782Multiple uses for byte 672 of page 15}
drh7da56b42016-03-14 18:34:42 +00001783
1784 set mainerr {/{\*\*\* in database main \*\*\*
dan597515d2014-02-28 18:39:51 +00001785Multiple uses for byte 672 of page 15}.*/}
drh7da56b42016-03-14 18:34:42 +00001786 set auxerr {/{\*\*\* in database aux \*\*\*
dan597515d2014-02-28 18:39:51 +00001787Multiple uses for byte 672 of page 15}.*/}
drh7da56b42016-03-14 18:34:42 +00001788
1789 do_test 22.2 {
1790 catch { db close }
1791 sqlite3 db testerr.db
1792 execsql { PRAGMA integrity_check }
1793 } $mainerr
1794
1795 do_test 22.3.1 {
1796 catch { db close }
1797 sqlite3 db test.db
1798 execsql {
1799 ATTACH 'testerr.db' AS 'aux';
1800 PRAGMA integrity_check;
1801 }
1802 } $auxerr
1803 do_test 22.3.2 {
1804 execsql { PRAGMA main.integrity_check; }
1805 } {ok}
1806 do_test 22.3.3 {
1807 execsql { PRAGMA aux.integrity_check; }
1808 } $auxerr
1809
1810 do_test 22.4.1 {
1811 catch { db close }
1812 sqlite3 db testerr.db
1813 execsql {
1814 ATTACH 'test.db' AS 'aux';
1815 PRAGMA integrity_check;
1816 }
1817 } $mainerr
1818 do_test 22.4.2 {
1819 execsql { PRAGMA main.integrity_check; }
1820 } $mainerr
1821 do_test 22.4.3 {
1822 execsql { PRAGMA aux.integrity_check; }
1823 } {ok}
1824}
1825
drhc95e01d2013-02-14 16:16:05 +00001826db close
1827forcedelete test.db test.db-wal test.db-journal
1828sqlite3 db test.db
1829sqlite3 db2 test.db
1830do_test 23.1 {
1831 db eval {
1832 CREATE TABLE t1(a INTEGER PRIMARY KEY,b,c,d);
1833 CREATE INDEX i1 ON t1(b,c);
1834 CREATE INDEX i2 ON t1(c,d);
drh7be0fd92015-03-05 15:34:15 +00001835 CREATE INDEX i2x ON t1(d COLLATE nocase, c DESC);
drhc95e01d2013-02-14 16:16:05 +00001836 CREATE TABLE t2(x INTEGER REFERENCES t1);
1837 }
1838 db2 eval {SELECT name FROM sqlite_master}
drh7be0fd92015-03-05 15:34:15 +00001839} {t1 i1 i2 i2x t2}
drhc228be52015-01-31 02:00:01 +00001840do_test 23.2a {
drhc95e01d2013-02-14 16:16:05 +00001841 db eval {
1842 DROP INDEX i2;
1843 CREATE INDEX i2 ON t1(c,d,b);
1844 }
drhc228be52015-01-31 02:00:01 +00001845 capture_pragma db2 out {PRAGMA index_info(i2)}
drh5e7028c2015-03-05 14:29:02 +00001846 db2 eval {SELECT cid, name, '|' FROM out ORDER BY seqno}
1847} {2 c | 3 d | 1 b |}
drh7be0fd92015-03-05 15:34:15 +00001848
drhb3366b92015-09-11 20:54:44 +00001849# EVIDENCE-OF: R-56143-29319 PRAGMA schema.index_xinfo(index-name); This
1850# pragma returns information about every column in an index.
drh7be0fd92015-03-05 15:34:15 +00001851#
1852# EVIDENCE-OF: R-45970-35618 Unlike this index_info pragma, this pragma
1853# returns information about every column in the index, not just the key
1854# columns.
1855#
drhc228be52015-01-31 02:00:01 +00001856do_test 23.2b {
1857 capture_pragma db2 out {PRAGMA index_xinfo(i2)}
1858 db2 eval {SELECT cid, name, "desc", coll, "key", '|' FROM out ORDER BY seqno}
1859} {2 c 0 BINARY 1 | 3 d 0 BINARY 1 | 1 b 0 BINARY 1 | -1 {} 0 BINARY 0 |}
drh7be0fd92015-03-05 15:34:15 +00001860
1861# (The first column of output from PRAGMA index_xinfo is...)
1862# EVIDENCE-OF: R-00197-14279 The rank of the column within the index. (0
1863# means left-most. Key columns come before auxiliary columns.)
1864#
1865# (The second column of output from PRAGMA index_xinfo is...)
1866# EVIDENCE-OF: R-40889-06838 The rank of the column within the table
1867# being indexed, or -1 if the index-column is the rowid of the table
1868# being indexed.
1869#
1870# (The third column of output from PRAGMA index_xinfo is...)
1871# EVIDENCE-OF: R-22751-28901 The name of the column being indexed, or
1872# NULL if the index-column is the rowid of the table being indexed.
1873#
1874# (The fourth column of output from PRAGMA index_xinfo is...)
1875# EVIDENCE-OF: R-11847-09179 1 if the index-column is sorted in reverse
1876# (DESC) order by the index and 0 otherwise.
1877#
1878# (The fifth column of output from PRAGMA index_xinfo is...)
1879# EVIDENCE-OF: R-15313-19540 The name for the collating sequence used to
1880# compare values in the index-column.
1881#
1882# (The sixth column of output from PRAGMA index_xinfo is...)
1883# EVIDENCE-OF: R-14310-64553 1 if the index-column is a key column and 0
1884# if the index-column is an auxiliary column.
1885#
1886do_test 23.2c {
1887 db2 eval {PRAGMA index_xinfo(i2)}
1888} {0 2 c 0 BINARY 1 1 3 d 0 BINARY 1 2 1 b 0 BINARY 1 3 -1 {} 0 BINARY 0}
1889do_test 23.2d {
1890 db2 eval {PRAGMA index_xinfo(i2x)}
1891} {0 3 d 0 nocase 1 1 2 c 1 BINARY 1 2 -1 {} 0 BINARY 0}
1892
drhb3366b92015-09-11 20:54:44 +00001893# EVIDENCE-OF: R-64103-17776 PRAGMA schema.index_list(table-name); This
1894# pragma returns one row for each index associated with the given table.
drh7be0fd92015-03-05 15:34:15 +00001895#
1896# (The first column of output from PRAGMA index_list is...)
1897# EVIDENCE-OF: R-02753-24748 A sequence number assigned to each index
1898# for internal tracking purposes.
1899#
1900# (The second column of output from PRAGMA index_list is...)
1901# EVIDENCE-OF: R-35496-03635 The name of the index.
1902#
1903# (The third column of output from PRAGMA index_list is...)
1904# EVIDENCE-OF: R-57301-64506 "1" if the index is UNIQUE and "0" if not.
1905#
1906# (The fourth column of output from PRAGMA index_list is...)
1907# EVIDENCE-OF: R-36609-39554 "c" if the index was created by a CREATE
1908# INDEX statement, "u" if the index was created by a UNIQUE constraint,
1909# or "pk" if the index was created by a PRIMARY KEY constraint.
1910#
drhc95e01d2013-02-14 16:16:05 +00001911do_test 23.3 {
1912 db eval {
1913 CREATE INDEX i3 ON t1(d,b,c);
1914 }
drhc228be52015-01-31 02:00:01 +00001915 capture_pragma db2 out {PRAGMA index_list(t1)}
drh7be0fd92015-03-05 15:34:15 +00001916 db2 eval {SELECT seq, name, "unique", origin, '|' FROM out ORDER BY seq}
1917} {0 i3 0 c | 1 i2 0 c | 2 i2x 0 c | 3 i1 0 c |}
drhc95e01d2013-02-14 16:16:05 +00001918do_test 23.4 {
1919 db eval {
1920 ALTER TABLE t1 ADD COLUMN e;
1921 }
1922 db2 eval {
1923 PRAGMA table_info(t1);
1924 }
1925} {/4 e {} 0 {} 0/}
1926do_test 23.5 {
1927 db eval {
1928 DROP TABLE t2;
1929 CREATE TABLE t2(x, y INTEGER REFERENCES t1);
1930 }
1931 db2 eval {
1932 PRAGMA foreign_key_list(t2);
1933 }
1934} {0 0 t1 y {} {NO ACTION} {NO ACTION} NONE}
drh3e1e14d2017-09-12 00:24:45 +00001935db2 close
drhc95e01d2013-02-14 16:16:05 +00001936
drhefeaec32017-10-23 16:34:07 +00001937ifcapable !has_codec {
1938 reset_db
1939 do_execsql_test 24.0 {
1940 PRAGMA page_size = 1024;
1941 CREATE TABLE t1(a, b, c);
1942 CREATE INDEX i1 ON t1(b);
1943 INSERT INTO t1 VALUES('a', 'b', 'c');
1944 PRAGMA integrity_check;
1945 } {ok}
1946
1947 set r [db one {SELECT rootpage FROM sqlite_master WHERE name = 't1'}]
1948 db close
1949 hexio_write test.db [expr $r*1024 - 16] 000000000000000701040f0f1f616263
1950
1951 sqlite3 db test.db
1952 do_catchsql_test 24.1 {
1953 SELECT * FROM t1;
1954 } {1 {database disk image is malformed}}
1955 do_catchsql_test 24.2 {
1956 PRAGMA integrity_check;
1957 } {0 {{database disk image is malformed}}}
1958}
dan1fed5da2014-02-25 21:01:25 +00001959database_never_corrupt
drhcd61c282002-03-06 22:01:34 +00001960finish_test