2009-01-03 13:41:29 +03:00
|
|
|
# 2009 January 3
|
|
|
|
#
|
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
|
|
|
#
|
|
|
|
# May you do good and not evil.
|
|
|
|
# May you find forgiveness for yourself and forgive others.
|
|
|
|
# May you share freely, never taking more than you give.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
#
|
2009-06-05 21:09:11 +04:00
|
|
|
# $Id: savepoint6.test,v 1.4 2009/06/05 17:09:12 drh Exp $
|
2009-01-03 13:41:29 +03:00
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
|
|
|
|
2009-01-06 16:40:08 +03:00
|
|
|
proc sql {zSql} {
|
|
|
|
uplevel db eval [list $zSql]
|
|
|
|
#puts stderr "$zSql ;"
|
|
|
|
}
|
|
|
|
|
|
|
|
set DATABASE_SCHEMA {
|
2009-01-03 13:41:29 +03:00
|
|
|
PRAGMA auto_vacuum = incremental;
|
|
|
|
CREATE TABLE t1(x, y);
|
|
|
|
CREATE UNIQUE INDEX i1 ON t1(x);
|
|
|
|
CREATE INDEX i2 ON t1(y);
|
2009-01-06 16:40:08 +03:00
|
|
|
}
|
2009-01-03 13:41:29 +03:00
|
|
|
|
2010-06-07 21:47:26 +04:00
|
|
|
if {0==[info exists ::G(savepoint6_iterations)]} {
|
|
|
|
set ::G(savepoint6_iterations) 1000
|
2010-04-27 09:42:32 +04:00
|
|
|
}
|
2010-04-26 21:42:56 +04:00
|
|
|
|
2009-01-03 13:41:29 +03:00
|
|
|
#--------------------------------------------------------------------------
|
|
|
|
# In memory database state.
|
|
|
|
#
|
|
|
|
# ::lSavepoint is a list containing one entry for each active savepoint. The
|
|
|
|
# first entry in the list corresponds to the most recently opened savepoint.
|
|
|
|
# Each entry consists of two elements:
|
|
|
|
#
|
|
|
|
# 1. The savepoint name.
|
|
|
|
#
|
|
|
|
# 2. A serialized Tcl array representing the contents of table t1 at the
|
|
|
|
# start of the savepoint. The keys of the array are the x values. The
|
|
|
|
# values are the y values.
|
|
|
|
#
|
|
|
|
# Array ::aEntry contains the contents of database table t1. Array keys are
|
|
|
|
# x values, the array data values are y values.
|
|
|
|
#
|
|
|
|
set lSavepoint [list]
|
|
|
|
array set aEntry [list]
|
|
|
|
|
|
|
|
proc x_to_y {x} {
|
|
|
|
set nChar [expr int(rand()*250) + 250]
|
|
|
|
set str " $nChar [string repeat $x. $nChar]"
|
|
|
|
string range $str 1 $nChar
|
|
|
|
}
|
|
|
|
#--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
# Procs to operate on database:
|
|
|
|
#
|
|
|
|
# savepoint NAME
|
|
|
|
# rollback NAME
|
|
|
|
# release NAME
|
|
|
|
#
|
|
|
|
# insert_rows XVALUES
|
|
|
|
# delete_rows XVALUES
|
|
|
|
#
|
|
|
|
proc savepoint {zName} {
|
2009-01-06 16:40:08 +03:00
|
|
|
catch { sql "SAVEPOINT $zName" }
|
2009-01-03 13:41:29 +03:00
|
|
|
lappend ::lSavepoint [list $zName [array get ::aEntry]]
|
|
|
|
}
|
|
|
|
|
|
|
|
proc rollback {zName} {
|
2009-01-06 16:40:08 +03:00
|
|
|
catch { sql "ROLLBACK TO $zName" }
|
2009-01-03 13:41:29 +03:00
|
|
|
for {set i [expr {[llength $::lSavepoint]-1}]} {$i>=0} {incr i -1} {
|
|
|
|
set zSavepoint [lindex $::lSavepoint $i 0]
|
|
|
|
if {$zSavepoint eq $zName} {
|
|
|
|
unset -nocomplain ::aEntry
|
|
|
|
array set ::aEntry [lindex $::lSavepoint $i 1]
|
|
|
|
|
|
|
|
|
|
|
|
if {$i+1 < [llength $::lSavepoint]} {
|
|
|
|
set ::lSavepoint [lreplace $::lSavepoint [expr $i+1] end]
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc release {zName} {
|
2009-01-06 16:40:08 +03:00
|
|
|
catch { sql "RELEASE $zName" }
|
2009-01-03 13:41:29 +03:00
|
|
|
for {set i [expr {[llength $::lSavepoint]-1}]} {$i>=0} {incr i -1} {
|
|
|
|
set zSavepoint [lindex $::lSavepoint $i 0]
|
|
|
|
if {$zSavepoint eq $zName} {
|
|
|
|
set ::lSavepoint [lreplace $::lSavepoint $i end]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2009-01-06 16:40:08 +03:00
|
|
|
|
|
|
|
if {[llength $::lSavepoint] == 0} {
|
|
|
|
#puts stderr "-- End of transaction!!!!!!!!!!!!!"
|
|
|
|
}
|
2009-01-03 13:41:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
proc insert_rows {lX} {
|
|
|
|
foreach x $lX {
|
|
|
|
set y [x_to_y $x]
|
|
|
|
|
|
|
|
# Update database [db]
|
2009-01-06 16:40:08 +03:00
|
|
|
sql "INSERT OR REPLACE INTO t1 VALUES($x, '$y')"
|
2009-01-03 13:41:29 +03:00
|
|
|
|
|
|
|
# Update the Tcl database.
|
|
|
|
set ::aEntry($x) $y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc delete_rows {lX} {
|
|
|
|
foreach x $lX {
|
|
|
|
# Update database [db]
|
2009-01-06 16:40:08 +03:00
|
|
|
sql "DELETE FROM t1 WHERE x = $x"
|
2009-01-03 13:41:29 +03:00
|
|
|
|
|
|
|
# Update the Tcl database.
|
|
|
|
unset -nocomplain ::aEntry($x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
# Proc to compare database content with the in-memory representation.
|
|
|
|
#
|
|
|
|
# checkdb
|
|
|
|
#
|
|
|
|
proc checkdb {} {
|
|
|
|
set nEntry [db one {SELECT count(*) FROM t1}]
|
|
|
|
set nEntry2 [array size ::aEntry]
|
|
|
|
if {$nEntry != $nEntry2} {
|
|
|
|
error "$nEntry entries in database, $nEntry2 entries in array"
|
|
|
|
}
|
|
|
|
db eval {SELECT x, y FROM t1} {
|
|
|
|
if {![info exists ::aEntry($x)]} {
|
|
|
|
error "Entry $x exists in database, but not in array"
|
|
|
|
}
|
|
|
|
if {$::aEntry($x) ne $y} {
|
|
|
|
error "Entry $x is set to {$y} in database, {$::aEntry($x)} in array"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
db eval { PRAGMA integrity_check }
|
|
|
|
}
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
# Proc to return random set of x values.
|
|
|
|
#
|
|
|
|
# random_integers
|
|
|
|
#
|
|
|
|
proc random_integers {nRes nRange} {
|
|
|
|
set ret [list]
|
|
|
|
for {set i 0} {$i<$nRes} {incr i} {
|
|
|
|
lappend ret [expr int(rand()*$nRange)]
|
|
|
|
}
|
|
|
|
return $ret
|
|
|
|
}
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc database_op {} {
|
|
|
|
set i [expr int(rand()*2)]
|
|
|
|
if {$i==0} {
|
|
|
|
insert_rows [random_integers 100 1000]
|
|
|
|
}
|
|
|
|
if {$i==1} {
|
|
|
|
delete_rows [random_integers 100 1000]
|
|
|
|
set i [expr int(rand()*3)]
|
|
|
|
if {$i==0} {
|
2009-01-06 16:40:08 +03:00
|
|
|
sql {PRAGMA incremental_vacuum}
|
2009-01-03 13:41:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc savepoint_op {} {
|
|
|
|
set names {one two three four five}
|
|
|
|
set cmds {savepoint savepoint savepoint savepoint release rollback}
|
|
|
|
|
|
|
|
set C [lindex $cmds [expr int(rand()*6)]]
|
|
|
|
set N [lindex $names [expr int(rand()*5)]]
|
|
|
|
|
2009-01-06 16:40:08 +03:00
|
|
|
#puts stderr " $C $N ; "
|
|
|
|
#flush stderr
|
|
|
|
|
2009-01-03 13:41:29 +03:00
|
|
|
$C $N
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2009-01-06 16:40:08 +03:00
|
|
|
expr srand(0)
|
|
|
|
|
|
|
|
############################################################################
|
|
|
|
############################################################################
|
|
|
|
# Start of test cases.
|
|
|
|
|
|
|
|
do_test savepoint6-1.1 {
|
|
|
|
sql $DATABASE_SCHEMA
|
|
|
|
} {}
|
|
|
|
do_test savepoint6-1.2 {
|
|
|
|
insert_rows {
|
|
|
|
497 166 230 355 779 588 394 317 290 475 362 193 805 851 564
|
|
|
|
763 44 930 389 819 765 760 966 280 538 414 500 18 25 287 320
|
|
|
|
30 382 751 87 283 981 429 630 974 421 270 810 405
|
|
|
|
}
|
|
|
|
|
2009-01-03 13:41:29 +03:00
|
|
|
savepoint one
|
2009-01-06 16:40:08 +03:00
|
|
|
insert_rows 858
|
|
|
|
delete_rows 930
|
|
|
|
savepoint two
|
|
|
|
execsql {PRAGMA incremental_vacuum}
|
|
|
|
savepoint three
|
|
|
|
insert_rows 144
|
|
|
|
rollback three
|
|
|
|
rollback two
|
2009-01-03 13:41:29 +03:00
|
|
|
release one
|
|
|
|
|
2009-01-06 16:40:08 +03:00
|
|
|
execsql {SELECT count(*) FROM t1}
|
|
|
|
} {44}
|
|
|
|
|
|
|
|
foreach zSetup [list {
|
|
|
|
set testname normal
|
|
|
|
sqlite3 db test.db
|
|
|
|
} {
|
2010-04-26 20:57:10 +04:00
|
|
|
if {[wal_is_wal_mode]} continue
|
2009-01-06 16:40:08 +03:00
|
|
|
set testname tempdb
|
|
|
|
sqlite3 db ""
|
2009-01-07 20:06:52 +03:00
|
|
|
} {
|
2010-06-07 21:47:26 +04:00
|
|
|
if {[permutation] eq "journaltest"} {
|
2009-01-07 20:06:52 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
set testname nosync
|
|
|
|
sqlite3 db test.db
|
|
|
|
sql { PRAGMA synchronous = off }
|
2009-01-06 16:40:08 +03:00
|
|
|
} {
|
|
|
|
set testname smallcache
|
|
|
|
sqlite3 db test.db
|
|
|
|
sql { PRAGMA cache_size = 10 }
|
|
|
|
}] {
|
|
|
|
|
|
|
|
unset -nocomplain ::lSavepoint
|
|
|
|
unset -nocomplain ::aEntry
|
2009-01-03 13:41:29 +03:00
|
|
|
|
2009-01-07 20:06:52 +03:00
|
|
|
catch { db close }
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test.db test.db-wal test.db-journal
|
2009-01-06 16:40:08 +03:00
|
|
|
eval $zSetup
|
|
|
|
sql $DATABASE_SCHEMA
|
|
|
|
|
2010-04-26 20:57:10 +04:00
|
|
|
wal_set_journal_mode
|
|
|
|
|
2009-01-06 16:40:08 +03:00
|
|
|
do_test savepoint6-$testname.setup {
|
|
|
|
savepoint one
|
|
|
|
insert_rows [random_integers 100 1000]
|
|
|
|
release one
|
2009-01-03 13:41:29 +03:00
|
|
|
checkdb
|
|
|
|
} {ok}
|
2009-01-06 16:40:08 +03:00
|
|
|
|
2010-06-07 21:47:26 +04:00
|
|
|
for {set i 0} {$i < $::G(savepoint6_iterations)} {incr i} {
|
2009-01-06 16:40:08 +03:00
|
|
|
do_test savepoint6-$testname.$i.1 {
|
|
|
|
savepoint_op
|
|
|
|
checkdb
|
|
|
|
} {ok}
|
|
|
|
|
|
|
|
do_test savepoint6-$testname.$i.2 {
|
|
|
|
database_op
|
|
|
|
database_op
|
|
|
|
checkdb
|
|
|
|
} {ok}
|
|
|
|
}
|
2010-04-26 20:57:10 +04:00
|
|
|
|
|
|
|
wal_check_journal_mode savepoint6-$testname.walok
|
2009-01-03 13:41:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
unset -nocomplain ::lSavepoint
|
|
|
|
unset -nocomplain ::aEntry
|
|
|
|
|
|
|
|
finish_test
|