@@ -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