@@ -213,21 +213,104 @@ public function test_does_not_expose_the_database_path_when_initialization_fails
213213 }
214214 }
215215
216- public function test_preserves_the_current_database_filename () {
217- $ this ->assert_legacy_database_is_preserved ( '.ht.sqlite ' );
216+ public function test_automatically_migrates_the_current_legacy_database () {
217+ $ this ->assert_legacy_database_is_migrated ( '.ht.sqlite ' );
218218 }
219219
220- public function test_preserves_the_older_database_filename () {
221- $ this ->assert_legacy_database_is_preserved ( '.ht.sqlite.php ' );
220+ public function test_automatically_migrates_the_older_legacy_database () {
221+ $ this ->assert_legacy_database_is_migrated ( '.ht.sqlite.php ' );
222222 }
223223
224- private function assert_legacy_database_is_preserved ( $ filename ) {
224+ public function test_prefers_the_current_legacy_database ( ) {
225225 $ database_root = $ this ->create_temporary_directory_path ();
226- $ this ->create_directory ( $ database_root );
227- touch ( $ database_root . '/ ' . $ filename );
226+ $ current_path = $ database_root . '/.ht.sqlite ' ;
227+ $ older_path = $ database_root . '/.ht.sqlite.php ' ;
228+ $ this ->create_sqlite_database ( $ current_path );
229+ $ this ->create_sqlite_database ( $ older_path );
228230
229- $ this ->assertSame ( $ database_root . '/ ' . $ filename , $ this ->initialize_managed_storage ( $ database_root ) );
230- $ this ->assertFileDoesNotExist ( $ database_root . '/db-path.php ' );
231+ $ database_path = $ this ->initialize_managed_storage ( $ database_root );
232+
233+ $ this ->assertFileDoesNotExist ( $ current_path );
234+ $ this ->assertFileExists ( $ older_path );
235+ $ this ->assertSame ( 'preserved ' , $ this ->read_sqlite_value ( $ database_path ) );
236+ }
237+
238+ public function test_waits_for_existing_wal_connections_before_migrating () {
239+ $ database_root = $ this ->create_temporary_directory_path ();
240+ $ legacy_path = $ database_root . '/.ht.sqlite ' ;
241+ $ connection = $ this ->create_sqlite_database ( $ legacy_path );
242+ $ connection ->query ( 'PRAGMA journal_mode = WAL ' );
243+ $ connection = null ;
244+
245+ list ( $ process , $ pipes ) = $ this ->open_temporary_database_connection ( $ legacy_path );
246+ try {
247+ $ database_path = $ this ->initialize_managed_storage ( $ database_root );
248+ } finally {
249+ $ process_result = $ this ->close_temporary_database_connection ( $ process , $ pipes );
250+ }
251+
252+ $ this ->assertSame ( 0 , $ process_result ['exit_code ' ] );
253+ $ this ->assertSame ( '' , $ process_result ['error ' ] );
254+ $ this ->assertFileDoesNotExist ( $ legacy_path );
255+ $ this ->assertSame ( 'preserved ' , $ this ->read_sqlite_value ( $ database_path ) );
256+ }
257+
258+ public function test_does_not_migrate_a_busy_legacy_database () {
259+ $ database_root = $ this ->create_temporary_directory_path ();
260+ $ legacy_path = $ database_root . '/.ht.sqlite ' ;
261+ $ connection = $ this ->create_sqlite_database ( $ legacy_path );
262+ $ connection ->beginTransaction ();
263+ $ connection ->exec ( "UPDATE storage_test SET value = 'pending' " );
264+
265+ try {
266+ $ this ->initialize_managed_storage ( $ database_root );
267+ $ this ->fail ( 'A busy SQLite database was migrated. ' );
268+ } catch ( RuntimeException $ exception ) {
269+ $ this ->assertStringContainsString ( 'Failed to prepare the SQLite database for migration ' , $ exception ->getMessage () );
270+ $ this ->assertStringNotContainsString ( $ legacy_path , $ exception ->getMessage () );
271+ } finally {
272+ $ connection ->rollBack ();
273+ }
274+
275+ $ database_path = require $ database_root . '/db-path.php ' ;
276+
277+ $ this ->assertFileExists ( $ legacy_path );
278+ $ this ->assertFileDoesNotExist ( $ database_path );
279+ $ this ->assertSame ( $ database_path , $ this ->initialize_managed_storage ( $ database_root ) );
280+ $ this ->assertFileDoesNotExist ( $ legacy_path );
281+ $ this ->assertSame ( 'preserved ' , $ this ->read_sqlite_value ( $ database_path ) );
282+ }
283+
284+ public function test_does_not_expose_database_paths_when_migration_fails () {
285+ $ database_root = $ this ->create_temporary_directory_path ();
286+ $ legacy_path = $ database_root . '/.ht.sqlite ' ;
287+ $ database_path = $ database_root . '/.ht.secret/.ht.sqlite ' ;
288+ $ this ->create_sqlite_database ( $ legacy_path );
289+ $ this ->create_directory ( $ database_path );
290+ file_put_contents ( $ database_root . '/db-path.php ' , "<?php \nreturn " . var_export ( $ database_path , true ) . "; \n" );
291+
292+ try {
293+ $ this ->initialize_managed_storage ( $ database_root );
294+ $ this ->fail ( 'The database was migrated over a directory. ' );
295+ } catch ( RuntimeException $ exception ) {
296+ $ this ->assertSame ( 'Failed to move the SQLite database file. ' , $ exception ->getMessage () );
297+ $ this ->assertStringNotContainsString ( $ database_path , $ exception ->getMessage () );
298+ }
299+
300+ $ this ->assertFileExists ( $ legacy_path );
301+ }
302+
303+ private function assert_legacy_database_is_migrated ( $ filename ) {
304+ $ database_root = $ this ->create_temporary_directory_path ();
305+ $ legacy_path = $ database_root . '/ ' . $ filename ;
306+ $ this ->create_wal_sqlite_database_copy ( $ legacy_path );
307+
308+ $ database_path = $ this ->initialize_managed_storage ( $ database_root );
309+ $ stored_database_path = require $ database_root . '/db-path.php ' ;
310+
311+ $ this ->assertSame ( $ database_path , $ stored_database_path );
312+ $ this ->assertFileDoesNotExist ( $ legacy_path );
313+ $ this ->assertSame ( 'preserved ' , $ this ->read_sqlite_value ( $ database_path ) );
231314 }
232315
233316 private function initialize_managed_storage ( $ database_root ) {
@@ -255,6 +338,58 @@ private function create_sqlite_database( $database_path ) {
255338 return $ connection ;
256339 }
257340
341+ private function create_wal_sqlite_database_copy ( $ database_path ) {
342+ $ source_directory = $ this ->create_temporary_directory_path ();
343+ $ source_path = $ source_directory . '/source.sqlite ' ;
344+ $ this ->create_directory ( $ source_directory );
345+ $ this ->create_directory ( dirname ( $ database_path ) );
346+
347+ $ connection = new PDO ( 'sqlite: ' . $ source_path );
348+ $ connection ->setAttribute ( PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
349+ $ this ->assertSame ( 'wal ' , $ connection ->query ( 'PRAGMA journal_mode = WAL ' )->fetchColumn () );
350+ $ connection ->exec ( 'PRAGMA wal_autocheckpoint = 0 ' );
351+ $ connection ->exec ( 'CREATE TABLE storage_test (value TEXT NOT NULL) ' );
352+ $ connection ->exec ( "INSERT INTO storage_test VALUES ('preserved') " );
353+
354+ $ this ->assertTrue ( copy ( $ source_path , $ database_path ) );
355+ $ this ->assertTrue ( copy ( $ source_path . '-wal ' , $ database_path . '-wal ' ) );
356+ $ connection = null ;
357+ }
358+
359+ private function open_temporary_database_connection ( $ database_path ) {
360+ $ script = sprintf (
361+ '$connection = new PDO(%s); $connection->query("SELECT value FROM storage_test")->fetchColumn(); fwrite(STDOUT, "ready\n"); fflush(STDOUT); usleep(250000); ' ,
362+ var_export ( 'sqlite: ' . $ database_path , true )
363+ );
364+ $ command = escapeshellarg ( PHP_BINARY ) . ' -r ' . escapeshellarg ( $ script );
365+ $ process = proc_open (
366+ $ command ,
367+ array (
368+ array ( 'pipe ' , 'r ' ),
369+ array ( 'pipe ' , 'w ' ),
370+ array ( 'pipe ' , 'w ' ),
371+ ),
372+ $ pipes
373+ );
374+
375+ $ this ->assertIsResource ( $ process );
376+ fclose ( $ pipes [0 ] );
377+ $ this ->assertSame ( "ready \n" , fgets ( $ pipes [1 ] ) );
378+
379+ return array ( $ process , $ pipes );
380+ }
381+
382+ private function close_temporary_database_connection ( $ process , $ pipes ) {
383+ fclose ( $ pipes [1 ] );
384+ $ error = stream_get_contents ( $ pipes [2 ] );
385+ fclose ( $ pipes [2 ] );
386+
387+ return array (
388+ 'exit_code ' => proc_close ( $ process ),
389+ 'error ' => $ error ,
390+ );
391+ }
392+
258393 private function read_sqlite_value ( $ database_path ) {
259394 $ connection = new PDO ( 'sqlite: ' . $ database_path );
260395
0 commit comments