Skip to content

Commit 5ca9ea7

Browse files
committed
Align savepoint handling with MySQL semantics
In MySQL, savepoints exist only within a transaction. With autocommit enabled, each statement forms its own implicit transaction, so a SAVEPOINT statement succeeds but the savepoint is discarded as soon as the statement completes. It does not start a transaction. In SQLite, a bare SAVEPOINT opens a transaction. Tracking that transaction makes the following write succeed, but it runs without a write lock, an unrelated statement error rolls it back, and it is lost entirely when the connection closes without a RELEASE. MySQL commits it. Skip the SQLite statement when no transaction is active instead, and report the MySQL error 1305 when a savepoint is referenced outside of a transaction. This makes the savepoint transaction stack unnecessary, so it is removed. MySQL and SQLite also disagree on what a savepoint name refers to. When a name is reused, MySQL deletes the original savepoint, while SQLite keeps it on the stack, shadowed by the new one. Track the savepoint names of the active transaction and resolve every ROLLBACK TO and RELEASE against them, so that a name MySQL considers deleted reports error 1305 instead of reaching the shadowed SQLite savepoint. The savepoint tests are updated accordingly: those that relied on a bare SAVEPOINT opening a transaction now open one with START TRANSACTION. Fixes WordPress#495
1 parent 48c457e commit 5ca9ea7

5 files changed

Lines changed: 195 additions & 87 deletions

File tree

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

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -712,18 +712,15 @@ class WP_MySQL_On_SQLite extends PDO {
712712
private $in_transaction = false;
713713

714714
/**
715-
* User savepoints in a transaction opened by a SAVEPOINT statement.
715+
* Names of active user savepoints, outermost first.
716716
*
717-
* On PHP < 8.4, PDO SQLite cannot detect transactions opened with raw SQL.
718-
* Tracking the savepoint stack keeps the inTransaction() polyfill accurate
719-
* when the outermost savepoint is released.
720-
*
721-
* Savepoints inside a transaction opened by BEGIN are not tracked because
722-
* releasing them cannot end the outer transaction.
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.
723720
*
724721
* @var string[]
725722
*/
726-
private $savepoint_transaction_stack = array();
723+
private $savepoint_names = array();
727724

728725
/**
729726
* Whether a MySQL table lock is active.
@@ -2106,8 +2103,8 @@ private function begin_user_transaction(): void {
21062103
* @see self::begin_wrapper_transaction()
21072104
*/
21082105
$this->connection->query( 'BEGIN IMMEDIATE' );
2109-
$this->in_transaction = true;
2110-
$this->savepoint_transaction_stack = array();
2106+
$this->in_transaction = true;
2107+
$this->savepoint_names = array();
21112108
}
21122109

21132110
/**
@@ -2119,8 +2116,8 @@ private function commit_user_transaction(): void {
21192116
return;
21202117
}
21212118
$this->connection->query( 'COMMIT' );
2122-
$this->in_transaction = false;
2123-
$this->savepoint_transaction_stack = array();
2119+
$this->in_transaction = false;
2120+
$this->savepoint_names = array();
21242121
}
21252122

21262123
/**
@@ -2132,8 +2129,8 @@ private function rollback_user_transaction(): void {
21322129
return;
21332130
}
21342131
$this->connection->query( 'ROLLBACK' );
2135-
$this->in_transaction = false;
2136-
$this->savepoint_transaction_stack = array();
2132+
$this->in_transaction = false;
2133+
$this->savepoint_names = array();
21372134
}
21382135

21392136
/**
@@ -2172,36 +2169,49 @@ private function execute_transaction_or_locking_statement( WP_Parser_Node $node
21722169
if ( null === $savepoint_name ) {
21732170
$this->rollback_user_transaction();
21742171
} else {
2175-
$this->execute_sqlite_query( sprintf( 'ROLLBACK TO SAVEPOINT %s', $savepoint_name ) );
2176-
$savepoint_index = $this->find_transaction_savepoint_index( $savepoint_key );
2177-
if ( null !== $savepoint_index ) {
2178-
$this->savepoint_transaction_stack = array_slice( $this->savepoint_transaction_stack, 0, $savepoint_index + 1 );
2172+
// ROLLBACK TO keeps the named savepoint and deletes those created after it.
2173+
$index = array_search( $savepoint_key, $this->savepoint_names, true );
2174+
if ( false === $index ) {
2175+
throw $this->new_savepoint_does_not_exist_exception( $savepoint_name );
21792176
}
2177+
$this->execute_sqlite_query( sprintf( 'ROLLBACK TO SAVEPOINT %s', $savepoint_name ) );
2178+
array_splice( $this->savepoint_names, $index + 1 );
21802179
}
21812180
return;
21822181
}
21832182

21842183
// SAVEPOINT.
21852184
if ( WP_MySQL_Lexer::SAVEPOINT_SYMBOL === $token->id ) {
2186-
$starts_transaction = ! $this->inTransaction();
2185+
// In MySQL with autocommit enabled, a standalone savepoint is discarded
2186+
// immediately without starting a transaction.
2187+
if ( ! $this->inTransaction() ) {
2188+
return;
2189+
}
21872190
$this->execute_sqlite_query( sprintf( 'SAVEPOINT %s', $savepoint_name ) );
2188-
if ( $starts_transaction || ! empty( $this->savepoint_transaction_stack ) ) {
2189-
$this->savepoint_transaction_stack[] = $savepoint_key;
2191+
2192+
/*
2193+
* MySQL deletes an existing savepoint when its name is reused, while
2194+
* SQLite keeps it on the stack, shadowed by the new one. Drop the old
2195+
* name so that it can no longer be referenced. The shadowed SQLite
2196+
* savepoint is harmless; it is discarded when the transaction ends.
2197+
*/
2198+
$index = array_search( $savepoint_key, $this->savepoint_names, true );
2199+
if ( false !== $index ) {
2200+
array_splice( $this->savepoint_names, $index, 1 );
21902201
}
2191-
$this->in_transaction = true;
2202+
$this->savepoint_names[] = $savepoint_key;
21922203
return;
21932204
}
21942205

