Skip to content

Commit 4cfe406

Browse files
chubes4JanJakes
andauthored
Align savepoint handling with MySQL semantics (#496)
## What? Align SQL savepoint handling with MySQL semantics. A standalone `SAVEPOINT` in the default autocommit mode no longer opens an SQLite transaction. It is discarded immediately, so following writes use the normal `BEGIN IMMEDIATE` wrapper and commit independently. Inside explicit transactions, track active savepoint names to emulate MySQL behavior for nested savepoints, reused and case-insensitive names, `ROLLBACK TO`, `RELEASE`, and missing-savepoint error 1305. Missing-savepoint errors leave the surrounding transaction active. Add regression coverage across the MySQL-on-SQLite, concurrency, PDO API, and WordPress integration test suites. ## Why? SQLite treats a standalone `SAVEPOINT` as opening a transaction, but MySQL does not. Passing it through to SQLite created a hidden transaction on PHP versions before 8.4. Subsequent writes then either attempted a nested `BEGIN IMMEDIATE` or bypassed the normal write-locking path, depending on how the transaction state was tracked. The original Data Machine reproduction relied on SQLite-only behavior: issuing a bare `SAVEPOINT`, performing a write, and committing with `RELEASE SAVEPOINT`. Real MySQL and MariaDB discard the standalone savepoint, so the later release reports error 1305. That downstream issue is tracked in Extra-Chill/data-machine#3436. > [!NOTE] > Consumers that relied on a bare `SAVEPOINT` opening a transaction on SQLite must open an explicit transaction instead, matching MySQL behavior. Fixes #495 --------- Co-authored-by: Jan Jakeš <jan@jakes.pro>
1 parent abf0dac commit 4cfe406

5 files changed

Lines changed: 380 additions & 10 deletions

File tree

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

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,17 @@ class WP_MySQL_On_SQLite extends PDO {
711711
*/
712712
private $in_transaction = false;
713713

714+
/**
715+
* Names of active user savepoints, outermost first.
716+
*
717+
* MySQL replaces a savepoint with the same name, while SQLite only shadows it.
718+
* Tracking normalized names prevents shadowed SQLite savepoints from becoming
719+
* visible again and supports case-insensitive lookup.
720+
*
721+
* @var string[]
722+
*/
723+
private $savepoint_names = array();
724+
714725
/**
715726
* Whether a MySQL table lock is active.
716727
*
@@ -1182,11 +1193,16 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
11821193
$this->error_info = array( '00000', null, null );
11831194
return $stmt;
11841195
} catch ( Throwable $e ) {
1185-
try {
1186-
$this->rollback_user_transaction();
1187-
$this->table_lock_active = false;
1188-
} catch ( Throwable $rollback_exception ) {
1189-
// 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+
}
11901206
}
11911207
if ( $e instanceof WP_SQLite_Information_Schema_Exception ) {
11921208
$e = $this->convert_information_schema_exception( $e );
@@ -2092,7 +2108,8 @@ private function begin_user_transaction(): void {
20922108
* @see self::begin_wrapper_transaction()
20932109
*/
20942110
$this->connection->query( 'BEGIN IMMEDIATE' );
2095-
$this->in_transaction = true;
2111+
$this->in_transaction = true;
2112+
$this->savepoint_names = array();
20962113
}
20972114

20982115
/**
@@ -2104,7 +2121,8 @@ private function commit_user_transaction(): void {
21042121
return;
21052122
}
21062123
$this->connection->query( 'COMMIT' );
2107-
$this->in_transaction = false;
2124+
$this->in_transaction = false;
2125+
$this->savepoint_names = array();
21082126
}
21092127

21102128
/**
@@ -2116,7 +2134,8 @@ private function rollback_user_transaction(): void {
21162134
return;
21172135
}
21182136
$this->connection->query( 'ROLLBACK' );
2119-
$this->in_transaction = false;
2137+
$this->in_transaction = false;
2138+
$this->savepoint_names = array();
21202139
}
21212140

21222141
/**
@@ -2146,26 +2165,58 @@ private function execute_transaction_or_locking_statement( WP_Parser_Node $node
21462165
break;
21472166
case 'savepointStatement':
21482167
$savepoint_name = $this->translate( $subnode->get_first_child_node( 'identifier' ) );
2168+
$savepoint_key = null === $savepoint_name
2169+
? null
2170+
: strtolower( $this->unquote_sqlite_identifier( $savepoint_name ) );
21492171

21502172
// ROLLBACK/ROLLBACK TO SAVEPOINT <identifier>.
21512173
if ( WP_MySQL_Lexer::ROLLBACK_SYMBOL === $token->id ) {
21522174
if ( null === $savepoint_name ) {
21532175
$this->rollback_user_transaction();
21542176
} else {
2177+
// ROLLBACK TO keeps the named savepoint and deletes those created after it.
2178+
$index = array_search( $savepoint_key, $this->savepoint_names, true );
2179+
if ( false === $index ) {
2180+
throw $this->new_savepoint_does_not_exist_exception( $savepoint_name );
2181+
}
21552182
$this->execute_sqlite_query( sprintf( 'ROLLBACK TO SAVEPOINT %s', $savepoint_name ) );
2183+
array_splice( $this->savepoint_names, $index + 1 );
21562184
}
21572185
return;
21582186
}
21592187

21602188
// SAVEPOINT.
21612189
if ( WP_MySQL_Lexer::SAVEPOINT_SYMBOL === $token->id ) {
2190+
// In MySQL with autocommit enabled, a standalone savepoint is discarded
2191+
// immediately without starting a transaction.
2192+
if ( ! $this->inTransaction() ) {
2193+
return;
2194+
}
21622195
$this->execute_sqlite_query( sprintf( 'SAVEPOINT %s', $savepoint_name ) );
2196+
2197+
/*
2198+
* MySQL deletes an existing savepoint when its name is reused, while
2199+
* SQLite keeps it on the stack, shadowed by the new one. Drop the old
2200+
* name so that it can no longer be referenced. The shadowed SQLite
2201+
* savepoint is harmless; it is discarded when the transaction ends.
2202+
*/
2203+
$index = array_search( $savepoint_key, $this->savepoint_names, true );
2204+
if ( false !== $index ) {
2205+
array_splice( $this->savepoint_names, $index, 1 );
2206+
}
2207+
$this->savepoint_names[] = $savepoint_key;
21632208
return;
21642209
}
21652210

