Skip to content

Commit 710bf49

Browse files
committed
Skip storage locking for in-memory databases
An in-memory database has no files, and no other process can open it, so there is nothing to lock. Don't set a storage root for it, which removes its dependency on FQDBDIR, and make lock() do nothing. Also don't derive FQDBDIR from an in-memory DB_PATH. Its directory is ".", which would make FQDBDIR a relative path. FQDBDIR keeps its default instead.
1 parent 1e9e00a commit 710bf49

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

‎packages/plugin-sqlite-database-integration/constants.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* @deprecated 3.0.0 Define DB_PATH instead of overriding FQDBDIR.
5454
*/
5555
if ( ! defined( 'FQDBDIR' ) ) {
56-
if ( defined( 'DB_PATH' ) && is_string( DB_PATH ) ) {
56+
if ( defined( 'DB_PATH' ) && is_string( DB_PATH ) && ':memory:' !== DB_PATH ) {
5757
define( 'FQDBDIR', rtrim( dirname( DB_PATH ), '/\\' ) . '/' );
5858
} elseif ( defined( 'DB_DIR' ) ) {
5959
define( 'FQDBDIR', rtrim( DB_DIR, '/\\' ) . '/' );

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-storage.php‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ class WP_SQLite_Storage {
5050

5151
/**
5252
* Directory with the storage lock files and, for a secret path, the database.
53+
* Null for an in-memory database.
5354
*
54-
* @var string
55+
* @var string|null
5556
*/
5657
private $database_root;
5758

@@ -65,21 +66,21 @@ class WP_SQLite_Storage {
6566
/**
6667
* Absolute path of the database path file.
6768
*
68-
* @var string
69+
* @var string|null
6970
*/
7071
private $database_path_file;
7172

7273
/**
7374
* Absolute path of the SQLite locking database.
7475
*
75-
* @var string
76+
* @var string|null
7677
*/
7778
private $lock_path;
7879

7980
/**
8081
* Absolute path of the storage maintenance marker.
8182
*
82-
* @var string
83+
* @var string|null
8384
*/
8485
private $maintenance_path;
8586

@@ -138,7 +139,9 @@ public static function with_explicit_path( string $path ): self {
138139

139140
$storage = new self();
140141
$storage->database_path = $path;
141-
$storage->set_database_root( ':memory:' === $path ? FQDBDIR : dirname( $path ) );
142+
if ( ':memory:' !== $path ) {
143+
$storage->set_database_root( dirname( $path ) );
144+
}
142145
return $storage;
143146
}
144147

@@ -230,6 +233,11 @@ public function initialize(): string {
230233
* @throws RuntimeException When the lock cannot be acquired.
231234
*/
232235
public function lock(): void {
236+
// An in-memory database has no files to lock.
237+
if ( ':memory:' === $this->database_path ) {
238+
return;
239+
}
240+
233241
$was_locked = null !== $this->storage_lock_connection;
234242

235243
// Serialize maintenance through the dedicated locking database.

‎tests/phpunit/WP_SQLite_Storage_Test.php‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ public function test_dropin_uses_in_memory_db_path() {
207207
echo json_encode(array(
208208
"path" => DB_PATH,
209209
"connected_path" => $wpdb->get_driver()->get_sqlite_pdo()->query("PRAGMA database_list")->fetch(PDO::FETCH_ASSOC)["file"],
210-
"legacy_path" => FQDB
210+
"legacy_path" => FQDB,
211+
"legacy_directory" => FQDBDIR
211212
));';
212213

213214
$result = $this->close_process( ...$this->open_dropin_process( $settings, $script ) );
@@ -219,6 +220,7 @@ public function test_dropin_uses_in_memory_db_path() {
219220
$this->assertSame( ':memory:', $data['path'] );
220221
$this->assertSame( '', $data['connected_path'] );
221222
$this->assertSame( ':memory:', $data['legacy_path'] );
223+
$this->assertSame( $directory . '/content/database/', $data['legacy_directory'] );
222224
$this->assertSame( array( '.', '..' ), scandir( $directory ) );
223225
}
224226

@@ -300,7 +302,8 @@ public function mixed_database_file_settings() {
300302
*/
301303
public function test_validates_absolute_database_paths_without_wordpress_helpers( $database_path ) {
302304
$script = sprintf(
303-
'define("DB_PATH", %s); require %s; require %s; WP_SQLite_Storage::with_explicit_path(DB_PATH); echo FQDB;',
305+
'define("ABSPATH", %s); define("DB_PATH", %s); require %s; require %s; WP_SQLite_Storage::with_explicit_path(DB_PATH); echo FQDB;',
306+
var_export( ABSPATH, true ),
304307
var_export( $database_path, true ),
305308
var_export( WP_CONTENT_DIR . '/plugins/sqlite-database-integration/constants.php', true ),
306309
var_export( WP_CONTENT_DIR . '/plugins/sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-storage.php', true )
@@ -640,11 +643,18 @@ public function test_initializes_an_explicit_database_file() {
640643
$this->assert_protected_directory( dirname( $database_path ) );
641644
}
642645

643-
public function test_initializes_an_in_memory_database_without_creating_files() {
646+
public function test_uses_an_in_memory_database_without_creating_files() {
647+
$script = sprintf(
648+
'require %s; $storage = WP_SQLite_Storage::with_explicit_path(":memory:"); echo $storage->initialize(); $storage->lock(); $storage->unlock();',
649+
var_export( WP_CONTENT_DIR . '/plugins/sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-storage.php', true )
650+
);
651+
644652
$this->assert_creates_no_files(
645-
function () {
646-
$storage = WP_SQLite_Storage::with_explicit_path( ':memory:' );
647-
$this->assertSame( ':memory:', $storage->initialize() );
653+
function () use ( $script ) {
654+
$result = $this->close_process( ...$this->open_process( $script ) );
655+
$this->assertSame( 0, $result['exit_code'], $result['error'] );
656+
$this->assertSame( '', $result['error'] );
657+
$this->assertSame( ':memory:', $result['output'] );
648658
}
649659
);
650660
}

0 commit comments

Comments
 (0)