Skip to content

Commit 852f068

Browse files
committed
fix: track SQL savepoint transactions
1 parent f3dfbc0 commit 852f068

3 files changed

Lines changed: 180 additions & 3 deletions

File tree

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

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

714+
/**
715+
* User savepoints active in the current transaction, from outermost to innermost.
716+
*
717+
* @var string[]
718+
*/
719+
private $transaction_savepoints = array();
720+
721+
/**
722+
* Whether the current transaction was opened by the outermost user savepoint.
723+
*
724+
* @var bool
725+
*/
726+
private $transaction_started_by_savepoint = false;
727+
714728
/**
715729
* Whether a MySQL table lock is active.
716730
*
@@ -2092,7 +2106,9 @@ private function begin_user_transaction(): void {
20922106
* @see self::begin_wrapper_transaction()
20932107
*/
20942108
$this->connection->query( 'BEGIN IMMEDIATE' );
2095-
$this->in_transaction = true;
2109+
$this->in_transaction = true;
2110+
$this->transaction_savepoints = array();
2111+
$this->transaction_started_by_savepoint = false;
20962112
}
20972113

20982114
/**
@@ -2104,7 +2120,9 @@ private function commit_user_transaction(): void {
21042120
return;
21052121
}
21062122
$this->connection->query( 'COMMIT' );
2107-
$this->in_transaction = false;
2123+
$this->in_transaction = false;
2124+
$this->transaction_savepoints = array();
2125+
$this->transaction_started_by_savepoint = false;
21082126
}
21092127

21102128
/**
@@ -2116,7 +2134,9 @@ 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->transaction_savepoints = array();
2139+
$this->transaction_started_by_savepoint = false;
21202140
}
21212141

21222142
/**
@@ -2146,26 +2166,47 @@ private function execute_transaction_or_locking_statement( WP_Parser_Node $node
21462166
break;
21472167
case 'savepointStatement':
21482168
$savepoint_name = $this->translate( $subnode->get_first_child_node( 'identifier' ) );
2169+
$savepoint_key = null === $savepoint_name
2170+
? null
2171+
: strtolower( $this->unquote_sqlite_identifier( $savepoint_name ) );
21492172

21502173
// ROLLBACK/ROLLBACK TO SAVEPOINT <identifier>.
21512174
if ( WP_MySQL_Lexer::ROLLBACK_SYMBOL === $token->id ) {
21522175
if ( null === $savepoint_name ) {
21532176
$this->rollback_user_transaction();
21542177
} else {
21552178
$this->execute_sqlite_query( sprintf( 'ROLLBACK TO SAVEPOINT %s', $savepoint_name ) );
2179+
$savepoint_index = $this->find_transaction_savepoint_index( $savepoint_key );
2180+
if ( null !== $savepoint_index ) {
2181+
$this->transaction_savepoints = array_slice( $this->transaction_savepoints, 0, $savepoint_index + 1 );
2182+
}
21562183
}
21572184
return;
21582185
}
21592186

21602187
// SAVEPOINT.
21612188
if ( WP_MySQL_Lexer::SAVEPOINT_SYMBOL === $token->id ) {
2189+
$starts_transaction = ! $this->inTransaction();
21622190
$this->execute_sqlite_query( sprintf( 'SAVEPOINT %s', $savepoint_name ) );
2191+
if ( $starts_transaction ) {
2192+
$this->transaction_started_by_savepoint = true;
2193+
}
2194+
$this->transaction_savepoints[] = $savepoint_key;
2195+
$this->in_transaction = true;
21632196
return;
21642197
}
21652198

21662199
// RELEASE SAVEPOINT.
21672200
if ( WP_MySQL_Lexer::RELEASE_SYMBOL === $token->id ) {
21682201
$this->execute_sqlite_query( sprintf( 'RELEASE SAVEPOINT %s', $savepoint_name ) );
2202+
$savepoint_index = $this->find_transaction_savepoint_index( $savepoint_key );
2203+
if ( null !== $savepoint_index ) {
2204+
$this->transaction_savepoints = array_slice( $this->transaction_savepoints, 0, $savepoint_index );
2205+
}
2206+
if ( $this->transaction_started_by_savepoint && empty( $this->transaction_savepoints ) ) {
2207+
$this->in_transaction = false;
2208+
$this->transaction_started_by_savepoint = false;
2209+
}
21692210
return;
21702211
}
21712212

@@ -2236,6 +2277,21 @@ private function execute_transaction_or_locking_statement( WP_Parser_Node $node
22362277
);
22372278
}
22382279

2280+
/**
2281+
* Find the innermost active user savepoint with the given name.
2282+
*
2283+
* @param string $savepoint_name Normalized savepoint name.
2284+
* @return int|null Savepoint index, or null when not tracked.
2285+
*/
2286+
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 ] ) {
2289+
return $index;
2290+
}
2291+
}
2292+
return null;
2293+
}
2294+
22392295
/**
22402296
* Translate and execute a MySQL SELECT statement in SQLite.
22412297
*

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,90 @@ 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+
956+
public function test_write_inside_savepoint_can_be_rolled_back(): 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( 'SAVEPOINT outer_transaction' );
961+
$this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
962+
$this->driver->query( 'ROLLBACK TO SAVEPOINT outer_transaction' );
963+
$this->driver->query( 'RELEASE SAVEPOINT outer_transaction' );
964+
965+
$this->assertFalse( $this->driver->inTransaction() );
966+
$this->assertSame( '1', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
967+
}
968+
969+
public function test_writes_inside_nested_savepoints_preserve_outer_changes(): void {
970+
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY, value INT)' );
971+
$this->driver->query( 'INSERT INTO t VALUES (1, 1)' );
972+
973+
$this->driver->query( 'SAVEPOINT outer_transaction' );
974+
$this->driver->query( 'UPDATE t SET value = 2 WHERE id = 1' );
975+
$this->driver->query( 'SAVEPOINT inner_transaction' );
976+
$this->driver->query( 'UPDATE t SET value = 3 WHERE id = 1' );
977+
$this->driver->query( 'ROLLBACK TO SAVEPOINT inner_transaction' );
978+
$this->driver->query( 'RELEASE SAVEPOINT inner_transaction' );
979+
980+
$this->assertTrue( $this->driver->inTransaction() );
981+
$this->assertSame( '2', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
982+
983+
$this->driver->query( 'RELEASE SAVEPOINT outer_transaction' );
984+
985+
$this->assertFalse( $this->driver->inTransaction() );
986+
$this->assertSame( '2', $this->driver->query( 'SELECT value FROM t' )->fetchColumn() );
987+
}
988+
989+
public function test_failed_write_cleans_up_savepoint_transaction_state(): void {
990+
$this->driver->query( 'CREATE TABLE t (id INT PRIMARY KEY)' );
991+
$this->driver->query( 'INSERT INTO t VALUES (1)' );
992+
$this->driver->query( 'SAVEPOINT outer_transaction' );
993+
994+
try {
995+
$this->driver->query( 'INSERT INTO t VALUES (1)' );
996+
$this->fail( 'Expected the duplicate insert to fail.' );
997+
} catch ( PDOException $e ) {
998+
$this->assertStringContainsString( 'UNIQUE constraint failed', $e->getMessage() );
999+
}
1000+
1001+
$this->assertFalse( $this->driver->inTransaction() );
1002+
$this->assertSame( '1', $this->driver->query( 'SELECT COUNT(*) FROM t' )->fetchColumn() );
1003+
1004+
$this->driver->query( 'INSERT INTO t VALUES (2)' );
1005+
$queries = array_column( $this->driver->get_last_sqlite_queries(), 'sql' );
1006+
$this->assertSame( 'BEGIN IMMEDIATE', $queries[0] );
1007+
$this->assertSame( 'COMMIT', end( $queries ) );
1008+
}
1009+
9261010
public function test_fetch_default(): void {
9271011
// Default fetch mode is PDO::FETCH_BOTH.
9281012
$result = $this->driver->query( "SELECT 1, 'abc', 2" );
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
class WP_SQLite_Database_Integration_Savepoint_Test extends WP_UnitTestCase {
4+
5+
public function test_wpdb_update_inside_savepoint_does_not_open_nested_transaction() {
6+
global $wpdb;
7+
8+
$table_name = $wpdb->prefix . 'sqlite_savepoint_write_test';
9+
$wpdb->query( $wpdb->prepare( 'CREATE TABLE %i (id INT PRIMARY KEY, value INT)', $table_name ) );
10+
$wpdb->insert(
11+
$table_name,
12+
array(
13+
'id' => 1,
14+
'value' => 1,
15+
)
16+
);
17+
18+
try {
19+
$this->assertNotFalse( $wpdb->query( 'SAVEPOINT wpdb_update' ) );
20+
21+
$result = $wpdb->update( $table_name, array( 'value' => 2 ), array( 'id' => 1 ) );
22+
$queries = array_column( $wpdb->get_driver()->get_last_sqlite_queries(), 'sql' );
23+
24+
$this->assertSame( 1, $result );
25+
$this->assertSame( 1, $wpdb->rows_affected );
26+
$this->assertSame( '', $wpdb->last_error );
27+
$this->assertNotContains( 'BEGIN IMMEDIATE', $queries );
28+
$this->assertNotFalse( $wpdb->query( 'RELEASE SAVEPOINT wpdb_update' ) );
29+
$this->assertSame( '2', $wpdb->get_var( $wpdb->prepare( 'SELECT value FROM %i WHERE id = 1', $table_name ) ) );
30+
} finally {
31+
if ( $wpdb->get_driver()->inTransaction() ) {
32+
$wpdb->query( 'ROLLBACK' );
33+
}
34+
$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table_name ) );
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)