Index: src/pager.c ================================================================== --- src/pager.c +++ src/pager.c @@ -5961,11 +5961,11 @@ ** journalmode. Journalmode changes can only happen when the database ** is unmodified. */ int sqlite3PagerOkToChangeJournalMode(Pager *pPager){ if( pPager->dbModified ) return 0; - if( isOpen(pPager->jfd) && pPager->journalOff>0 ) return 0; + if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0; return 1; } /* ** Get/set the size-limit used for persistent journal files. Index: src/test1.c ================================================================== --- src/test1.c +++ src/test1.c @@ -1900,10 +1900,11 @@ Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ char **argv /* Text of each argument */ ){ assert( interp==0 ); /* This will always fail */ + abort(); return TCL_OK; } /* ** The following routine is a user-defined SQL function whose purpose Index: src/test_vfs.c ================================================================== --- src/test_vfs.c +++ src/test_vfs.c @@ -188,11 +188,11 @@ static int tvfsShmMap(sqlite3_file*,int,int,int, void volatile **); static void tvfsShmBarrier(sqlite3_file*); static int tvfsShmClose(sqlite3_file*, int); static sqlite3_io_methods tvfs_io_methods = { - 2, /* iVersion */ + 2, /* iVersion */ tvfsClose, /* xClose */ tvfsRead, /* xRead */ tvfsWrite, /* xWrite */ tvfsTruncate, /* xTruncate */ tvfsSync, /* xSync */ @@ -573,13 +573,22 @@ Tcl_ResetResult(p->interp); rc = sqlite3OsOpen(PARENTVFS(pVfs), zName, pFd->pReal, flags, pOutFlags); if( pFd->pReal->pMethods ){ sqlite3_io_methods *pMethods; - pMethods = (sqlite3_io_methods *)ckalloc(sizeof(sqlite3_io_methods)); - memcpy(pMethods, &tvfs_io_methods, sizeof(sqlite3_io_methods)); - if( ((Testvfs *)pVfs->pAppData)->isNoshm ){ + int nByte; + + if( pVfs->iVersion>1 ){ + nByte = sizeof(sqlite3_io_methods); + }else{ + nByte = offsetof(sqlite3_io_methods, xShmOpen); + } + + pMethods = (sqlite3_io_methods *)ckalloc(nByte); + memcpy(pMethods, &tvfs_io_methods, nByte); + pMethods->iVersion = pVfs->iVersion; + if( pVfs->iVersion>1 && ((Testvfs *)pVfs->pAppData)->isNoshm ){ pMethods->xShmOpen = 0; pMethods->xShmClose = 0; pMethods->xShmLock = 0; pMethods->xShmBarrier = 0; pMethods->xShmMap = 0; @@ -1298,10 +1307,11 @@ int i; int isNoshm = 0; /* True if -noshm is passed */ int isDefault = 0; /* True if -default is passed */ int szOsFile = 0; /* Value passed to -szosfile */ int mxPathname = -1; /* Value passed to -mxpathname */ + int iVersion = 2; /* Value passed to -iversion */ if( objc<2 || 0!=(objc%2) ) goto bad_args; for(i=2; i2 && 0==strncmp("-mxpathname", zSwitch, nSwitch) ){ if( Tcl_GetIntFromObj(interp, objv[i+1], &mxPathname) ){ return TCL_ERROR; } + } + else if( nSwitch>2 && 0==strncmp("-iversion", zSwitch, nSwitch) ){ + if( Tcl_GetIntFromObj(interp, objv[i+1], &iVersion) ){ + return TCL_ERROR; + } } else{ goto bad_args; } } @@ -1357,10 +1372,11 @@ memcpy(p->zName, zVfs, strlen(zVfs)+1); pVfs = (sqlite3_vfs *)ckalloc(sizeof(sqlite3_vfs)); memcpy(pVfs, &tvfs_vfs, sizeof(sqlite3_vfs)); pVfs->pAppData = (void *)p; + pVfs->iVersion = iVersion; pVfs->zName = p->zName; pVfs->mxPathname = p->pParent->mxPathname; if( mxPathname>=0 && mxPathnamemxPathname ){ pVfs->mxPathname = mxPathname; } @@ -1372,15 +1388,15 @@ sqlite3_vfs_register(pVfs, isDefault); return TCL_OK; bad_args: - Tcl_WrongNumArgs(interp, 1, objv, "VFSNAME ?-noshm BOOL? ?-default BOOL? ?-mxpathname INT? ?-szosfile INT?"); + Tcl_WrongNumArgs(interp, 1, objv, "VFSNAME ?-noshm BOOL? ?-default BOOL? ?-mxpathname INT? ?-szosfile INT? ?-iversion INT?"); return TCL_ERROR; } int Sqlitetestvfs_Init(Tcl_Interp *interp){ Tcl_CreateObjCommand(interp, "testvfs", testvfs_cmd, 0, 0); return TCL_OK; } #endif Index: test/pager1.test ================================================================== --- test/pager1.test +++ test/pager1.test @@ -1090,10 +1090,32 @@ catch { set J -1 ; set J [file size test.db-journal] } catch { set W -1 ; set W [file size test.db-wal] } do_test pager1-7.1.$tn.2 { list $J $W } [list $js $ws] } } + +do_test pager1-7.2.1 { + faultsim_delete_and_reopen + execsql { + PRAGMA locking_mode = EXCLUSIVE; + CREATE TABLE t1(a, b); + BEGIN; + PRAGMA journal_mode = delete; + PRAGMA journal_mode = truncate; + } +} {exclusive delete truncate} +do_test pager1-7.2.2 { + execsql { INSERT INTO t1 VALUES(1, 2) } + execsql { PRAGMA journal_mode = persist } +} {truncate} +do_test pager1-7.2.3 { + execsql { COMMIT } + execsql { + PRAGMA journal_mode = persist; + PRAGMA journal_size_limit; + } +} {persist -1} #------------------------------------------------------------------------- # The following tests, pager1-8.*, test that the special filenames # ":memory:" and "" open temporary databases. # @@ -1754,9 +1776,180 @@ } recursive_select 32 execsql COMMIT } {} - +#------------------------------------------------------------------------- +# Test that a WAL database may not be opened if: +# +# pager1-21.1.*: The VFS has an iVersion less than 2, or +# pager1-21.2.*: The VFS does not provide xShmXXX() methods. +# +do_test pager1-21.0 { + faultsim_delete_and_reopen + execsql { + PRAGMA journal_mode = WAL; + CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def'); + INSERT INTO ko DEFAULT VALUES; + } +} {wal} +do_test pager1-21.1 { + testvfs tv -noshm 1 + sqlite3 db2 test.db -vfs tv + catchsql { SELECT * FROM ko } db2 +} {1 {unable to open database file}} +db2 close +tv delete +do_test pager1-21.2 { +breakpoint + testvfs tv -iversion 1 + sqlite3 db2 test.db -vfs tv + catchsql { SELECT * FROM ko } db2 +} {1 {unable to open database file}} +db2 close +tv delete + +#------------------------------------------------------------------------- +# Test that a "PRAGMA wal_checkpoint": +# +# pager1-22.1.*: is a no-op on a non-WAL db, and +# pager1-22.2.*: does not cause xSync calls with a synchronous=off db. +# +do_test pager1-22.1.1 { + faultsim_delete_and_reopen + execsql { + CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def'); + INSERT INTO ko DEFAULT VALUES; + } + execsql { PRAGMA wal_checkpoint } +} {} +do_test pager1-22.2.1 { + testvfs tv -default 1 + tv filter xSync + tv script xSyncCb + proc xSyncCb {args} {incr ::synccount} + set ::synccount 0 + sqlite3 db test.db + execsql { + PRAGMA synchronous = off; + PRAGMA journal_mode = WAL; + INSERT INTO ko DEFAULT VALUES; + } + execsql { PRAGMA wal_checkpoint } + set synccount +} {0} +db close +tv delete + +#------------------------------------------------------------------------- +# Tests for changing journal mode. +# +# pager1-23.1.*: Test that when changing from PERSIST to DELETE mode, +# the journal file is deleted. +# +# pager1-23.2.*: Same test as above, but while a shared lock is held +# on the database file. +# +# pager1-23.3.*: Same test as above, but while a reserved lock is held +# on the database file. +# +# pager1-23.4.*: And, for fun, while holding an exclusive lock. +# +# pager1-23.5.*: Try to set various different journal modes with an +# in-memory database (only MEMORY and OFF should work). +# +do_test pager1-23.1.1 { + faultsim_delete_and_reopen + execsql { + PRAGMA journal_mode = PERSIST; + CREATE TABLE t1(a, b); + } + file exists test.db-journal +} {1} +do_test pager1-23.1.2 { + execsql { PRAGMA journal_mode = DELETE } + file exists test.db-journal +} {0} + +do_test pager1-23.2.1 { + execsql { + PRAGMA journal_mode = PERSIST; + INSERT INTO t1 VALUES('Canberra', 'ACT'); + } + db eval { SELECT * FROM t1 } { + db eval { PRAGMA journal_mode = DELETE } + } + execsql { PRAGMA journal_mode } +} {delete} +do_test pager1-23.2.2 { + file exists test.db-journal +} {0} + +do_test pager1-23.3.1 { + execsql { + PRAGMA journal_mode = PERSIST; + INSERT INTO t1 VALUES('Darwin', 'NT'); + BEGIN IMMEDIATE; + } + db eval { PRAGMA journal_mode = DELETE } + execsql { PRAGMA journal_mode } +} {delete} +do_test pager1-23.3.2 { + file exists test.db-journal +} {0} +do_test pager1-23.3.3 { + execsql COMMIT +} {} + +do_test pager1-23.4.1 { + execsql { + PRAGMA journal_mode = PERSIST; + INSERT INTO t1 VALUES('Adelaide', 'SA'); + BEGIN EXCLUSIVE; + } + db eval { PRAGMA journal_mode = DELETE } + execsql { PRAGMA journal_mode } +} {delete} +do_test pager1-23.4.2 { + file exists test.db-journal +} {0} +do_test pager1-23.4.3 { + execsql COMMIT +} {} + +do_test pager1-23.5.1 { + faultsim_delete_and_reopen + sqlite3 db :memory: +} {} +foreach {tn mode possible} { + 2 off 1 + 3 memory 1 + 4 persist 0 + 5 delete 0 + 6 wal 0 + 7 truncate 0 +} { + do_test pager1-23.5.$tn.1 { + execsql "PRAGMA journal_mode = off" + execsql "PRAGMA journal_mode = $mode" + } [if $possible {list $mode} {list off}] + do_test pager1-23.5.$tn.2 { + execsql "PRAGMA journal_mode = memory" + execsql "PRAGMA journal_mode = $mode" + } [if $possible {list $mode} {list memory}] +} +do_test pager1-23.8.1 { + execsql {PRAGMA locking_mode = normal} +} {exclusive} +do_test pager1-23.8.2 { + execsql {PRAGMA locking_mode = exclusive} +} {exclusive} +do_test pager1-23.8.3 { + execsql {PRAGMA locking_mode} +} {exclusive} +do_test pager1-23.8.4 { + execsql {PRAGMA main.locking_mode} +} {exclusive} + finish_test Index: test/pagerfault.test ================================================================== --- test/pagerfault.test +++ test/pagerfault.test @@ -612,12 +612,10 @@ execsql { CREATE TABLE xx(a, b) } } -test { faultsim_test_result {0 {}} } -} - #--------------------------------------------------------------------------- # Test fault injection into a small backup operation. # do_test pagerfault-14-pre1 { @@ -710,7 +708,203 @@ faultsim_test_result {0 {exclusive wal delete wal persist}} faultsim_integrity_check } +#------------------------------------------------------------------------- +# Test fault injection while changing into and out of WAL mode. +# +do_test pagerfault-17-pre1 { + faultsim_delete_and_reopen + execsql { + CREATE TABLE t1(a PRIMARY KEY, b); + INSERT INTO t1 VALUES(1862, 'Botha'); + INSERT INTO t1 VALUES(1870, 'Smuts'); + INSERT INTO t1 VALUES(1866, 'Hertzog'); + } + faultsim_save_and_close +} {} +do_faultsim_test pagerfault-17a -prep { + faultsim_restore_and_reopen +} -body { + execsql { + PRAGMA journal_mode = wal; + PRAGMA journal_mode = delete; + } +} -test { + faultsim_test_result {0 {wal delete}} + faultsim_integrity_check +} +do_faultsim_test pagerfault-17b -prep { + faultsim_restore_and_reopen + execsql { PRAGMA synchronous = OFF } +} -body { + execsql { + PRAGMA journal_mode = wal; + INSERT INTO t1 VALUES(22, 'Clarke'); + PRAGMA journal_mode = delete; + } +} -test { + faultsim_test_result {0 {wal delete}} + faultsim_integrity_check +} +do_faultsim_test pagerfault-17c -prep { + faultsim_restore_and_reopen + execsql { + PRAGMA locking_mode = exclusive; + PRAGMA journal_mode = wal; + } +} -body { + execsql { PRAGMA journal_mode = delete } +} -test { + faultsim_test_result {0 delete} + faultsim_integrity_check +} +do_faultsim_test pagerfault-17d -prep { + faultsim_restore_and_reopen + sqlite3 db2 test.db + execsql { PRAGMA journal_mode = delete } + execsql { PRAGMA journal_mode = wal } + execsql { INSERT INTO t1 VALUES(99, 'Bradman') } db2 +} -body { + execsql { PRAGMA journal_mode = delete } +} -test { + faultsim_test_result {1 {database is locked}} + faultsim_integrity_check +} +do_faultsim_test pagerfault-17e -prep { + faultsim_restore_and_reopen + sqlite3 db2 test.db + execsql { PRAGMA journal_mode = delete } + execsql { PRAGMA journal_mode = wal } + set ::chan [launch_testfixture] + testfixture $::chan { + sqlite3 db test.db + db eval { INSERT INTO t1 VALUES(101, 'Latham') } + } + catch { testfixture $::chan sqlite_abort } + catch { close $::chan } +} -body { + execsql { PRAGMA journal_mode = delete } +} -test { + faultsim_test_result {0 delete} + faultsim_integrity_check +} + +#------------------------------------------------------------------------- +# Test fault-injection when changing from journal_mode=persist to +# journal_mode=delete (this involves deleting the journal file). +# +do_test pagerfault-18-pre1 { + faultsim_delete_and_reopen + execsql { + CREATE TABLE qq(x); + INSERT INTO qq VALUES('Herbert'); + INSERT INTO qq VALUES('Macalister'); + INSERT INTO qq VALUES('Mackenzie'); + INSERT INTO qq VALUES('Lilley'); + INSERT INTO qq VALUES('Palmer'); + } + faultsim_save_and_close +} {} +do_faultsim_test pagerfault-18 -prep { + faultsim_restore_and_reopen + execsql { + PRAGMA journal_mode = PERSIST; + INSERT INTO qq VALUES('Beatty'); + } +} -body { + execsql { PRAGMA journal_mode = delete } +} -test { + faultsim_test_result {0 delete} + faultsim_integrity_check +} + +} + +do_faultsim_test pagerfault-19a -prep { + sqlite3 db :memory: + db func a_string a_string + execsql { + PRAGMA auto_vacuum = FULL; + BEGIN; + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(a_string(5000), a_string(6000)); + COMMIT; + } +} -body { + execsql { + CREATE TABLE t2(a, b); + INSERT INTO t2 SELECT * FROM t1; + DELETE FROM t1; + } +} -test { + faultsim_test_result {0 {}} +} + +do_test pagerfault-19-pre1 { + faultsim_delete_and_reopen + execsql { + PRAGMA auto_vacuum = FULL; + CREATE TABLE t1(x); INSERT INTO t1 VALUES(1); + CREATE TABLE t2(x); INSERT INTO t2 VALUES(2); + CREATE TABLE t3(x); INSERT INTO t3 VALUES(3); + CREATE TABLE t4(x); INSERT INTO t4 VALUES(4); + CREATE TABLE t5(x); INSERT INTO t5 VALUES(5); + CREATE TABLE t6(x); INSERT INTO t6 VALUES(6); + } + faultsim_save_and_close +} {} +do_faultsim_test pagerfault-19b -prep { + faultsim_restore_and_reopen +} -body { + execsql { + BEGIN; + UPDATE t4 SET x = x+1; + UPDATE t6 SET x = x+1; + SAVEPOINT one; + UPDATE t3 SET x = x+1; + SAVEPOINT two; + DROP TABLE t2; + ROLLBACK TO one; + COMMIT; + SELECT * FROM t3; + SELECT * FROM t4; + SELECT * FROM t6; + } +} -test { + faultsim_test_result {0 {3 5 7}} +} + +do_test pagerfault-20-pre1 { + faultsim_delete_and_reopen + db func a_string a_string + execsql { + CREATE TABLE x1(x, y, z, PRIMARY KEY(y, z)); + INSERT INTO x1 VALUES(a_string(400), a_string(500), a_string(600)); + INSERT INTO x1 SELECT a_string(600), a_string(400), a_string(500) FROM x1; + INSERT INTO x1 SELECT a_string(500), a_string(600), a_string(400) FROM x1; + INSERT INTO x1 SELECT a_string(400), a_string(500), a_string(600) FROM x1; + INSERT INTO x1 SELECT a_string(600), a_string(400), a_string(500) FROM x1; + INSERT INTO x1 SELECT a_string(500), a_string(600), a_string(400) FROM x1; + INSERT INTO x1 SELECT a_string(400), a_string(500), a_string(600) FROM x1; + } + faultsim_save_and_close +} {} +do_faultsim_test pagerfault-20 -prep { + faultsim_restore_and_reopen + db func a_string a_string +} -body { + execsql { + BEGIN; + UPDATE x1 SET z = a_string(300); + DELETE FROM x1 WHERE rowid<32; + COMMIT; + } +} -test { + faultsim_test_result {0 {}} + faultsim_integrity_check + set nRow [db one {SELECT count(*) FROM x1}] + if {$nRow!=33 && $nRow!=64} {error "Wrong number of rows $nRow"} +} finish_test