Skip to content

Commit 52bc7f8

Browse files
committed
Migrate legacy SQLite database storage
1 parent 9e7c500 commit 52bc7f8

3 files changed

Lines changed: 151 additions & 43 deletions

File tree

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -432,25 +432,6 @@ public function db_connect( $allow_bail = true ) {
432432
$this->init_charset();
433433
}
434434

435-
// Migrate the database file from a legacy path, if it exists.
436-
if ( ! defined( 'DB_FILE' ) && ! file_exists( FQDB ) ) {
437-
$old_db_path = FQDBDIR . '.ht.sqlite.php';
438-
439-
if ( file_exists( $old_db_path ) ) {
440-
if ( ! rename( $old_db_path, FQDB ) ) {
441-
wp_die( 'Failed to rename database file.', 'Error!' );
442-
}
443-
444-
foreach ( array( '-wal', '-shm', '-journal' ) as $suffix ) {
445-
if ( file_exists( $old_db_path . $suffix ) ) {
446-
if ( ! rename( $old_db_path . $suffix, FQDB . $suffix ) ) {
447-
wp_die( 'Failed to rename database file.', 'Error!' );
448-
}
449-
}
450-
}
451-
}
452-
}
453-
454435
if ( null === $this->dbname || '' === $this->dbname ) {
455436
$this->bail(
456437
'The database name was not set. The SQLite driver requires a database name to be set to emulate MySQL information schema tables.',

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

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* The storage handles the path of the SQLite database:
77
* - An explicit database path is used as is.
88
* - A managed database is stored under a randomized protected path.
9-
* - A database in a legacy path stays in use.
9+
* - A database in a legacy path is migrated to the managed storage.
1010
*
1111
* The storage implements a locking mechanism for initialization and maintenance.
1212
* Locking uses a dedicated empty SQLite database and a maintenance file:
@@ -126,7 +126,7 @@ public function __construct( ?string $database_root = null, ?string $database_pa
126126
* Initialize the SQLite database storage.
127127
*
128128
* Uses an explicit file path or ":memory:" as configured. Otherwise, initializes
129-
* managed storage with a randomized path. A legacy database stays at its fixed path.
129+
* managed storage with a randomized path and migrates legacy databases as needed.
130130
*
131131
* @return string Absolute path to the SQLite database file, or ":memory:".
132132
* @throws RuntimeException When the storage cannot be initialized.
@@ -159,15 +159,6 @@ public function initialize(): string {
159159
return $database_path;
160160
}
161161

162-
// Keep using a legacy database at its fixed path.
163-
$legacy_path = $this->database_root . self::DATABASE_FILENAME;
164-
if ( ! @is_file( $legacy_path ) ) {
165-
$legacy_path = $this->database_root . self::DATABASE_FILENAME . '.php';
166-
}
167-
if ( @is_file( $legacy_path ) ) {
168-
return $legacy_path;
169-
}
170-
171162
// Initialize or repair the managed storage under the storage lock.
172163
// Preserve a lock already held by this instance for a larger operation.
173164
$this->lock();
@@ -182,8 +173,17 @@ public function initialize(): string {
182173
$database_path = $this->publish_database_path();
183174
}
184175

185-
$this->ensure_database( $database_path );
186-
$this->lock();
176+
// Migrate legacy database paths.
177+
$legacy_path = $this->database_root . self::DATABASE_FILENAME;
178+
if ( ! @is_file( $legacy_path ) ) {
179+
$legacy_path = $this->database_root . self::DATABASE_FILENAME . '.php';
180+
}
181+
if ( @is_file( $legacy_path ) ) {
182+
$this->move_legacy_database( $legacy_path, $database_path );
183+
} else {
184+
$this->ensure_database( $database_path );
185+
$this->lock();
186+
}
187187
return $database_path;
188188
} finally {
189189
if ( ! $was_locked ) {
@@ -369,6 +369,35 @@ function_exists( 'opcache_invalidate' )
369369
return $this->database_root . $directory_name . '/' . self::DATABASE_FILENAME;
370370
}
371371

372+
/**
373+
* Move a legacy database into a managed path.
374+
*
375+
* @param string $legacy_path Absolute path of the legacy database file.
376+
* @param string $database_path Absolute destination path.
377+
*/
378+
private function move_legacy_database( string $legacy_path, string $database_path ): void {
379+
$this->ensure_protected_directory( dirname( $database_path ) );
380+
381+
/*
382+
* Close the connection just before moving the database. SQLite considers
383+
* renaming an open database undefined, and Windows generally prevents it.
384+
* We cannot fully prevent race conditions, but this makes them unlikely.
385+
*
386+
* See: https://www.sqlite.org/howtocorrupt.html#unlink
387+
*/
388+
$this->database_lock_connection = null;
389+
if ( ! @rename( $legacy_path, $database_path ) ) {
390+
throw new RuntimeException( 'Failed to move the SQLite database file.' );
391+
}
392+
@chmod( $database_path, 0600 );
393+
394+
try {
395+
$this->database_lock_connection = $this->lock_database( $database_path );
396+
} catch ( Throwable $exception ) {
397+
throw new RuntimeException( 'Failed to lock the migrated SQLite database.', 0, $exception );
398+
}
399+
}
400+
372401
/**
373402
* Ensure that a database and its protected directory exist.
374403
*

‎tests/phpunit/WP_SQLite_Storage_Test.php‎

Lines changed: 109 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,12 @@ public function test_does_not_expose_the_database_path_when_initialization_fails
284284
}
285285
}
286286

287-
public function test_keeps_using_the_current_legacy_database() {
288-
$this->assert_legacy_database_is_kept( '.ht.sqlite' );
287+
public function test_automatically_migrates_the_current_legacy_database() {
288+
$this->assert_legacy_database_is_migrated( '.ht.sqlite' );
289289
}
290290

291-
public function test_keeps_using_the_older_legacy_database() {
292-
$this->assert_legacy_database_is_kept( '.ht.sqlite.php' );
291+
public function test_automatically_migrates_the_older_legacy_database() {
292+
$this->assert_legacy_database_is_migrated( '.ht.sqlite.php' );
293293
}
294294

295295
public function test_prefers_the_current_legacy_database() {
@@ -299,9 +299,100 @@ public function test_prefers_the_current_legacy_database() {
299299
$this->create_sqlite_database( $current_path );
300300
$this->create_sqlite_database( $older_path );
301301

302-
$this->assertSame( $current_path, $this->initialize_managed_storage( $database_root ) );
303-
$this->assertFileExists( $current_path );
302+
$database_path = $this->initialize_managed_storage( $database_root );
303+
304+
$this->assertFileDoesNotExist( $current_path );
304305
$this->assertFileExists( $older_path );
306+
$this->assertSame( 'preserved', $this->read_sqlite_value( $database_path ) );
307+
}
308+
309+
public function test_waits_for_existing_wal_connections_before_migrating() {
310+
$database_root = $this->create_temporary_directory();
311+
$legacy_path = $database_root . '/.ht.sqlite';
312+
$this->create_wal_sqlite_database_copy( $legacy_path );
313+
314+
list( $process, $pipes ) = $this->open_database_connection_process( $legacy_path );
315+
try {
316+
$database_path = $this->initialize_managed_storage( $database_root );
317+
} finally {
318+
$result = $this->close_process( $process, $pipes );
319+
}
320+
321+
$this->assertSame( 0, $result['exit_code'] );
322+
$this->assertSame( '', $result['error'] );
323+
$this->assertFileDoesNotExist( $legacy_path );
324+
$this->assertFileDoesNotExist( $legacy_path . '-wal' );
325+
$this->assertSame( 'preserved', $this->read_sqlite_value( $database_path ) );
326+
}
327+
328+
public function test_keeps_the_legacy_database_when_migration_fails() {
329+
$database_root = $this->create_temporary_directory();
330+
$legacy_path = $database_root . '/.ht.sqlite';
331+
$database_path = $database_root . '/.ht.secret/.ht.sqlite';
332+
$this->create_sqlite_database( $legacy_path );
333+
$this->assertTrue( mkdir( $database_path, 0700, true ) );
334+
file_put_contents( $database_root . '/db-path.php', "<?php\nreturn " . var_export( $database_path, true ) . ";\n" );
335+
336+
try {
337+
$this->initialize_managed_storage( $database_root );
338+
$this->fail( 'The database was migrated over a directory.' );
339+
} catch ( RuntimeException $exception ) {
340+
$this->assertSame( 'Failed to move the SQLite database file.', $exception->getMessage() );
341+
$this->assertStringNotContainsString( $database_path, $exception->getMessage() );
342+
}
343+
344+
$this->assertFileExists( $legacy_path );
345+
$this->assertSame( 'preserved', $this->read_sqlite_value( $legacy_path ) );
346+
$this->assertFileExists( $database_root . '/.ht.sqlite.lock' );
347+
348+
// The migration succeeds once the obstacle is removed.
349+
$this->assertTrue( rmdir( $database_path ) );
350+
$this->assertSame( $database_path, $this->initialize_managed_storage( $database_root ) );
351+
$this->assertFileDoesNotExist( $legacy_path );
352+
$this->assertSame( 'preserved', $this->read_sqlite_value( $database_path ) );
353+
}
354+
355+
public function test_does_not_migrate_an_invalid_legacy_database() {
356+
$database_root = $this->create_temporary_directory();
357+
$legacy_path = $database_root . '/.ht.sqlite';
358+
file_put_contents( $legacy_path, str_repeat( 'x', 4096 ) );
359+
360+
try {
361+
$this->initialize_managed_storage( $database_root );
362+
$this->fail( 'An invalid legacy database was migrated.' );
363+
} catch ( RuntimeException $exception ) {
364+
$this->assertSame( 'Failed to lock the SQLite database.', $exception->getMessage() );
365+
}
366+
367+
$this->assertSame( str_repeat( 'x', 4096 ), file_get_contents( $legacy_path ) );
368+
$this->assertFileDoesNotExist( $database_root . '/db-path.php' );
369+
$this->assertFileExists( $database_root . '/.ht.sqlite.lock' );
370+
}
371+
372+
public function test_reports_when_the_migrated_database_cannot_be_locked() {
373+
$database_root = $this->create_temporary_directory();
374+
$legacy_path = $database_root . '/.ht.sqlite';
375+
$database_path = $database_root . '/managed/.ht.sqlite';
376+
file_put_contents( $legacy_path, str_repeat( 'x', 4096 ) );
377+
$storage = new WP_SQLite_Storage( $database_root );
378+
$move = Closure::bind(
379+
function () use ( $legacy_path, $database_path ) {
380+
$this->move_legacy_database( $legacy_path, $database_path );
381+
},
382+
$storage,
383+
WP_SQLite_Storage::class
384+
);
385+
386+
try {
387+
$move();
388+
$this->fail( 'An invalid migrated database was locked.' );
389+
} catch ( RuntimeException $exception ) {
390+
$this->assertSame( 'Failed to lock the migrated SQLite database.', $exception->getMessage() );
391+
$this->assertStringNotContainsString( $database_path, $exception->getMessage() );
392+
}
393+
394+
$this->assertFileDoesNotExist( $legacy_path );
395+
$this->assertSame( str_repeat( 'x', 4096 ), file_get_contents( $database_path ) );
305396
}
306397

307398
public function test_locks_and_unlocks_the_storage() {
@@ -574,14 +665,21 @@ public function test_does_not_lock_a_busy_database() {
574665
$storage->unlock();
575666
}
576667

577-
private function assert_legacy_database_is_kept( $filename ) {
668+
private function assert_legacy_database_is_migrated( $filename ) {
578669
$database_root = $this->create_temporary_directory();
579670
$legacy_path = $database_root . '/' . $filename;
580-
$this->create_sqlite_database( $legacy_path );
671+
$this->create_wal_sqlite_database_copy( $legacy_path );
581672

582-
$this->assertSame( $legacy_path, $this->initialize_managed_storage( $database_root ) );
583-
$this->assertSame( 'preserved', $this->read_sqlite_value( $legacy_path ) );
584-
$this->assertFileDoesNotExist( $database_root . '/db-path.php' );
673+
$database_path = $this->initialize_managed_storage( $database_root );
674+
$stored_database_path = require $database_root . '/db-path.php';
675+
676+
$this->assertSame( $database_path, $stored_database_path );
677+
$this->assertSame( 0600, fileperms( $database_path ) & 0777 );
678+
$this->assertFileDoesNotExist( $legacy_path );
679+
$this->assertFileDoesNotExist( $legacy_path . '-wal' );
680+
$this->assertFileExists( $database_root . '/.ht.sqlite.lock' );
681+
$this->assertSame( 'preserved', $this->read_sqlite_value( $database_path ) );
682+
$this->assert_protected_directory( dirname( $database_path ) );
585683
}
586684

587685
private function assert_creates_no_files( $callback ) {

0 commit comments

Comments
 (0)