Skip to content

Commit c559b3a

Browse files
committed
Add tests for SHOW and DESCRIBE using a deferred BEGIN transaction
1 parent 14dc77a commit c559b3a

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

packages/mysql-on-sqlite/tests/WP_SQLite_Driver_Concurrency_Tests.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,54 @@ function ( string $sql, array $params ) use ( &$logged_queries ): void {
5757
$this->assertStringStartsNotWith( 'BEGIN', $logged_queries[0] );
5858
}
5959

60+
/**
61+
* A SHOW statement should use a deferred BEGIN (SHARED lock), not
62+
* BEGIN IMMEDIATE (RESERVED/write lock).
63+
*/
64+
public function testShowQueryOpensReadOnlyTransaction(): void {
65+
$pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class;
66+
$pdo = new $pdo_class( 'sqlite::memory:' );
67+
68+
$connection = new WP_SQLite_Connection( array( 'pdo' => $pdo ) );
69+
$driver = new WP_SQLite_Driver( $connection, 'wp' );
70+
$driver->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
71+
72+
$logged_queries = array();
73+
$driver->get_connection()->set_query_logger(
74+
function ( string $sql, array $params ) use ( &$logged_queries ): void {
75+
$logged_queries[] = $sql;
76+
}
77+
);
78+
79+
$driver->query( 'SHOW TABLES' );
80+
81+
$this->assertSame( 'BEGIN', $logged_queries[0] );
82+
}
83+
84+
/**
85+
* A DESCRIBE statement should use a deferred BEGIN (SHARED lock), not
86+
* BEGIN IMMEDIATE (RESERVED/write lock).
87+
*/
88+
public function testDescribeQueryOpensReadOnlyTransaction(): void {
89+
$pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class;
90+
$pdo = new $pdo_class( 'sqlite::memory:' );
91+
92+
$connection = new WP_SQLite_Connection( array( 'pdo' => $pdo ) );
93+
$driver = new WP_SQLite_Driver( $connection, 'wp' );
94+
$driver->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
95+
96+
$logged_queries = array();
97+
$driver->get_connection()->set_query_logger(
98+
function ( string $sql, array $params ) use ( &$logged_queries ): void {
99+
$logged_queries[] = $sql;
100+
}
101+
);
102+
103+
$driver->query( 'DESCRIBE t' );
104+
105+
$this->assertSame( 'BEGIN', $logged_queries[0] );
106+
}
107+
60108
/**
61109
* A SELECT on one connection should succeed even when another connection
62110
* holds an open write transaction (RESERVED lock).

0 commit comments

Comments
 (0)