Skip to content

Commit c6ffc07

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.
1 parent 2d32a5b commit c6ffc07

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

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

Lines changed: 12 additions & 6 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 managed storage, 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

@@ -115,9 +116,9 @@ public function __construct( ?string $database_path = null ) {
115116
throw new RuntimeException( 'The SQLite database path must be an absolute filesystem path or ":memory:".' );
116117
}
117118
$this->database_path = $database_path;
118-
if ( null === $database_path || ':memory:' === $database_path ) {
119+
if ( null === $database_path ) {
119120
$this->set_database_root( FQDBDIR );
120-
} else {
121+
} elseif ( ':memory:' !== $database_path ) {
121122
$this->set_database_root( dirname( $database_path ) );
122123
}
123124
}
@@ -210,6 +211,11 @@ public function initialize(): string {
210211
* @throws RuntimeException When the lock cannot be acquired.
211212
*/
212213
public function lock(): void {
214+
// An in-memory database has no files to lock.
215+
if ( ':memory:' === $this->database_path ) {
216+
return;
217+
}
218+
213219
$was_locked = null !== $this->storage_lock_connection;
214220

215221
// Serialize maintenance through the dedicated locking database.

‎tests/phpunit/WP_SQLite_Storage_Test.php‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,18 @@ public function test_initializes_an_explicit_database_file() {
634634
$this->assert_protected_directory( dirname( $database_path ) );
635635
}
636636

637-
public function test_initializes_an_in_memory_database_without_creating_files() {
637+
public function test_uses_an_in_memory_database_without_creating_files() {
638+
$script = sprintf(
639+
'require %s; $storage = new WP_SQLite_Storage(":memory:"); echo $storage->initialize(); $storage->lock(); $storage->unlock();',
640+
var_export( WP_CONTENT_DIR . '/plugins/sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-storage.php', true )
641+
);
642+
638643
$this->assert_creates_no_files(
639-
function () {
640-
$storage = new WP_SQLite_Storage( ':memory:' );
641-
$this->assertSame( ':memory:', $storage->initialize() );
644+
function () use ( $script ) {
645+
$result = $this->close_process( ...$this->open_process( $script ) );
646+
$this->assertSame( 0, $result['exit_code'], $result['error'] );
647+
$this->assertSame( '', $result['error'] );
648+
$this->assertSame( ':memory:', $result['output'] );
642649
}
643650
);
644651
}

0 commit comments

Comments
 (0)