21662211
// RELEASE SAVEPOINT.
21672212
if ( WP_MySQL_Lexer::RELEASE_SYMBOL === $token->id ) {
2213+
// RELEASE deletes the named savepoint and those created after it.
2214+
$index = array_search( $savepoint_key, $this->savepoint_names, true );
2215+
if ( false === $index ) {
2216+
throw $this->new_savepoint_does_not_exist_exception( $savepoint_name );
2217+
}
21682218
$this->execute_sqlite_query( sprintf( 'RELEASE SAVEPOINT %s', $savepoint_name ) );
2219+
array_splice( $this->savepoint_names, $index );
21692220
return;
21702221
}
21712222

@@ -7816,6 +7867,25 @@ private function new_table_not_found_exception(
78167867
);
78177868
}
78187869

7870+
/**
7871+
* Create a MySQL-compatible savepoint-not-found exception.
7872+
*
7873+
* @param string $savepoint_name The missing savepoint name, as an SQLite identifier.
7874+
* @return WP_MySQL_On_SQLite_Exception
7875+
*/
7876+
private function new_savepoint_does_not_exist_exception( string $savepoint_name ): WP_MySQL_On_SQLite_Exception {
7877+
$driver_message = sprintf(
7878+
'SAVEPOINT %s does not exist',
7879+
$this->unquote_sqlite_identifier( $savepoint_name )
7880+
);
7881+
return $this->new_driver_exception(
7882+
'SQLSTATE[42000]: Syntax error or access violation: 1305 ' . $driver_message,
7883+
'42000',
7884+
null,
7885+
array( '42000', 1305, $driver_message )
7886+
);
7887+
}
7888+
78197889
/**
78207890
* Create a MySQL-compatible exception for an invalid SQL mode value.
78217891
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ public function testWriteQueryOpensWriteTransaction( string $query ): void {
7272
$this->assertSame( 'BEGIN IMMEDIATE', $driver->get_last_sqlite_queries()[0]['sql'] );
7373
}
7474

75+
public function testWriteQueryAfterSavepointOpensWriteTransaction(): void {
76+
$driver = $this->create_in_memory_driver();
77+
$driver->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
78+
$driver->query( "INSERT INTO t VALUES (1, 'Alice')" );
79+
80+
// A savepoint outside of a transaction must not leave a deferred SQLite
81+
// transaction open, which would make the following write skip the lock.
82+
$driver->query( 'SAVEPOINT sp1' );
83+
$driver->query( "UPDATE t SET name = 'Carol' WHERE id = 1" );
84+
85+
$this->assertSame( 'BEGIN IMMEDIATE', $driver->get_last_sqlite_queries()[0]['sql'] );
86+
}
87+
7588
public function provideWriteStatements(): array {
7689
return array(
7790
'INSERT' => array( "INSERT INTO t VALUES (2, 'Bob')" ),

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

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,118 @@ public function test_transaction_methods_flush_operation_state(): void {
923923
$this->assertSame( array( 'ROLLBACK' ), array_column( $this->driver->get_last_sqlite_queries(), 'sql' ) );
924924
}
925925

926+
public function test_releasing_savepoint_inside_explicit_transaction_keeps_transaction_active(): void {
927+
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
928+
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
929+
930+
$this->driver->query( 'START TRANSACTION' );
931+
$this->driver->query( 'SAVEPOINT nested_transaction' );
932+
$this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
933+
$this->driver->query( 'RELEASE SAVEPOINT nested_transaction' );
934+
935+
$this->assertTrue( $this->driver->inTransaction() );
936+
$this->driver->query( 'ROLLBACK' );
937+
$this->assertFalse( $this->driver->inTransaction() );
938+
$this->assertSame( '1', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
939+
}
940+
941+
public function test_write_inside_savepoint_can_be_rolled_back(): void {
942+
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
943+
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
944+
945+
$this->driver->query( 'START TRANSACTION' );
946+
$this->driver->query( 'SAVEPOINT outer_transaction' );
947+
$this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
948+
$this->driver->query( 'ROLLBACK TO SAVEPOINT outer_transaction' );
949+
$this->driver->query( 'RELEASE SAVEPOINT outer_transaction' );
950+
951+
$this->assertTrue( $this->driver->inTransaction() );
952+
$this->driver->query( 'COMMIT' );
953+
954+
$this->assertFalse( $this->driver->inTransaction() );
955+
$this->assertSame( '1', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
956+
}
957+
958+
public function test_writes_inside_nested_savepoints_preserve_outer_changes(): void {
959+
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
960+
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
961+
962+
$this->driver->query( 'START TRANSACTION' );
963+
$this->driver->query( 'SAVEPOINT outer_transaction' );
964+
$this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
965+
$this->driver->query( 'SAVEPOINT inner_transaction' );
966+
$this->driver->query( 'UPDATE t SET value = 3 WHERE id = 1' );
967+
$this->driver->query( 'ROLLBACK TO SAVEPOINT inner_transaction' );
968+
$this->driver->query( 'RELEASE SAVEPOINT inner_transaction' );
969+
970+
$this->assertTrue( $this->driver->inTransaction() );
971+
$this->assertSame( '2', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
972+
973+
$this->driver->query( 'RELEASE SAVEPOINT outer_transaction' );
974+
975+
$this->assertTrue( $this->driver->inTransaction() );
976+
$this->driver->query( 'COMMIT' );
977+
978+
$this->assertFalse( $this->driver->inTransaction() );
979+
$this->assertSame( '2', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
980+
}
981+
982+
public function test_duplicate_savepoint_names_roll_back_to_the_latest(): void {
983+
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
984+
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
985+
986+
$this->driver->query( 'START TRANSACTION' );
987+
$this->driver->query( 'SAVEPOINT repeated' );
988+
$this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
989+
$this->driver->query( 'SAVEPOINT repeated' );
990+
$this->driver->query( 'UPDATE t SET value = 3 WHERE id = 1' );
991+
992+
// A reused name refers to the savepoint that was set last.
993+
$this->driver->query( 'ROLLBACK TO SAVEPOINT repeated' );
994+
$this->assertSame( '2', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
995+
996+
$this->driver->query( 'COMMIT' );
997+
$this->assertSame( '2', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
998+
}
999+
1000+
public function test_quoted_savepoint_names_are_case_insensitive(): void {
1001+
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
1002+
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
1003+
1004+
$this->driver->query( 'START TRANSACTION' );
1005+
$this->driver->query( 'SAVEPOINT `MixedCase`' );
1006+
$this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
1007+
$this->driver->query( 'ROLLBACK TO SAVEPOINT `mixedcase`' );
1008+
$this->driver->query( 'RELEASE SAVEPOINT `MIXEDCASE`' );
1009+
1010+
$this->assertTrue( $this->driver->inTransaction() );
1011+
$this->driver->query( 'COMMIT' );
1012+
1013+
$this->assertFalse( $this->driver->inTransaction() );
1014+
$this->assertSame( '1', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
1015+
}
1016+
1017+
public function test_failed_write_after_standalone_savepoint_keeps_autocommit(): void {
1018+
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY)' );
1019+
$this->driver->query( 'INSERT INTO t VALUES (1)' );
1020+
$this->driver->query( 'SAVEPOINT outer_transaction' );
1021+
1022+
try {
1023+
$this->driver->query( 'INSERT INTO t VALUES (1)' );
1024+
$this->fail( 'Expected the duplicate insert to fail.' );
1025+
} catch ( PDOException $e ) {
1026+
$this->assertStringContainsString( 'UNIQUE constraint failed', $e->getMessage() );
1027+
}
1028+
1029+
$this->assertFalse( $this->driver->inTransaction() );
1030+
$this->assertSame( '1', $this->driver->query( 'SELECT COUNT(*) FROM t' )->fetchColumn() );
1031+
1032+
$this->driver->query( 'INSERT INTO t VALUES (2)' );
1033+
$queries = array_column( $this->driver->get_last_sqlite_queries(), 'sql' );
1034+
$this->assertSame( 'BEGIN IMMEDIATE', $queries[0] );
1035+
$this->assertSame( 'COMMIT', end( $queries ) );
1036+
}
1037+
9261038
public function test_fetch_default(): void {
9271039
// Default fetch mode is PDO::FETCH_BOTH.
9281040
$result = $this->driver->query( "SELECT 1, 'abc', 2" );

0 commit comments

Comments
 (0)