Index: src/build.c ================================================================== --- src/build.c +++ src/build.c @@ -2636,10 +2636,11 @@ /* Remove the table entry from SQLite's internal schema and modify ** the schema cookie. */ if( IsVirtual(pTab) ){ sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); + sqlite3MayAbort(pParse); } sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); sqlite3ChangeCookie(pParse, iDb); sqliteViewResetAll(db, iDb); } Index: src/vdbe.c ================================================================== --- src/vdbe.c +++ src/vdbe.c @@ -6847,10 +6847,11 @@ */ case OP_VDestroy: { db->nVDestroy++; rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); db->nVDestroy--; + assert( p->errorAction==OE_Abort && p->usesStmtJournal ); if( rc ) goto abort_due_to_error; break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ Index: src/vdbeaux.c ================================================================== --- src/vdbeaux.c +++ src/vdbeaux.c @@ -601,10 +601,11 @@ sIter.v = v; while( (pOp = opIterNext(&sIter))!=0 ){ int opcode = pOp->opcode; if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename + || opcode==OP_VDestroy || ((opcode==OP_Halt || opcode==OP_HaltIfNull) && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) ){ hasAbort = 1; break; ADDED test/vtabdrop.test Index: test/vtabdrop.test ================================================================== --- /dev/null +++ test/vtabdrop.test @@ -0,0 +1,127 @@ +# 2018 December 28 +# +# 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. +# +#*********************************************************************** +# +# The tests in this file test edge cases surrounding DROP TABLE on +# virtual tables. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +ifcapable !vtab { finish_test ; return } +source $testdir/fts3_common.tcl +source $testdir/malloc_common.tcl + +set testprefix vtabdrop + +#------------------------------------------------------------------------- +# Test that if a DROP TABLE is executed against an rtree table, but the +# xDestroy() call fails, the rtree table is not dropped, the sqlite_master +# table is not modified and the internal schema remains intact. +# +ifcapable rtree { + do_execsql_test 1.0 { + CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2); + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + } + + do_test 1.1 { + execsql { + BEGIN; + INSERT INTO t1 VALUES(3, 4); + } + db eval { SELECT * FROM t1 } { + catchsql { DROP TABLE rt } + } + execsql COMMIT + } {} + + do_execsql_test 1.2 { + SELECT name FROM sqlite_master ORDER BY 1; + SELECT * FROM t1; + SELECT * FROM rt; + } {rt rt_node rt_parent rt_rowid t1 1 2 3 4} + + db close + sqlite3 db test.db + + do_execsql_test 1.3 { + SELECT name FROM sqlite_master ORDER BY 1; + } {rt rt_node rt_parent rt_rowid t1} +} + +#------------------------------------------------------------------------- +# Same as tests 1.*, except with fts5 instead of rtree. +# +ifcapable fts5 { + reset_db + do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft USING fts5(x); + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + } + + do_test 2.1 { + execsql { + BEGIN; + INSERT INTO t1 VALUES(3, 4); + } + db eval { SELECT * FROM t1 } { + catchsql { DROP TABLE ft } + } + execsql COMMIT + } {} + + do_execsql_test 2.2 { + SELECT name FROM sqlite_master ORDER BY 1; + } {ft ft_config ft_content ft_data ft_docsize ft_idx t1} + + db close + sqlite3 db test.db + + do_execsql_test 2.3 { + SELECT name FROM sqlite_master ORDER BY 1; + } {ft ft_config ft_content ft_data ft_docsize ft_idx t1} +} + +#------------------------------------------------------------------------- +# Same as tests 1.*, except with fts3 instead of rtree. +# +ifcapable fts3 { + reset_db + do_execsql_test 2.0 { + CREATE VIRTUAL TABLE ft USING fts3(x); + CREATE TABLE t1(x, y); + INSERT INTO t1 VALUES(1, 2); + } + + do_test 2.1 { + execsql { + BEGIN; + INSERT INTO t1 VALUES(3, 4); + } + db eval { SELECT * FROM t1 } { + catchsql { DROP TABLE ft } + } + execsql COMMIT + } {} + + do_execsql_test 2.2 { + SELECT name FROM sqlite_master ORDER BY 1; + } {ft ft_content ft_segdir ft_segments sqlite_autoindex_ft_segdir_1 t1} + + db close + sqlite3 db test.db + + do_execsql_test 2.3 { + SELECT name FROM sqlite_master ORDER BY 1; + } {ft ft_content ft_segdir ft_segments sqlite_autoindex_ft_segdir_1 t1} +} + +finish_test