Index: src/alter.c ================================================================== --- src/alter.c +++ src/alter.c @@ -164,19 +164,19 @@ pVTab = 0; } } #endif - /* Begin a transaction for database iDb. - ** Then modify the schema cookie (since the ALTER TABLE modifies the - ** schema). Open a statement transaction if the table is a virtual - ** table. - */ + /* Begin a transaction for database iDb. Then modify the schema cookie + ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(), + ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the + ** nested SQL may raise an exception. */ v = sqlite3GetVdbe(pParse); if( v==0 ){ goto exit_rename_table; } + sqlite3MayAbort(pParse); /* figure out how many UTF-8 characters are in zName */ zTabName = pTab->zName; nTabName = sqlite3Utf8CharLen(zTabName, -1); @@ -241,11 +241,10 @@ #ifndef SQLITE_OMIT_VIRTUALTABLE if( pVTab ){ int i = ++pParse->nMem; sqlite3VdbeLoadString(v, i, zName); sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB); - sqlite3MayAbort(pParse); } #endif renameReloadSchema(pParse, iDb); renameTestSchema(pParse, zDb, iDb==1); @@ -562,10 +561,11 @@ /* Do the rename operation using a recursive UPDATE statement that ** uses the sqlite_rename_column() SQL function to compute the new ** CREATE statement text for the sqlite_master table. */ + sqlite3MayAbort(pParse); zNew = sqlite3NameFromToken(db, pNew); if( !zNew ) goto exit_rename_column; assert( pNew->n>0 ); bQuote = sqlite3Isquote(pNew->z[0]); sqlite3NestedParse(pParse, Index: src/vdbeaux.c ================================================================== --- src/vdbeaux.c +++ src/vdbeaux.c @@ -635,10 +635,11 @@ while( (pOp = opIterNext(&sIter))!=0 ){ int opcode = pOp->opcode; if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename || opcode==OP_VDestroy + || (opcode==OP_Function0 && pOp->p4.pFunc->funcFlags&SQLITE_FUNC_INTERNAL) || ((opcode==OP_Halt || opcode==OP_HaltIfNull) && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort)) ){ hasAbort = 1; break; Index: test/altertab3.test ================================================================== --- test/altertab3.test +++ test/altertab3.test @@ -79,10 +79,41 @@ do_execsql_test 3.2 { SELECT sql FROM sqlite_master WHERE name = 'v1' } {{CREATE VIEW v1 AS SELECT * FROM t1 WHERE a=1 OR (bbb IN ())}} +#------------------------------------------------------------------------- +reset_db +do_execsql_test 4.0 { + CREATE TABLE t1(a, b); + CREATE TABLE t3(e, f); + CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN + INSERT INTO t2 VALUES(new.a, new.b); + END; +} + +do_catchsql_test 4.1.2 { + BEGIN; + ALTER TABLE t3 RENAME TO t4; +} {1 {error in trigger tr1: no such table: main.t2}} +do_execsql_test 4.1.2 { + COMMIT; +} +do_execsql_test 4.1.3 { + SELECT * FROM sqlite_master WHERE type='table' AND name!='t1'; +} {table t3 t3 3 {CREATE TABLE t3(e, f)}} + +do_catchsql_test 4.2.1 { + BEGIN; + ALTER TABLE t3 RENAME e TO eee; +} {1 {error in trigger tr1: no such table: main.t2}} +do_execsql_test 4.2.2 { + COMMIT; +} +do_execsql_test 4.2.3 { + SELECT * FROM sqlite_master WHERE type='table' AND name!='t1'; +} {table t3 t3 3 {CREATE TABLE t3(e, f)}} finish_test