@@ -228,21 +228,113 @@ public function test_does_not_expose_the_database_path_when_initialization_fails
228228 }
229229 }
230230
231- public function test_preserves_the_current_database_filename () {
232- $ this ->assert_legacy_database_is_preserved ( '.ht.sqlite ' );
231+ public function test_automatically_migrates_the_current_legacy_database () {
232+ $ this ->assert_legacy_database_is_migrated ( '.ht.sqlite ' );
233233 }
234234
235- public function test_preserves_the_older_database_filename () {
236- $ this ->assert_legacy_database_is_preserved ( '.ht.sqlite.php ' );
235+ public function test_automatically_migrates_the_older_legacy_database () {
236+ $ this ->assert_legacy_database_is_migrated ( '.ht.sqlite.php ' );
237237 }
238238
239- private function assert_legacy_database_is_preserved ( $ filename ) {
239+ public function test_prefers_the_current_legacy_database ( ) {
240240 $ database_root = $ this ->create_temporary_directory_path ();
241- $ this ->create_directory ( $ database_root );
242- touch ( $ database_root . '/ ' . $ filename );
241+ $ current_path = $ database_root . '/.ht.sqlite ' ;
242+ $ older_path = $ database_root . '/.ht.sqlite.php ' ;
243+ $ this ->create_sqlite_database ( $ current_path );
244+ $ this ->create_sqlite_database ( $ older_path );
243245
244- $ this ->assertSame ( $ database_root . '/ ' . $ filename , $ this ->initialize_managed_storage ( $ database_root ) );
245- $ this ->assertFileDoesNotExist ( $ database_root . '/db-path.php ' );
246+ $ database_path = $ this ->initialize_managed_storage ( $ database_root );
247+
248+ $ this ->assertFileDoesNotExist ( $ current_path );
249+ $ this ->assertFileExists ( $ older_path );
250+ $ this ->assertSame ( 'preserved ' , $ this ->read_sqlite_value ( $ database_path ) );
251+ }
252+
253+ public function test_waits_for_existing_wal_connections_before_migrating () {
254+ $ database_root = $ this ->create_temporary_directory_path ();
255+ $ legacy_path = $ database_root . '/.ht.sqlite ' ;
256+ $ connection = $ this ->create_sqlite_database ( $ legacy_path );
257+ $ connection ->query ( 'PRAGMA journal_mode = WAL ' );
258+ $ connection = null ;
259+
260+ list ( $ process , $ pipes ) = $ this ->open_temporary_database_connection ( $ legacy_path );
261+ try {
262+ $ database_path = $ this ->initialize_managed_storage ( $ database_root );
263+ } finally {
264+ $ process_result = $ this ->close_temporary_database_connection ( $ process , $ pipes );
265+ }
266+
267+ $ this ->assertSame ( 0 , $ process_result ['exit_code ' ] );
268+ $ this ->assertSame ( '' , $ process_result ['error ' ] );
269+ $ this ->assertFileDoesNotExist ( $ legacy_path );
270+ $ this ->assertSame ( 'preserved ' , $ this ->read_sqlite_value ( $ database_path ) );
271+ }
272+
273+ public function test_does_not_migrate_a_busy_legacy_database () {
274+ $ database_root = $ this ->create_temporary_directory_path ();
275+ $ legacy_path = $ database_root . '/.ht.sqlite ' ;
276+ $ connection = $ this ->create_sqlite_database ( $ legacy_path );
277+ $ connection ->beginTransaction ();
278+ $ connection ->exec ( "UPDATE storage_test SET value = 'pending' " );
279+ $ storage = new WP_SQLite_Storage ( $ database_root );
280+ $ set_migration_timeout = Closure ::bind (
281+ function ( $ migration_timeout ) {
282+ $ this ->migration_timeout = $ migration_timeout ;
283+ },
284+ $ storage ,
285+ WP_SQLite_Storage::class
286+ );
287+ $ set_migration_timeout ( 10 );
288+
289+ try {
290+ $ storage ->initialize ();
291+ $ this ->fail ( 'A busy SQLite database was migrated. ' );
292+ } catch ( RuntimeException $ exception ) {
293+ $ this ->assertStringContainsString ( 'Failed to prepare the SQLite database for migration ' , $ exception ->getMessage () );
294+ $ this ->assertStringNotContainsString ( $ legacy_path , $ exception ->getMessage () );
295+ } finally {
296+ $ connection ->rollBack ();
297+ }
298+
299+ $ database_path = require $ database_root . '/db-path.php ' ;
300+
301+ $ this ->assertFileExists ( $ legacy_path );
302+ $ this ->assertFileDoesNotExist ( $ database_path );
303+ $ this ->assertSame ( $ database_path , $ storage ->initialize () );
304+ $ this ->assertFileDoesNotExist ( $ legacy_path );
305+ $ this ->assertSame ( 'preserved ' , $ this ->read_sqlite_value ( $ database_path ) );
306+ }
307+
308+ public function test_does_not_expose_database_paths_when_migration_fails () {
309+ $ database_root = $ this ->create_temporary_directory_path ();
310+ $ legacy_path = $ database_root . '/.ht.sqlite ' ;
311+ $ database_path = $ database_root . '/.ht.secret/.ht.sqlite ' ;
312+ $ this ->create_sqlite_database ( $ legacy_path );
313+ $ this ->create_directory ( $ database_path );
314+ file_put_contents ( $ database_root . '/db-path.php ' , "<?php \nreturn " . var_export ( $ database_path , true ) . "; \n" );
315+
316+ try {
317+ $ this ->initialize_managed_storage ( $ database_root );
318+ $ this ->fail ( 'The database was migrated over a directory. ' );
319+ } catch ( RuntimeException $ exception ) {
320+ $ this ->assertSame ( 'Failed to move the SQLite database file. ' , $ exception ->getMessage () );
321+ $ this ->assertStringNotContainsString ( $ database_path , $ exception ->getMessage () );
322+ }
323+
324+ $ this ->assertFileExists ( $ legacy_path );
325+ }
326+
327+ private function assert_legacy_database_is_migrated ( $ filename ) {
328+ $ database_root = $ this ->create_temporary_directory_path ();
329+ $ legacy_path = $ database_root . '/ ' . $ filename ;
330+ $ this ->create_wal_sqlite_database_copy ( $ legacy_path );
331+
332+ $ database_path = $ this ->initialize_managed_storage ( $ database_root );
333+ $ stored_database_path = require $ database_root . '/db-path.php ' ;
334+
335+ $ this ->assertSame ( $ database_path , $ stored_database_path );
336+ $ this ->assertFileDoesNotExist ( $ legacy_path );
337+ $ this ->assertSame ( 'preserved ' , $ this ->read_sqlite_value ( $ database_path ) );
246338 }
247339
248340 private function initialize_managed_storage ( $ database_root ) {
@@ -270,6 +362,58 @@ private function create_sqlite_database( $database_path ) {
270362 return $ connection ;
271363 }
272364
365+ private function create_wal_sqlite_database_copy ( $ database_path ) {
366+ $ source_directory = $ this ->create_temporary_directory_path ();
367+ $ source_path = $ source_directory . '/source.sqlite ' ;
368+ $ this ->create_directory ( $ source_directory );
369+ $ this ->create_directory ( dirname ( $ database_path ) );
370+
371+ $ connection = new PDO ( 'sqlite: ' . $ source_path );
372+ $ connection ->setAttribute ( PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
373+ $ this ->assertSame ( 'wal ' , $ connection ->query ( 'PRAGMA journal_mode = WAL ' )->fetchColumn () );
374+ $ connection ->exec ( 'PRAGMA wal_autocheckpoint = 0 ' );
375+ $ connection ->exec ( 'CREATE TABLE storage_test (value TEXT NOT NULL) ' );
376+ $ connection ->exec ( "INSERT INTO storage_test VALUES ('preserved') " );
377+
378+ $ this ->assertTrue ( copy ( $ source_path , $ database_path ) );
379+ $ this ->assertTrue ( copy ( $ source_path . '-wal ' , $ database_path . '-wal ' ) );
380+ $ connection = null ;
381+ }
382+
383+ private function open_temporary_database_connection ( $ database_path ) {
384+ $ script = sprintf (
385+ '$connection = new PDO(%s); $connection->query("SELECT value FROM storage_test")->fetchColumn(); fwrite(STDOUT, "ready\n"); fflush(STDOUT); usleep(250000); ' ,
386+ var_export ( 'sqlite: ' . $ database_path , true )
387+ );
388+ $ command = escapeshellarg ( PHP_BINARY ) . ' -r ' . escapeshellarg ( $ script );
389+ $ process = proc_open (
390+ $ command ,
391+ array (
392+ array ( 'pipe ' , 'r ' ),
393+ array ( 'pipe ' , 'w ' ),
394+ array ( 'pipe ' , 'w ' ),
395+ ),
396+ $ pipes
397+ );
398+
399+ $ this ->assertIsResource ( $ process );
400+ fclose ( $ pipes [0 ] );
401+ $ this ->assertSame ( "ready \n" , fgets ( $ pipes [1 ] ) );
402+
403+ return array ( $ process , $ pipes );
404+ }
405+
406+ private function close_temporary_database_connection ( $ process , $ pipes ) {
407+ fclose ( $ pipes [1 ] );
408+ $ error = stream_get_contents ( $ pipes [2 ] );
409+ fclose ( $ pipes [2 ] );
410+
411+ return array (
412+ 'exit_code ' => proc_close ( $ process ),
413+ 'error ' => $ error ,
414+ );
415+ }
416+
273417 private function read_sqlite_value ( $ database_path ) {
274418 $ connection = new PDO ( 'sqlite: ' . $ database_path );
275419
0 commit comments