Skip to content

Commit 48c457e

Browse files
committed
refactor: simplify SQL savepoint tracking
1 parent 9879478 commit 48c457e

2 files changed

Lines changed: 39 additions & 29 deletions

File tree

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

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

714714
/**
715-
* User savepoints active in the current transaction, from outermost to innermost.
715+
* User savepoints in a transaction opened by a SAVEPOINT statement.
716716
*
717-
* @var string[]
718-
*/
719-
private $transaction_savepoints = array();
720-
721-
/**
722-
* Whether the current transaction was opened by the outermost user savepoint.
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.
723720
*
724-
* @var bool
721+
* Savepoints inside a transaction opened by BEGIN are not tracked because
722+
* releasing them cannot end the outer transaction.
723+
*
724+
* @var string[]
725725
*/
726-
private $transaction_started_by_savepoint = false;
726+
private $savepoint_transaction_stack = array();
727727

728728
/**
729729
* Whether a MySQL table lock is active.
@@ -2106,9 +2106,8 @@ private function begin_user_transaction(): void {
21062106
* @see self::begin_wrapper_transaction()
21072107
*/
21082108
$this->connection->query( 'BEGIN IMMEDIATE' );
2109-
$this->in_transaction = true;
2110-
$this->transaction_savepoints = array();
2111-
$this->transaction_started_by_savepoint = false;
2109+
$this->in_transaction = true;
2110+
$this->savepoint_transaction_stack = array();
21122111
}
21132112

21142113
/**
@@ -2120,9 +2119,8 @@ private function commit_user_transaction(): void {
21202119
return;
21212120
}
21222121
$this->connection->query( 'COMMIT' );
2123-
$this->in_transaction = false;
2124-
$this->transaction_savepoints = array();
2125-
$this->transaction_started_by_savepoint = false;
2122+
$this->in_transaction = false;
2123+
$this->savepoint_transaction_stack = array();
21262124
}
21272125

21282126
/**
@@ -2134,9 +2132,8 @@ private function rollback_user_transaction(): void {
21342132
return;
21352133
}
21362134
$this->connection->query( 'ROLLBACK' );
2137-
$this->in_transaction = false;
2138-
$this->transaction_savepoints = array();
2139-
$this->transaction_started_by_savepoint = false;
2135+
$this->in_transaction = false;
2136+
$this->savepoint_transaction_stack = array();
21402137
}
21412138

21422139
/**
@@ -2178,7 +2175,7 @@ private function execute_transaction_or_locking_statement( WP_Parser_Node $node
21782175
$this->execute_sqlite_query( sprintf( 'ROLLBACK TO SAVEPOINT %s', $savepoint_name ) );
21792176
$savepoint_index = $this->find_transaction_savepoint_index( $savepoint_key );
21802177
if ( null !== $savepoint_index ) {
2181-
$this->transaction_savepoints = array_slice( $this->transaction_savepoints, 0, $savepoint_index + 1 );
2178+
$this->savepoint_transaction_stack = array_slice( $this->savepoint_transaction_stack, 0, $savepoint_index + 1 );
21822179
}
21832180
}
21842181
return;
@@ -2188,11 +2185,10 @@ private function execute_transaction_or_locking_statement( WP_Parser_Node $node
21882185
if ( WP_MySQL_Lexer::SAVEPOINT_SYMBOL === $token->id ) {
21892186
$starts_transaction = ! $this->inTransaction();
21902187
$this->execute_sqlite_query( sprintf( 'SAVEPOINT %s', $savepoint_name ) );
2191-
if ( $starts_transaction ) {
2192-
$this->transaction_started_by_savepoint = true;
2188+
if ( $starts_transaction || ! empty( $this->savepoint_transaction_stack ) ) {
2189+
$this->savepoint_transaction_stack[] = $savepoint_key;
21932190
}
2194-
$this->transaction_savepoints[] = $savepoint_key;
2195-
$this->in_transaction = true;
2191+
$this->in_transaction = true;
21962192
return;
21972193
}
21982194

@@ -2201,11 +2197,10 @@ private function execute_transaction_or_locking_statement( WP_Parser_Node $node
22012197
$this->execute_sqlite_query( sprintf( 'RELEASE SAVEPOINT %s', $savepoint_name ) );
22022198
$savepoint_index = $this->find_transaction_savepoint_index( $savepoint_key );
22032199
if ( null !== $savepoint_index ) {
2204-
$this->transaction_savepoints = array_slice( $this->transaction_savepoints, 0, $savepoint_index );
2200+
$this->savepoint_transaction_stack = array_slice( $this->savepoint_transaction_stack, 0, $savepoint_index );
22052201
}
2206-
if ( $this->transaction_started_by_savepoint && empty( $this->transaction_savepoints ) ) {
2207-
$this->in_transaction = false;
2208-
$this->transaction_started_by_savepoint = false;
2202+
if ( null !== $savepoint_index && empty( $this->savepoint_transaction_stack ) ) {
2203+
$this->in_transaction = false;
22092204
}
22102205
return;
22112206
}
@@ -2284,8 +2279,8 @@ private function execute_transaction_or_locking_statement( WP_Parser_Node $node
22842279
* @return int|null Savepoint index, or null when not tracked.
22852280
*/
22862281
private function find_transaction_savepoint_index( string $savepoint_name ): ?int {
2287-
for ( $index = count( $this->transaction_savepoints ) - 1; $index >= 0; $index-- ) {
2288-
if ( $savepoint_name === $this->transaction_savepoints[ $index ] ) {
2282+
for ( $index = count( $this->savepoint_transaction_stack ) - 1; $index >= 0; $index-- ) {
2283+
if ( $savepoint_name === $this->savepoint_transaction_stack[ $index ] ) {
22892284
return $index;
22902285
}
22912286
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,21 @@ public function test_write_inside_savepoint_commits_on_release(): void {
953953
$this->assertSame( '2', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
954954
}
955955

956+
public function test_releasing_savepoint_inside_explicit_transaction_keeps_transaction_active(): void {
957+
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
958+
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
959+
960+
$this->driver->query( 'START TRANSACTION' );
961+
$this->driver->query( 'SAVEPOINT nested_transaction' );
962+
$this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
963+
$this->driver->query( 'RELEASE SAVEPOINT nested_transaction' );
964+
965+
$this->assertTrue( $this->driver->inTransaction() );
966+
$this->driver->query( 'ROLLBACK' );
967+
$this->assertFalse( $this->driver->inTransaction() );
968+
$this->assertSame( '1', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
969+
}
970+
956971
public function test_write_inside_savepoint_can_be_rolled_back(): void {
957972
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
958973
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );

0 commit comments

Comments
 (0)