Skip to content

Commit 3cc53c7

Browse files
committed
Preserve transactions after missing savepoint errors
MySQL error 1305 leaves the surrounding transaction and existing savepoints active. Skip the driver's automatic rollback for this error and cover both rollback and release paths.
1 parent 5ca9ea7 commit 3cc53c7

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,11 +1193,16 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
11931193
$this->error_info = array( '00000', null, null );
11941194
return $stmt;
11951195
} catch ( Throwable $e ) {
1196-
try {
1197-
$this->rollback_user_transaction();
1198-
$this->table_lock_active = false;
1199-
} catch ( Throwable $rollback_exception ) {
1200-
// Ignore rollback errors.
1196+
// MySQL error 1305 reports a missing savepoint without ending the transaction.
1197+
$preserves_transaction = $e instanceof WP_MySQL_On_SQLite_Exception
1198+
&& 1305 === ( $e->errorInfo[1] ?? null );
1199+
if ( ! $preserves_transaction ) {
1200+
try {
1201+
$this->rollback_user_transaction();
1202+
$this->table_lock_active = false;
1203+
} catch ( Throwable $rollback_exception ) {
1204+
// Ignore rollback errors.
1205+
}
12011206
}
12021207
if ( $e instanceof WP_SQLite_Information_Schema_Exception ) {
12031208
$e = $this->convert_information_schema_exception( $e );

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7763,6 +7763,36 @@ public function testReleaseSavepointWithoutTransaction(): void {
77637763
);
77647764
}
77657765

7766+
/**
7767+
* @dataProvider missingSavepointStatements
7768+
*/
7769+
public function testMissingSavepointDoesNotRollbackTransaction( string $statement ): void {
7770+
$this->assertQuery( 'CREATE TABLE t (id INT)' );
7771+
$this->assertQuery( 'BEGIN' );
7772+
$this->assertQuery( 'SAVEPOINT existing' );
7773+
$this->assertQuery( 'INSERT INTO t VALUES (1)' );
7774+
7775+
$this->assertQueryError(
7776+
$statement,
7777+
sprintf( self::SAVEPOINT_DOES_NOT_EXIST_ERROR, 'missing' )
7778+
);
7779+
7780+
$this->assertTrue( $this->engine->inTransaction() );
7781+
$this->assertSame( '1', $this->assertQuery( 'SELECT id FROM t' )[0]->id );
7782+
7783+
$this->assertQuery( 'ROLLBACK TO SAVEPOINT existing' );
7784+
$this->assertQuery( 'RELEASE SAVEPOINT existing' );
7785+
$this->assertQuery( 'COMMIT' );
7786+
$this->assertSame( array(), $this->assertQuery( 'SELECT id FROM t' ) );
7787+
}
7788+
7789+
public static function missingSavepointStatements(): array {
7790+
return array(
7791+
'ROLLBACK TO SAVEPOINT' => array( 'ROLLBACK TO SAVEPOINT missing' ),
7792+
'RELEASE SAVEPOINT' => array( 'RELEASE SAVEPOINT missing' ),
7793+
);
7794+
}
7795+
77667796
public function testDuplicateSavepointNameReplacesOldSavepoint(): void {
77677797
$this->assertQuery( 'BEGIN' );
77687798
$this->assertQuery( 'SAVEPOINT sp1' );

0 commit comments

Comments
 (0)