2004-06-30 15:14:18 +04:00
|
|
|
# 2004 June 30
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
# This file implements regression tests for SQLite library. The
|
|
|
|
# focus of this file is verifying that a rollback in one statement
|
|
|
|
# caused by an ON CONFLICT ROLLBACK clause aborts any other pending
|
|
|
|
# statements.
|
|
|
|
#
|
2009-06-26 11:12:06 +04:00
|
|
|
# $Id: rollback.test,v 1.11 2009/06/26 07:12:07 danielk1977 Exp $
|
2004-06-30 15:14:18 +04:00
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
|
|
|
|
2006-01-03 03:33:50 +03:00
|
|
|
set DB [sqlite3_connection_pointer db]
|
2004-06-30 15:14:18 +04:00
|
|
|
|
|
|
|
do_test rollback-1.1 {
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE t1(a);
|
|
|
|
INSERT INTO t1 VALUES(1);
|
|
|
|
INSERT INTO t1 VALUES(2);
|
|
|
|
INSERT INTO t1 VALUES(3);
|
|
|
|
INSERT INTO t1 VALUES(4);
|
|
|
|
SELECT * FROM t1;
|
|
|
|
}
|
|
|
|
} {1 2 3 4}
|
|
|
|
|
2006-01-17 12:35:01 +03:00
|
|
|
ifcapable conflict {
|
|
|
|
do_test rollback-1.2 {
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE t3(a unique on conflict rollback);
|
|
|
|
INSERT INTO t3 SELECT a FROM t1;
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO t1 SELECT * FROM t1;
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
}
|
2004-06-30 15:14:18 +04:00
|
|
|
do_test rollback-1.3 {
|
|
|
|
set STMT [sqlite3_prepare $DB "SELECT a FROM t1" -1 TAIL]
|
|
|
|
sqlite3_step $STMT
|
|
|
|
} {SQLITE_ROW}
|
|
|
|
|
2006-01-17 12:35:01 +03:00
|
|
|
ifcapable conflict {
|
|
|
|
# This causes a ROLLBACK, which deletes the table out from underneath the
|
|
|
|
# SELECT statement.
|
|
|
|
#
|
|
|
|
do_test rollback-1.4 {
|
|
|
|
catchsql {
|
|
|
|
INSERT INTO t3 SELECT a FROM t1;
|
|
|
|
}
|
2013-11-05 17:33:55 +04:00
|
|
|
} {1 {UNIQUE constraint failed: t3.a}}
|
2006-01-17 12:35:01 +03:00
|
|
|
|
|
|
|
# Try to continue with the SELECT statement
|
|
|
|
#
|
|
|
|
do_test rollback-1.5 {
|
|
|
|
sqlite3_step $STMT
|
2007-08-30 06:26:53 +04:00
|
|
|
} {SQLITE_ERROR}
|
2007-09-12 21:01:45 +04:00
|
|
|
|
|
|
|
# Restart the SELECT statement
|
|
|
|
#
|
|
|
|
do_test rollback-1.6 { sqlite3_reset $STMT } {SQLITE_ABORT}
|
|
|
|
} else {
|
|
|
|
do_test rollback-1.6 { sqlite3_reset $STMT } {SQLITE_OK}
|
2006-01-17 12:35:01 +03:00
|
|
|
}
|
2004-06-30 15:14:18 +04:00
|
|
|
|
|
|
|
do_test rollback-1.7 {
|
|
|
|
sqlite3_step $STMT
|
|
|
|
} {SQLITE_ROW}
|
|
|
|
do_test rollback-1.8 {
|
|
|
|
sqlite3_step $STMT
|
|
|
|
} {SQLITE_ROW}
|
|
|
|
do_test rollback-1.9 {
|
|
|
|
sqlite3_finalize $STMT
|
|
|
|
} {SQLITE_OK}
|
|
|
|
|
2008-10-17 22:51:52 +04:00
|
|
|
if {$tcl_platform(platform) == "unix"
|
2010-06-07 21:47:26 +04:00
|
|
|
&& [permutation] ne "onefile"
|
|
|
|
&& [permutation] ne "inmemory_journal"
|
2008-10-17 22:51:52 +04:00
|
|
|
} {
|
2008-10-07 15:51:20 +04:00
|
|
|
do_test rollback-2.1 {
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO t3 VALUES('hello world');
|
|
|
|
}
|
2011-08-02 04:57:34 +04:00
|
|
|
forcecopy test.db testA.db
|
|
|
|
forcecopy test.db-journal testA.db-journal
|
2008-10-07 15:51:20 +04:00
|
|
|
execsql {
|
|
|
|
COMMIT;
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
|
|
|
|
# At this point files testA.db and testA.db-journal are present in the
|
|
|
|
# file system. This block adds a master-journal file pointer to the
|
|
|
|
# end of testA.db-journal. The master-journal file does not exist.
|
|
|
|
#
|
|
|
|
set mj [file normalize testA.db-mj-123]
|
|
|
|
binary scan $mj c* a
|
|
|
|
set cksum 0
|
|
|
|
foreach i $a { incr cksum $i }
|
|
|
|
set mj_pgno [expr $sqlite_pending_byte / 1024]
|
|
|
|
set zAppend [binary format Ia*IIa8 $mj_pgno $mj [string length $mj] $cksum \
|
|
|
|
"\xd9\xd5\x05\xf9\x20\xa1\x63\xd7"
|
|
|
|
]
|
|
|
|
set iOffset [expr (([file size testA.db-journal] + 511)/512)*512]
|
|
|
|
set fd [open testA.db-journal a+]
|
|
|
|
fconfigure $fd -encoding binary -translation binary
|
|
|
|
seek $fd $iOffset
|
|
|
|
puts -nonewline $fd $zAppend
|
2009-06-26 11:12:06 +04:00
|
|
|
|
|
|
|
# Also, fix the first journal-header in the journal-file. Because the
|
|
|
|
# journal file has not yet been synced, the 8-byte magic string at the
|
|
|
|
# start of the first journal-header has not been written by SQLite.
|
|
|
|
# So write it now.
|
|
|
|
seek $fd 0
|
|
|
|
puts -nonewline $fd "\xd9\xd5\x05\xf9\x20\xa1\x63\xd7"
|
2008-10-07 15:51:20 +04:00
|
|
|
close $fd
|
|
|
|
|
|
|
|
# Open a handle on testA.db and use it to query the database. At one
|
|
|
|
# point the first query would attempt a hot rollback, attempt to open
|
|
|
|
# the master-journal file and return SQLITE_CANTOPEN when it could not
|
|
|
|
# be opened. This is incorrect, it should simply delete the journal
|
|
|
|
# file and proceed with the query.
|
|
|
|
#
|
|
|
|
do_test rollback-2.2 {
|
|
|
|
sqlite3 db2 testA.db
|
|
|
|
execsql {
|
|
|
|
SELECT distinct tbl_name FROM sqlite_master;
|
|
|
|
} db2
|
|
|
|
} {t1 t3}
|
2010-06-07 21:47:26 +04:00
|
|
|
if {[lsearch {exclusive persistent_journal no_journal} [permutation]]<0} {
|
2008-10-13 18:16:11 +04:00
|
|
|
do_test rollback-2.3 {
|
|
|
|
file exists testA.db-journal
|
|
|
|
} 0
|
|
|
|
}
|
2008-10-07 15:51:20 +04:00
|
|
|
do_test rollback-2.4 {
|
|
|
|
execsql {
|
|
|
|
SELECT distinct tbl_name FROM sqlite_master;
|
|
|
|
} db2
|
|
|
|
} {t1 t3}
|
|
|
|
|
|
|
|
db2 close
|
|
|
|
}
|
|
|
|
|
2004-06-30 15:14:18 +04:00
|
|
|
finish_test
|