21952206
// RELEASE SAVEPOINT.
21962207
if ( WP_MySQL_Lexer::RELEASE_SYMBOL === $token->id ) {
2197-
$this->execute_sqlite_query( sprintf( 'RELEASE SAVEPOINT %s', $savepoint_name ) );
2198-
$savepoint_index = $this->find_transaction_savepoint_index( $savepoint_key );
2199-
if ( null !== $savepoint_index ) {
2200-
$this->savepoint_transaction_stack = array_slice( $this->savepoint_transaction_stack, 0, $savepoint_index );
2201-
}
2202-
if ( null !== $savepoint_index && empty( $this->savepoint_transaction_stack ) ) {
2203-
$this->in_transaction = false;
2208+
// RELEASE deletes the named savepoint and those created after it.
2209+
$index = array_search( $savepoint_key, $this->savepoint_names, true );
2210+
if ( false === $index ) {
2211+
throw $this->new_savepoint_does_not_exist_exception( $savepoint_name );
22042212
}
2213+
$this->execute_sqlite_query( sprintf( 'RELEASE SAVEPOINT %s', $savepoint_name ) );
2214+
array_splice( $this->savepoint_names, $index );
22052215
return;
22062216
}
22072217

@@ -2272,21 +2282,6 @@ private function execute_transaction_or_locking_statement( WP_Parser_Node $node
22722282
);
22732283
}
22742284

