blob: 0a8c4dc11f00a384c3c21cc83085ce782d6f2b93 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
drhdc2c4912009-02-04 22:46:47 +000014# $Id: tester.tcl,v 1.138 2009/02/04 22:46:47 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
drh8359d8c2008-03-29 11:00:54 +000016#
17# What for user input before continuing. This gives an opportunity
18# to connect profiling tools to the process.
19#
20for {set i 0} {$i<[llength $argv]} {incr i} {
21 if {[regexp {^-+pause$} [lindex $argv $i] all value]} {
22 puts -nonewline "Press RETURN to begin..."
23 flush stdout
24 gets stdin
25 set argv [lreplace $argv $i $i]
26 }
27}
drh348784e2000-05-29 20:41:49 +000028
drh92febd92004-08-20 18:34:20 +000029set tcl_precision 15
drh49285702005-09-17 15:20:26 +000030set sqlite_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000031
drh3a7fb7c2007-08-10 16:41:08 +000032#
33# Check the command-line arguments for a default soft-heap-limit.
34# Store this default value in the global variable ::soft_limit and
35# update the soft-heap-limit each time this script is run. In that
36# way if an individual test file changes the soft-heap-limit, it
37# will be reset at the start of the next test file.
38#
39if {![info exists soft_limit]} {
40 set soft_limit 0
41 for {set i 0} {$i<[llength $argv]} {incr i} {
42 if {[regexp {^--soft-heap-limit=(.+)$} [lindex $argv $i] all value]} {
43 if {$value!="off"} {
44 set soft_limit $value
45 }
46 set argv [lreplace $argv $i $i]
47 }
48 }
49}
50sqlite3_soft_heap_limit $soft_limit
51
drh4a50aac2007-08-23 02:47:53 +000052#
53# Check the command-line arguments to set the memory debugger
54# backtrace depth.
55#
56# See the sqlite3_memdebug_backtrace() function in mem2.c or
57# test_malloc.c for additional information.
58#
59for {set i 0} {$i<[llength $argv]} {incr i} {
danielk197735754ac2008-03-21 17:29:37 +000060 if {[lindex $argv $i] eq "--malloctrace"} {
61 set argv [lreplace $argv $i $i]
danielk19775f096132008-03-28 15:44:09 +000062 sqlite3_memdebug_backtrace 10
danielk197735754ac2008-03-21 17:29:37 +000063 sqlite3_memdebug_log start
danielk197735754ac2008-03-21 17:29:37 +000064 set tester_do_malloctrace 1
65 }
66}
67for {set i 0} {$i<[llength $argv]} {incr i} {
drh4a50aac2007-08-23 02:47:53 +000068 if {[regexp {^--backtrace=(\d+)$} [lindex $argv $i] all value]} {
69 sqlite3_memdebug_backtrace $value
70 set argv [lreplace $argv $i $i]
71 }
72}
73
danielk19771e21fd52008-04-10 17:27:38 +000074
75proc ostrace_call {zCall nClick zFile i32 i64} {
danielk197727467042008-05-12 07:42:20 +000076 set s "INSERT INTO ostrace VALUES('$zCall', $nClick, '$zFile', $i32, $i64);"
danielk19771e21fd52008-04-10 17:27:38 +000077 puts $::ostrace_fd $s
78}
79
80for {set i 0} {$i<[llength $argv]} {incr i} {
81 if {[lindex $argv $i] eq "--ossummary" || [lindex $argv $i] eq "--ostrace"} {
82 sqlite3_instvfs create -default ostrace
83 set tester_do_ostrace 1
84 set ostrace_fd [open ostrace.sql w]
85 puts $ostrace_fd "BEGIN;"
86 if {[lindex $argv $i] eq "--ostrace"} {
87 set s "CREATE TABLE ostrace"
88 append s "(method TEXT, clicks INT, file TEXT, i32 INT, i64 INT);"
89 puts $ostrace_fd $s
90 sqlite3_instvfs configure ostrace ostrace_call
danielk1977374177e2008-05-08 15:58:06 +000091 sqlite3_instvfs configure ostrace ostrace_call
danielk19771e21fd52008-04-10 17:27:38 +000092 }
93 set argv [lreplace $argv $i $i]
94 }
danielk1977374177e2008-05-08 15:58:06 +000095 if {[lindex $argv $i] eq "--binarylog"} {
96 set tester_do_binarylog 1
danielk1977374177e2008-05-08 15:58:06 +000097 set argv [lreplace $argv $i $i]
98 }
danielk19771e21fd52008-04-10 17:27:38 +000099}
100
drh6a288a32008-01-07 19:20:24 +0000101#
102# Check the command-line arguments to set the maximum number of
103# errors tolerated before halting.
104#
105if {![info exists maxErr]} {
106 set maxErr 1000
107}
108for {set i 0} {$i<[llength $argv]} {incr i} {
109 if {[regexp {^--maxerror=(\d+)$} [lindex $argv $i] all maxErr]} {
110 set argv [lreplace $argv $i $i]
111 }
112}
113#puts "Max error = $maxErr"
114
drh4a50aac2007-08-23 02:47:53 +0000115
drh9eb9e262004-02-11 02:18:05 +0000116# Use the pager codec if it is available
117#
drhef4ac8f2004-06-19 00:16:31 +0000118if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
119 rename sqlite3 sqlite_orig
120 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +0000121 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
122 lappend args -key {xyzzy}
123 }
124 uplevel 1 sqlite_orig $args
125 }
126}
127
128
drhbec2bf42000-05-29 23:48:22 +0000129# Create a test database
130#
danielk1977f7b9d662008-06-23 18:49:43 +0000131if {![info exists nTest]} {
132 sqlite3_shutdown
133 install_malloc_faultsim 1
134 sqlite3_initialize
danielk1977185eac92008-07-12 15:55:54 +0000135 if {[info exists tester_do_binarylog]} {
136 sqlite3_instvfs binarylog -default binarylog ostrace.bin
137 sqlite3_instvfs marker binarylog "$argv0 $argv"
138 }
danielk1977f7b9d662008-06-23 18:49:43 +0000139}
danielk197703ba3fa2009-01-09 10:49:14 +0000140
141proc reset_db {} {
142 catch {db close}
143 file delete -force test.db
144 file delete -force test.db-journal
145 sqlite3 db ./test.db
146 set ::DB [sqlite3_connection_pointer db]
147 if {[info exists ::SETUP_SQL]} {
148 db eval $::SETUP_SQL
149 }
drhcd61c282002-03-06 22:01:34 +0000150}
danielk197703ba3fa2009-01-09 10:49:14 +0000151reset_db
drhbec2bf42000-05-29 23:48:22 +0000152
153# Abort early if this script has been run before.
154#
155if {[info exists nTest]} return
156
157# Set the test counters to zero
158#
drh348784e2000-05-29 20:41:49 +0000159set nErr 0
160set nTest 0
drh767c2002000-10-19 14:10:08 +0000161set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000162set failList {}
drh521cc842008-04-15 02:36:33 +0000163set omitList {}
drhb62c3352006-11-23 09:39:16 +0000164if {![info exists speedTest]} {
165 set speedTest 0
166}
drh348784e2000-05-29 20:41:49 +0000167
drh521cc842008-04-15 02:36:33 +0000168# Record the fact that a sequence of tests were omitted.
169#
170proc omit_test {name reason} {
171 global omitList
172 lappend omitList [list $name $reason]
173}
174
drh348784e2000-05-29 20:41:49 +0000175# Invoke the do_test procedure to run a single test
176#
177proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000178 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000179 sqlite3_memdebug_settitle $name
danielk197706fb0402008-05-08 16:51:11 +0000180 if {[info exists ::tester_do_binarylog]} {
danielk1977374177e2008-05-08 15:58:06 +0000181 sqlite3_instvfs marker binarylog "Start of $name"
182 }
drh767c2002000-10-19 14:10:08 +0000183 if {$skip_test} {
184 set skip_test 0
185 return
186 }
187 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000188 set go 1
189 } else {
190 set go 0
191 foreach pattern $argv {
192 if {[string match $pattern $name]} {
193 set go 1
194 break
195 }
196 }
197 }
198 if {!$go} return
199 incr nTest
drh5edc3122001-09-13 21:53:09 +0000200 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000201 flush stdout
202 if {[catch {uplevel #0 "$cmd;\n"} result]} {
203 puts "\nError: $result"
204 incr nErr
drha1b351a2001-09-14 16:42:12 +0000205 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000206 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000207 } elseif {[string compare $result $expected]} {
208 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
209 incr nErr
drha1b351a2001-09-14 16:42:12 +0000210 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000211 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000212 } else {
213 puts " Ok"
214 }
drh6558db82007-04-13 03:23:21 +0000215 flush stdout
danielk197706fb0402008-05-08 16:51:11 +0000216 if {[info exists ::tester_do_binarylog]} {
danielk1977374177e2008-05-08 15:58:06 +0000217 sqlite3_instvfs marker binarylog "End of $name"
218 }
drh348784e2000-05-29 20:41:49 +0000219}
220
drhb62c3352006-11-23 09:39:16 +0000221# Run an SQL script.
222# Return the number of microseconds per statement.
223#
drh3590f152006-11-23 21:09:10 +0000224proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000225 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000226 flush stdout
227 set speed [time {sqlite3_exec_nr db $sql}]
228 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000229 if {$tm == 0} {
230 set rate [format %20s "many"]
231 } else {
232 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
233 }
drh3590f152006-11-23 21:09:10 +0000234 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000235 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000236 global total_time
237 set total_time [expr {$total_time+$tm}]
238}
drh45c236d2008-03-22 01:08:00 +0000239proc speed_trial_tcl {name numstmt units script} {
240 puts -nonewline [format {%-21.21s } $name...]
241 flush stdout
242 set speed [time {eval $script}]
243 set tm [lindex $speed 0]
244 if {$tm == 0} {
245 set rate [format %20s "many"]
246 } else {
247 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
248 }
249 set u2 $units/s
250 puts [format {%12d uS %s %s} $tm $rate $u2]
251 global total_time
252 set total_time [expr {$total_time+$tm}]
253}
drhdd924312007-03-31 22:34:16 +0000254proc speed_trial_init {name} {
255 global total_time
256 set total_time 0
257}
258proc speed_trial_summary {name} {
259 global total_time
260 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000261}
262
drh348784e2000-05-29 20:41:49 +0000263# Run this routine last
264#
265proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000266 finalize_testing
267}
268proc finalize_testing {} {
drh521cc842008-04-15 02:36:33 +0000269 global nTest nErr sqlite_open_file_count omitList
danielk19772e588c72005-12-09 14:25:08 +0000270
drh6e142f52000-06-08 13:36:40 +0000271 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000272 catch {db2 close}
273 catch {db3 close}
274
drh9bc54492007-10-23 14:49:59 +0000275 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000276 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000277 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000278 db close
drh984bfaa2008-03-19 16:08:53 +0000279 sqlite3_reset_auto_extension
drh3a7fb7c2007-08-10 16:41:08 +0000280 set heaplimit [sqlite3_soft_heap_limit]
281 if {$heaplimit!=$::soft_limit} {
282 puts "soft-heap-limit changed by this script\
283 from $::soft_limit to $heaplimit"
284 } elseif {$heaplimit!="" && $heaplimit>0} {
285 puts "soft-heap-limit set to $heaplimit"
286 }
287 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000288 incr nTest
drh348784e2000-05-29 20:41:49 +0000289 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000290 if {$nErr>0} {
291 puts "Failures on these tests: $::failList"
292 }
drh521cc842008-04-15 02:36:33 +0000293 if {[llength $omitList]>0} {
294 puts "Omitted test cases:"
295 set prec {}
296 foreach {rec} [lsort $omitList] {
297 if {$rec==$prec} continue
298 set prec $rec
299 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
300 }
301 }
drh80788d82006-09-02 14:50:23 +0000302 if {$nErr>0 && ![working_64bit_int]} {
303 puts "******************************************************************"
304 puts "N.B.: The version of TCL that you used to build this test harness"
305 puts "is defective in that it does not support 64-bit integers. Some or"
306 puts "all of the test failures above might be a result from this defect"
307 puts "in your TCL build."
308 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000309 }
danielk1977374177e2008-05-08 15:58:06 +0000310 if {[info exists ::tester_do_binarylog]} {
311 sqlite3_instvfs destroy binarylog
312 }
drh94e92032003-02-16 22:21:32 +0000313 if {$sqlite_open_file_count} {
314 puts "$sqlite_open_file_count files were left open"
315 incr nErr
316 }
danielk19771e21fd52008-04-10 17:27:38 +0000317 if {[info exists ::tester_do_ostrace]} {
318 puts "Writing ostrace.sql..."
319 set fd $::ostrace_fd
320
321 puts -nonewline $fd "CREATE TABLE ossummary"
322 puts $fd "(method TEXT, clicks INTEGER, count INTEGER);"
323 foreach row [sqlite3_instvfs report ostrace] {
324 foreach {method count clicks} $row break
325 puts $fd "INSERT INTO ossummary VALUES('$method', $clicks, $count);"
326 }
327 puts $fd "COMMIT;"
328 close $fd
329 sqlite3_instvfs destroy ostrace
330 }
drhf3a65f72007-08-22 20:18:21 +0000331 if {[sqlite3_memory_used]>0} {
332 puts "Unfreed memory: [sqlite3_memory_used] bytes"
333 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000334 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000335 puts "Writing unfreed memory log to \"./memleak.txt\""
336 sqlite3_memdebug_dump ./memleak.txt
337 }
drhf3a65f72007-08-22 20:18:21 +0000338 } else {
339 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000340 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000341 sqlite3_memdebug_dump ./memusage.txt
342 }
drhf3a65f72007-08-22 20:18:21 +0000343 }
drhf7141992008-06-19 00:16:08 +0000344 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000345 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
346 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000347 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
348 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
349 }
danielk197735754ac2008-03-21 17:29:37 +0000350 if {[info exists ::tester_do_malloctrace]} {
351 puts "Writing mallocs.sql..."
352 memdebug_log_sql
353 sqlite3_memdebug_log stop
354 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000355
356 if {[sqlite3_memory_used]>0} {
357 puts "Writing leaks.sql..."
358 sqlite3_memdebug_log sync
359 memdebug_log_sql leaks.sql
360 }
danielk197735754ac2008-03-21 17:29:37 +0000361 }
drhd9910fe2006-10-04 11:55:49 +0000362 foreach f [glob -nocomplain test.db-*-journal] {
363 file delete -force $f
364 }
365 foreach f [glob -nocomplain test.db-mj*] {
366 file delete -force $f
367 }
drhdb25e382001-03-15 18:21:22 +0000368 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000369}
370
drhf7141992008-06-19 00:16:08 +0000371# Display memory statistics for analysis and debugging purposes.
372#
373proc show_memstats {} {
374 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000375 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
376 set val [format {now %10d max %10d max-size %10d} \
377 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000378 puts "Memory used: $val"
379 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000380 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
381 set val [format {now %10d max %10d max-size %10d} \
382 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000383 puts "Page-cache used: $val"
384 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
385 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
386 puts "Page-cache overflow: $val"
387 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
388 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
389 puts "Scratch memory used: $val"
390 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000391 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
392 set val [format {now %10d max %10d max-size %10d} \
393 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000394 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000395 ifcapable yytrackmaxstackdepth {
396 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
397 set val [format { max %10d} [lindex $x 2]]
398 puts "Parser stack depth: $val"
399 }
drhf7141992008-06-19 00:16:08 +0000400}
401
drh348784e2000-05-29 20:41:49 +0000402# A procedure to execute SQL
403#
drhc4a3c772001-04-04 11:48:57 +0000404proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000405 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000406 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000407}
drh3aadb2e2000-05-31 17:59:25 +0000408
drhadbca9c2001-09-27 15:11:53 +0000409# Execute SQL and catch exceptions.
410#
411proc catchsql {sql {db db}} {
412 # puts "SQL = $sql"
413 set r [catch {$db eval $sql} msg]
414 lappend r $msg
415 return $r
416}
417
drh04096482001-11-09 22:41:44 +0000418# Do an VDBE code dump on the SQL given
419#
420proc explain {sql {db db}} {
421 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000422 puts "addr opcode p1 p2 p3 p4 p5 #"
423 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000424 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000425 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
426 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
427 ]
drh04096482001-11-09 22:41:44 +0000428 }
429}
430
drhdafc0ce2008-04-17 19:14:02 +0000431# Show the VDBE program for an SQL statement but omit the Trace
432# opcode at the beginning. This procedure can be used to prove
433# that different SQL statements generate exactly the same VDBE code.
434#
435proc explain_no_trace {sql} {
436 set tr [db eval "EXPLAIN $sql"]
437 return [lrange $tr 7 end]
438}
439
drh3aadb2e2000-05-31 17:59:25 +0000440# Another procedure to execute SQL. This one includes the field
441# names in the returned list.
442#
443proc execsql2 {sql} {
444 set result {}
445 db eval $sql data {
446 foreach f $data(*) {
447 lappend result $f $data($f)
448 }
449 }
450 return $result
451}
drh17a68932001-01-31 13:28:08 +0000452
drhdde85d92003-03-01 19:45:34 +0000453# Use the non-callback API to execute multiple SQL statements
454#
455proc stepsql {dbptr sql} {
456 set sql [string trim $sql]
457 set r 0
458 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000459 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000460 return [list 1 $vm]
461 }
462 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000463# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
464# foreach v $VAL {lappend r $v}
465# }
466 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
467 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
468 lappend r [sqlite3_column_text $vm $i]
469 }
drhdde85d92003-03-01 19:45:34 +0000470 }
danielk1977106bb232004-05-21 10:08:53 +0000471 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000472 return [list 1 $errmsg]
473 }
474 }
475 return $r
476}
477
drh17a68932001-01-31 13:28:08 +0000478# Delete a file or directory
479#
480proc forcedelete {filename} {
481 if {[catch {file delete -force $filename}]} {
482 exec rm -rf $filename
483 }
484}
drh21504322002-06-25 13:16:02 +0000485
486# Do an integrity check of the entire database
487#
danielk197704103022009-02-03 16:51:24 +0000488proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000489 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000490 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000491 }
492}
493
danielk1977db4e8862008-01-16 08:24:46 +0000494proc fix_ifcapable_expr {expr} {
495 set ret ""
496 set state 0
497 for {set i 0} {$i < [string length $expr]} {incr i} {
498 set char [string range $expr $i $i]
499 set newstate [expr {[string is alnum $char] || $char eq "_"}]
500 if {$newstate && !$state} {
501 append ret {$::sqlite_options(}
502 }
503 if {!$newstate && $state} {
504 append ret )
505 }
506 append ret $char
507 set state $newstate
508 }
509 if {$state} {append ret )}
510 return $ret
511}
512
drh27d258a2004-10-30 20:23:09 +0000513# Evaluate a boolean expression of capabilities. If true, execute the
514# code. Omit the code if false.
515#
danielk19773e8c37e2005-01-21 03:12:14 +0000516proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000517 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
518 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000519 if ($e2) {
520 set c [catch {uplevel 1 $code} r]
521 } else {
522 set c [catch {uplevel 1 $elsecode} r]
523 }
524 return -code $c $r
drh21504322002-06-25 13:16:02 +0000525}
danielk197745901d62004-11-10 15:27:38 +0000526
danielk1977aca790a2005-01-13 11:07:52 +0000527# This proc execs a seperate process that crashes midway through executing
528# the SQL script $sql on database test.db.
529#
530# The crash occurs during a sync() of file $crashfile. When the crash
531# occurs a random subset of all unsynced writes made by the process are
532# written into the files on disk. Argument $crashdelay indicates the
533# number of file syncs to wait before crashing.
534#
535# The return value is a list of two elements. The first element is a
536# boolean, indicating whether or not the process actually crashed or
537# reported some other error. The second element in the returned list is the
538# error message. This is "child process exited abnormally" if the crash
539# occured.
540#
danielk1977f8940ae2007-08-23 11:07:10 +0000541# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000542#
543proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000544 if {$::tcl_platform(platform)!="unix"} {
545 error "crashsql should only be used on unix"
546 }
danielk197759a33f92007-03-17 10:26:59 +0000547
548 set blocksize ""
549 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000550 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000551 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000552 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000553 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000554 set sql [lindex $args end]
555
556 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
557 set z [lindex $args $ii]
558 set n [string length $z]
559 set z2 [lindex $args [expr $ii+1]]
560
561 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000562 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000563 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000564 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000565 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000566 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000567 else { error "Unrecognized option: $z" }
568 }
569
570 if {$crashfile eq ""} {
571 error "Compulsory option -file missing"
572 }
573
danielk1977aca790a2005-01-13 11:07:52 +0000574 set cfile [file join [pwd] $crashfile]
575
576 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000577 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000578 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000579 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000580 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000581
582 # This block sets the cache size of the main database to 10
583 # pages. This is done in case the build is configured to omit
584 # "PRAGMA cache_size".
585 puts $f {db eval {SELECT * FROM sqlite_master;}}
586 puts $f {set bt [btree_from_db db]}
587 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000588 if {$prngseed} {
589 set seed [expr {$prngseed%10007+1}]
590 # puts seed=$seed
591 puts $f "db eval {SELECT randomblob($seed)}"
592 }
danielk1977933bbd62007-03-17 07:22:42 +0000593
drhf8587402008-01-08 16:03:49 +0000594 if {[string length $tclbody]>0} {
595 puts $f $tclbody
596 }
597 if {[string length $sql]>0} {
598 puts $f "db eval {"
599 puts $f "$sql"
600 puts $f "}"
601 }
danielk1977aca790a2005-01-13 11:07:52 +0000602 close $f
603
604 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000605 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000606 } msg]
607 lappend r $msg
608}
609
danielk197732554c12005-01-22 03:39:39 +0000610# Usage: do_ioerr_test <test number> <options...>
611#
612# This proc is used to implement test cases that check that IO errors
613# are correctly handled. The first argument, <test number>, is an integer
614# used to name the tests executed by this proc. Options are as follows:
615#
616# -tclprep TCL script to run to prepare test.
617# -sqlprep SQL script to run to prepare test.
618# -tclbody TCL script to run with IO error simulation.
619# -sqlbody TCL script to run with IO error simulation.
620# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000621# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000622# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000623# -start Value of 'N' to begin with (default 1)
624#
625# -cksum Boolean. If true, test that the database does
626# not change during the execution of the test case.
627#
628proc do_ioerr_test {testname args} {
629
danielk197732554c12005-01-22 03:39:39 +0000630 set ::ioerropts(-start) 1
631 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000632 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000633 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000634 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000635 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000636 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000637 array set ::ioerropts $args
638
danielk197747cd39c2008-05-12 12:41:15 +0000639 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
640 # a couple of obscure IO errors that do not return them.
641 set ::ioerropts(-erc) 0
642
danielk197732554c12005-01-22 03:39:39 +0000643 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000644 #reset_prng_state
645 save_prng_state
danielk1977861f7452008-06-05 11:39:11 +0000646 for {set n $::ioerropts(-start)} {$::go && $n<200} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000647 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000648 incr ::ioerropts(-count) -1
649 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000650
651 # Skip this IO error if it was specified with the "-exclude" option.
652 if {[info exists ::ioerropts(-exclude)]} {
653 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
654 }
danielk1977861f7452008-06-05 11:39:11 +0000655 if {$::ioerropts(-restoreprng)} {
656 restore_prng_state
657 }
danielk197732554c12005-01-22 03:39:39 +0000658
659 # Delete the files test.db and test2.db, then execute the TCL and
660 # SQL (in that order) to prepare for the test case.
661 do_test $testname.$n.1 {
662 set ::sqlite_io_error_pending 0
663 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000664 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000665 catch {file delete -force test.db}
666 catch {file delete -force test.db-journal}
667 catch {file delete -force test2.db}
668 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000669 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000670 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000671 if {[info exists ::ioerropts(-tclprep)]} {
672 eval $::ioerropts(-tclprep)
673 }
674 if {[info exists ::ioerropts(-sqlprep)]} {
675 execsql $::ioerropts(-sqlprep)
676 }
677 expr 0
678 } {0}
679
680 # Read the 'checksum' of the database.
681 if {$::ioerropts(-cksum)} {
682 set checksum [cksum]
683 }
drh93aed5a2008-01-16 17:46:38 +0000684
danielk197732554c12005-01-22 03:39:39 +0000685 # Set the Nth IO error to fail.
686 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000687 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000688 set ::sqlite_io_error_pending $n
689 }] $n
690
691 # Create a single TCL script from the TCL and SQL specified
692 # as the body of the test.
693 set ::ioerrorbody {}
694 if {[info exists ::ioerropts(-tclbody)]} {
695 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
696 }
697 if {[info exists ::ioerropts(-sqlbody)]} {
698 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
699 }
700
701 # Execute the TCL Script created in the above block. If
702 # there are at least N IO operations performed by SQLite as
703 # a result of the script, the Nth will fail.
704 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000705 set ::sqlite_io_error_hit 0
706 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000707 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000708 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000709 set rc [sqlite3_errcode $::DB]
710 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000711 # If we are in extended result code mode, make sure all of the
712 # IOERRs we get back really do have their extended code values.
713 # If an extended result code is returned, the sqlite3_errcode
714 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
715 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000716 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
717 return $rc
718 }
719 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000720 # If we are not in extended result code mode, make sure no
721 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000722 if {[regexp {\+\d} $rc]} {
723 return $rc
724 }
725 }
drh93aed5a2008-01-16 17:46:38 +0000726 # The test repeats as long as $::go is non-zero. $::go starts out
727 # as 1. When a test runs to completion without hitting an I/O
728 # error, that means there is no point in continuing with this test
729 # case so set $::go to zero.
730 #
731 if {$::sqlite_io_error_pending>0} {
732 set ::go 0
733 set q 0
734 set ::sqlite_io_error_pending 0
735 } else {
736 set q 1
737 }
738
drhc9ac5ca2005-11-04 22:03:30 +0000739 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +0000740 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
741 set r 1
742 }
danielk1977f2fa8312006-01-24 13:09:33 +0000743 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000744
745 # One of two things must have happened. either
746 # 1. We never hit the IO error and the SQL returned OK
747 # 2. An IO error was hit and the SQL failed
748 #
drh93aed5a2008-01-16 17:46:38 +0000749 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +0000750 } {1}
751
danielk197780daec62008-05-12 10:57:02 +0000752 set ::sqlite_io_error_hit 0
753 set ::sqlite_io_error_pending 0
754
danielk197752b472a2008-05-05 16:23:55 +0000755 # Check that no page references were leaked. There should be
756 # a single reference if there is still an active transaction,
757 # or zero otherwise.
758 #
danielk197781620542008-06-07 05:19:37 +0000759 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
760 # locks are established on database files (i.e. if the error
761 # occurs while attempting to detect a hot-journal file), then
762 # there may 0 page references and an active transaction according
763 # to [sqlite3_get_autocommit].
764 #
danielk19774abd5442008-05-05 15:26:50 +0000765 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +0000766 do_test $testname.$n.4 {
767 set bt [btree_from_db db]
768 db_enter db
769 array set stats [btree_pager_stats $bt]
770 db_leave db
danielk197781620542008-06-07 05:19:37 +0000771 set nRef $stats(ref)
772 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
773 } {1}
danielk19774abd5442008-05-05 15:26:50 +0000774 }
775
danielk197752b472a2008-05-05 16:23:55 +0000776 # If there is an open database handle and no open transaction,
777 # and the pager is not running in exclusive-locking mode,
778 # check that the pager is in "unlocked" state. Theoretically,
779 # if a call to xUnlock() failed due to an IO error the underlying
780 # file may still be locked.
781 #
782 ifcapable pragma {
783 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +0000784 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +0000785 && [db one {pragma locking_mode}] eq "normal"
786 && [sqlite3_get_autocommit db]
787 } {
788 do_test $testname.$n.5 {
789 set bt [btree_from_db db]
790 db_enter db
791 array set stats [btree_pager_stats $bt]
792 db_leave db
793 set stats(state)
794 } 0
795 }
796 }
797
danielk197732554c12005-01-22 03:39:39 +0000798 # If an IO error occured, then the checksum of the database should
799 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +0000800 #
drh1aa5af12008-03-07 19:51:14 +0000801 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +0000802 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +0000803 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +0000804 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +0000805 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000806 cksum
807 } $checksum
808 }
809
drh1aa5af12008-03-07 19:51:14 +0000810 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +0000811 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000812 if {[info exists ::ioerropts(-cleanup)]} {
813 catch $::ioerropts(-cleanup)
814 }
danielk197732554c12005-01-22 03:39:39 +0000815 }
816 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000817 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000818 unset ::ioerropts
819}
820
drh93aed5a2008-01-16 17:46:38 +0000821# Return a checksum based on the contents of the main database associated
822# with connection $db
danielk197732554c12005-01-22 03:39:39 +0000823#
824proc cksum {{db db}} {
825 set txt [$db eval {
826 SELECT name, type, sql FROM sqlite_master order by name
827 }]\n
828 foreach tbl [$db eval {
829 SELECT name FROM sqlite_master WHERE type='table' order by name
830 }] {
831 append txt [$db eval "SELECT * FROM $tbl"]\n
832 }
833 foreach prag {default_synchronous default_cache_size} {
834 append txt $prag-[$db eval "PRAGMA $prag"]\n
835 }
836 set cksum [string length $txt]-[md5 $txt]
837 # puts $cksum-[file size test.db]
838 return $cksum
839}
840
drh93aed5a2008-01-16 17:46:38 +0000841# Generate a checksum based on the contents of the main and temp tables
842# database $db. If the checksum of two databases is the same, and the
843# integrity-check passes for both, the two databases are identical.
844#
drh1db639c2008-01-17 02:36:28 +0000845proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +0000846 set ret [list]
847 ifcapable tempdb {
848 set sql {
849 SELECT name FROM sqlite_master WHERE type = 'table' UNION
850 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
851 SELECT 'sqlite_master' UNION
852 SELECT 'sqlite_temp_master' ORDER BY 1
853 }
854 } else {
855 set sql {
856 SELECT name FROM sqlite_master WHERE type = 'table' UNION
857 SELECT 'sqlite_master' ORDER BY 1
858 }
859 }
860 set tbllist [$db eval $sql]
861 set txt {}
862 foreach tbl $tbllist {
863 append txt [$db eval "SELECT * FROM $tbl"]
864 }
865 foreach prag {default_cache_size} {
866 append txt $prag-[$db eval "PRAGMA $prag"]\n
867 }
868 # puts txt=$txt
869 return [md5 $txt]
870}
871
drhdc2c4912009-02-04 22:46:47 +0000872# Generate a checksum based on the contents of a single database with
873# a database connection. The name of the database is $dbname.
874# Examples of $dbname are "temp" or "main".
875#
876proc dbcksum {db dbname} {
877 if {$dbname=="temp"} {
878 set master sqlite_temp_master
879 } else {
880 set master $dbname.sqlite_master
881 }
882 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
883 set txt [$db eval "SELECT * FROM $master"]\n
884 foreach tab $alltab {
885 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
886 }
887 return [md5 $txt]
888}
889
danielk197735754ac2008-03-21 17:29:37 +0000890proc memdebug_log_sql {{filename mallocs.sql}} {
891
danielk19776f332c12008-03-21 14:22:44 +0000892 set data [sqlite3_memdebug_log dump]
893 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +0000894 if {$nFrame < 0} { return "" }
895
danielk197735754ac2008-03-21 17:29:37 +0000896 set database temp
897
danielk19776f332c12008-03-21 14:22:44 +0000898 set tbl "CREATE TABLE ${database}.malloc(nCall, nByte"
899 for {set ii 1} {$ii <= $nFrame} {incr ii} {
900 append tbl ", f${ii}"
901 }
902 append tbl ");\n"
903
904 set sql ""
905 foreach e $data {
906 append sql "INSERT INTO ${database}.malloc VALUES([join $e ,]);\n"
907 foreach f [lrange $e 2 end] {
908 set frames($f) 1
909 }
910 }
911
912 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +0000913 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +0000914
915 foreach f [array names frames] {
916 set addr [format %x $f]
917 set cmd "addr2line -e [info nameofexec] $addr"
918 set line [eval exec $cmd]
919 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +0000920
921 set file [lindex [split $line :] 0]
922 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +0000923 }
924
danielk197735754ac2008-03-21 17:29:37 +0000925 foreach f [array names files] {
926 set contents ""
927 catch {
928 set fd [open $f]
929 set contents [read $fd]
930 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000931 }
danielk197735754ac2008-03-21 17:29:37 +0000932 set contents [string map {' ''} $contents]
933 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +0000934 }
danielk19776f332c12008-03-21 14:22:44 +0000935
danielk197735754ac2008-03-21 17:29:37 +0000936 set fd [open $filename w]
937 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
938 close $fd
danielk19776f332c12008-03-21 14:22:44 +0000939}
drh93aed5a2008-01-16 17:46:38 +0000940
danielk197732554c12005-01-22 03:39:39 +0000941# Copy file $from into $to. This is used because some versions of
942# TCL for windows (notably the 8.4.1 binary package shipped with the
943# current mingw release) have a broken "file copy" command.
944#
945proc copy_file {from to} {
946 if {$::tcl_platform(platform)=="unix"} {
947 file copy -force $from $to
948 } else {
949 set f [open $from]
950 fconfigure $f -translation binary
951 set t [open $to w]
952 fconfigure $t -translation binary
953 puts -nonewline $t [read $f [file size $from]]
954 close $t
955 close $f
956 }
957}
958
danielk197745901d62004-11-10 15:27:38 +0000959# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
960# to non-zero, then set the global variable $AUTOVACUUM to 1.
961set AUTOVACUUM $sqlite_options(default_autovacuum)