2275-
/**
2276-
* Find the innermost active user savepoint with the given name.
2277-
*
2278-
* @param string $savepoint_name Normalized savepoint name.
2279-
* @return int|null Savepoint index, or null when not tracked.
2280-
*/
2281-
private function find_transaction_savepoint_index( string $savepoint_name ): ?int {
2282-
for ( $index = count( $this->savepoint_transaction_stack ) - 1; $index >= 0; $index-- ) {
2283-
if ( $savepoint_name === $this->savepoint_transaction_stack[ $index ] ) {
2284-
return $index;
2285-
}
2286-
}
2287-
return null;
2288-
}
2289-
22902285
/**
22912286
* Translate and execute a MySQL SELECT statement in SQLite.
22922287
*
@@ -7867,6 +7862,25 @@ private function new_table_not_found_exception(
78677862
);
78687863
}
78697864

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

‎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: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -923,36 +923,6 @@ 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_standalone_write_uses_wrapper_transaction(): 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-
$statement = $this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
931-
$queries = array_column( $this->driver->get_last_sqlite_queries(), 'sql' );
932-
933-
$this->assertSame( 1, $statement->rowCount() );
934-
$this->assertSame( 'BEGIN IMMEDIATE', $queries[0] );
935-
$this->assertSame( 'COMMIT', end( $queries ) );
936-
}
937-
938-
public function test_write_inside_savepoint_commits_on_release(): void {
939-
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
940-
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
941-
942-
$this->driver->query( 'SAVEPOINT outer_transaction' );
943-
$statement = $this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
944-
$queries = array_column( $this->driver->get_last_sqlite_queries(), 'sql' );
945-
946-
$this->assertSame( 1, $statement->rowCount() );
947-
$this->assertNotContains( 'BEGIN IMMEDIATE', $queries );
948-
$this->assertTrue( $this->driver->inTransaction() );
949-
950-
$this->driver->query( 'RELEASE SAVEPOINT outer_transaction' );
951-
952-
$this->assertFalse( $this->driver->inTransaction() );
953-
$this->assertSame( '2', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
954-
}
955-
956926
public function test_releasing_savepoint_inside_explicit_transaction_keeps_transaction_active(): void {
957927
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
958928
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
@@ -972,11 +942,15 @@ public function test_write_inside_savepoint_can_be_rolled_back(): void {
972942
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
973943
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
974944

945+
$this->driver->query( 'START TRANSACTION' );
975946
$this->driver->query( 'SAVEPOINT outer_transaction' );
976947
$this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
977948
$this->driver->query( 'ROLLBACK TO SAVEPOINT outer_transaction' );
978949
$this->driver->query( 'RELEASE SAVEPOINT outer_transaction' );
979950

951+
$this->assertTrue( $this->driver->inTransaction() );
952+
$this->driver->query( 'COMMIT' );
953+
980954
$this->assertFalse( $this->driver->inTransaction() );
981955
$this->assertSame( '1', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
982956
}
@@ -985,6 +959,7 @@ public function test_writes_inside_nested_savepoints_preserve_outer_changes(): v
985959
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
986960
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
987961

962+
$this->driver->query( 'START TRANSACTION' );
988963
$this->driver->query( 'SAVEPOINT outer_transaction' );
989964
$this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
990965
$this->driver->query( 'SAVEPOINT inner_transaction' );
@@ -997,44 +972,49 @@ public function test_writes_inside_nested_savepoints_preserve_outer_changes(): v
997972

998973
$this->driver->query( 'RELEASE SAVEPOINT outer_transaction' );
999974

975+
$this->assertTrue( $this->driver->inTransaction() );
976+
$this->driver->query( 'COMMIT' );
977+
1000978
$this->assertFalse( $this->driver->inTransaction() );
1001979
$this->assertSame( '2', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
1002980
}
1003981

1004-
public function test_duplicate_savepoint_names_track_innermost_scope(): void {
982+
public function test_duplicate_savepoint_names_roll_back_to_the_latest(): void {
1005983
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
1006984
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
1007985

986+
$this->driver->query( 'START TRANSACTION' );
1008987
$this->driver->query( 'SAVEPOINT repeated' );
1009988
$this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
1010989
$this->driver->query( 'SAVEPOINT repeated' );
1011990
$this->driver->query( 'UPDATE t SET value = 3 WHERE id = 1' );
1012-
$this->driver->query( 'ROLLBACK TO SAVEPOINT repeated' );
1013-
$this->driver->query( 'RELEASE SAVEPOINT repeated' );
1014991

1015-
$this->assertTrue( $this->driver->inTransaction() );
992+
// A reused name refers to the savepoint that was set last.
993+
$this->driver->query( 'ROLLBACK TO SAVEPOINT repeated' );
1016994
$this->assertSame( '2', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
1017995

1018-
$this->driver->query( 'RELEASE SAVEPOINT repeated' );
1019-
1020-
$this->assertFalse( $this->driver->inTransaction() );
996+
$this->driver->query( 'COMMIT' );
1021997
$this->assertSame( '2', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
1022998
}
1023999

10241000
public function test_quoted_savepoint_names_are_case_insensitive(): void {
10251001
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
10261002
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
10271003

1004+
$this->driver->query( 'START TRANSACTION' );
10281005
$this->driver->query( 'SAVEPOINT `MixedCase`' );
10291006
$this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
10301007
$this->driver->query( 'ROLLBACK TO SAVEPOINT `mixedcase`' );
10311008
$this->driver->query( 'RELEASE SAVEPOINT `MIXEDCASE`' );
10321009

1010+
$this->assertTrue( $this->driver->inTransaction() );
1011+
$this->driver->query( 'COMMIT' );
1012+
10331013
$this->assertFalse( $this->driver->inTransaction() );
10341014
$this->assertSame( '1', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
10351015
}
10361016

1037-
public function test_failed_write_cleans_up_savepoint_transaction_state(): void {
1017+
public function test_failed_write_after_standalone_savepoint_keeps_autocommit(): void {
10381018
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY)' );
10391019
$this->driver->query( 'INSERT INTO t VALUES (1)' );
10401020
$this->driver->query( 'SAVEPOINT outer_transaction' );

0 commit comments

Comments
 (0)