diff --git a/src/Commands/MigrateDumpCommand.php b/src/Commands/MigrateDumpCommand.php index 3a55e3f..36b9d9e 100644 --- a/src/Commands/MigrateDumpCommand.php +++ b/src/Commands/MigrateDumpCommand.php @@ -8,10 +8,9 @@ final class MigrateDumpCommand extends Command { - public const SCHEMA_SQL_PATH_SUFFIX = '/migrations/sql/schema.sql'; - public const DATA_SQL_PATH_SUFFIX = '/migrations/sql/data.sql'; - public const SUPPORTED_DB_DRIVERS = ['mysql', 'pgsql', 'sqlite']; + protected const SCHEMA_SQL_PATH_SUFFIX = 'migrations/sql/schema.'; + protected const DATA_SQL_PATH_SUFFIX = 'migrations/sql/data.'; protected $signature = 'migrate:dump {--database= : The database connection to use} @@ -22,15 +21,11 @@ final class MigrateDumpCommand extends Command public function handle() { - $exit_code = null; - $database = $this->option('database') ?: DB::getDefaultConnection(); DB::setDefaultConnection($database); $db_config = DB::getConfig(); - // CONSIDER: Ending with ".mysql" or "-mysql.sql" unless in - // compatibility mode. - $schema_sql_path = database_path() . self::SCHEMA_SQL_PATH_SUFFIX; + $schema_sql_path = self::getSchemaSqlPath($db_config['driver']); $schema_sql_directory = dirname($schema_sql_path); if (! file_exists($schema_sql_directory)) { mkdir($schema_sql_directory, 0755); @@ -38,7 +33,7 @@ public function handle() if (! in_array($db_config['driver'], self::SUPPORTED_DB_DRIVERS, true)) { throw new \InvalidArgumentException( - 'Unsupported DB driver ' . var_export($db_config['driver'], 1) + 'Unsupported database driver ' . var_export($db_config['driver'], 1) ); } @@ -61,13 +56,13 @@ public function handle() exit($exit_code); // CONSIDER: Returning instead. } - $this->info('Dumped schema'); + $this->info('Dumped ' . $db_config['driver'] . ' schema'); $data_path = null; if ($this->option('include-data')) { $this->info('Starting Data Dump'); - $data_path = database_path() . self::DATA_SQL_PATH_SUFFIX; + $data_path = self::getDataSqlPath($db_config['driver']); if ('pgsql' === $db_config['driver']) { $data_path = preg_replace('/\.sql$/', '.pgdump', $data_path); } @@ -134,6 +129,16 @@ public static function reorderMigrationRows(array $output) : array return $output; } + public static function getSchemaSqlPath(string $driver) : string + { + return database_path(self::SCHEMA_SQL_PATH_SUFFIX . $driver . '.sql'); + } + + public static function getDataSqlPath(string $driver) : string + { + return database_path(self::DATA_SQL_PATH_SUFFIX . $driver . '.sql'); + } + /** * @param array $db_config like ['host' => , 'port' => ]. * @param string $schema_sql_path like '.../schema.sql' diff --git a/src/Commands/MigrateLoadCommand.php b/src/Commands/MigrateLoadCommand.php index bf78f60..b3433dd 100644 --- a/src/Commands/MigrateLoadCommand.php +++ b/src/Commands/MigrateLoadCommand.php @@ -20,30 +20,28 @@ final class MigrateLoadCommand extends Command public function handle() { - $exit_code = null; - if ( ! $this->option('force') && app()->environment('production') - && ! $this->confirm('Are you sure you want to load the DB schema from a file?') + && ! $this->confirm('Are you sure you want to load the database schema from a file?') ) { return; } - $schema_sql_path = database_path() . MigrateDumpCommand::SCHEMA_SQL_PATH_SUFFIX; - if (! file_exists($schema_sql_path)) { - throw new InvalidArgumentException( - 'Schema-migrations path not found, run `migrate:dump` first.' - ); - } - $database = $this->option('database') ?: DB::getDefaultConnection(); DB::setDefaultConnection($database); $db_config = DB::getConfig(); if (! in_array($db_config['driver'], MigrateDumpCommand::SUPPORTED_DB_DRIVERS, true)) { throw new InvalidArgumentException( - 'Unsupported DB driver ' . var_export($db_config['driver'], 1) + 'Unsupported database driver ' . var_export($db_config['driver'], 1) + ); + } + + $schema_sql_path = MigrateDumpCommand::getSchemaSqlPath($db_config['driver']); + if (! file_exists($schema_sql_path)) { + throw new InvalidArgumentException( + 'No schema dump found for the current database driver. Run `migrate:dump --database=' . $database . '` before running this command.' ); } @@ -71,9 +69,9 @@ public function handle() exit($exit_code); // CONSIDER: Returning instead. } - $this->info('Loaded schema'); + $this->info('Loaded ' . $db_config['driver'] . ' schema'); - $data_path = database_path() . MigrateDumpCommand::DATA_SQL_PATH_SUFFIX; + $data_path = MigrateDumpCommand::getDataSqlPath($db_config['driver']); if ('pgsql' === $db_config['driver']) { $data_path = preg_replace('/\.sql$/', '.pgdump', $data_path); } diff --git a/src/EventServiceProvider.php b/src/EventServiceProvider.php index c30fc9b..4d2d3f0 100644 --- a/src/EventServiceProvider.php +++ b/src/EventServiceProvider.php @@ -3,12 +3,17 @@ namespace AlwaysOpen\MigrationSnapshot; +use AlwaysOpen\MigrationSnapshot\Handlers\MigrateFinishedHandler; +use AlwaysOpen\MigrationSnapshot\Handlers\MigrateStartingHandler; +use Illuminate\Console\Events\CommandFinished; +use Illuminate\Console\Events\CommandStarting; + final class EventServiceProvider extends \Illuminate\Foundation\Support\Providers\EventServiceProvider { protected $listen = [ // CONSIDER: Only registering these when Laravel version doesn't have // more specific hooks. - 'Illuminate\Console\Events\CommandFinished' => ['AlwaysOpen\MigrationSnapshot\Handlers\MigrateFinishedHandler'], - 'Illuminate\Console\Events\CommandStarting' => ['AlwaysOpen\MigrationSnapshot\Handlers\MigrateStartingHandler'], + CommandFinished::class => [MigrateFinishedHandler::class], + CommandStarting::class => [MigrateStartingHandler::class], ]; -} \ No newline at end of file +} diff --git a/src/Handlers/MigrateStartingHandler.php b/src/Handlers/MigrateStartingHandler.php index 5320ff5..245c37c 100644 --- a/src/Handlers/MigrateStartingHandler.php +++ b/src/Handlers/MigrateStartingHandler.php @@ -59,8 +59,6 @@ public function handle(CommandStarting $event) // Never implicitly load fresh (from file) in production since it // would need to drop first, and that would be destructive. && in_array(app()->environment(), explode(',', config('migration-snapshot.environments')), true) - // No point in implicitly loading when it's not present. - && file_exists(database_path() . MigrateDumpCommand::SCHEMA_SQL_PATH_SUFFIX) ) { // Must pass along options or it may use wrong DB or have // inconsistent output. @@ -72,6 +70,12 @@ public function handle(CommandStarting $event) return; } + // No point in continuing to load when it's not present. + if (!file_exists(MigrateDumpCommand::getSchemaSqlPath($db_driver))) { + // CONSIDER: Logging or emitting console warning. + return; + } + // Only implicitly load when DB has *not* migrated any since load // would wipe existing data. $has_migrated_any = false; diff --git a/tests/MigrateDumpTest.php b/tests/MigrateDumpTest.php index 918df5b..285a167 100644 --- a/tests/MigrateDumpTest.php +++ b/tests/MigrateDumpTest.php @@ -1,6 +1,5 @@ assertEquals(0, $result); - $schema_sql = file_get_contents( - database_path() . MigrateDumpCommand::SCHEMA_SQL_PATH_SUFFIX - ); + $schema_sql = file_get_contents($this->schemaSqlPath); $this->assertStringNotContainsString('/*', $schema_sql); } } diff --git a/tests/MigrateHookTest.php b/tests/MigrateHookTest.php index 9e9082c..ae9fde1 100644 --- a/tests/MigrateHookTest.php +++ b/tests/MigrateHookTest.php @@ -1,6 +1,5 @@ assertEquals(0, $result); $output_string = $output->fetch(); - $this->assertStringContainsString('Loaded schema', $output_string); - $this->assertStringContainsString('Dumped schema', $output_string); + $this->assertStringContainsString('Loaded ' . $this->dbDefault . ' schema', $output_string); + $this->assertStringContainsString('Dumped ' . $this->dbDefault . ' schema', $output_string); } public function test_handle_dumpsOnFresh() @@ -43,7 +42,7 @@ public function test_handle_dumpsOnFresh() $this->assertEquals(0, $result); $output_string = $output->fetch(); - $this->assertStringContainsString('Dumped schema', $output_string); + $this->assertStringContainsString('Dumped ' . $this->dbDefault . ' schema', $output_string); } public function test_handle_dumpsOnRollback() @@ -62,7 +61,7 @@ public function test_handle_dumpsOnRollback() $this->assertEquals(0, $result); $output_string = $output->fetch(); - $this->assertStringContainsString('Dumped schema', $output_string); + $this->assertStringContainsString('Dumped ' . $this->dbDefault . ' schema', $output_string); } public function test_handle_doesNotLoadWhenDbHasMigrated() @@ -79,7 +78,7 @@ public function test_handle_doesNotLoadWhenDbHasMigrated() $this->assertEquals(0, $result); $output_string = $output->fetch(); - $this->assertStringNotContainsString('Loaded schema', $output_string); + $this->assertStringNotContainsString('Loaded ' . $this->dbDefault . ' schema', $output_string); $this->assertEquals(1, \DB::table('test_ms')->count()); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 1fbe719..d2f8d9e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,6 +5,7 @@ use AlwaysOpen\MigrationSnapshot\Commands\MigrateDumpCommand; +use AlwaysOpen\MigrationSnapshot\ServiceProvider; class TestCase extends \Orchestra\Testbench\TestCase { @@ -17,9 +18,9 @@ protected function setUp(): void { parent::setUp(); - $this->schemaSqlPath = realpath( - __DIR__ . '/../vendor/orchestra/testbench-core/laravel/database' - ) . MigrateDumpCommand::SCHEMA_SQL_PATH_SUFFIX; + $this->schemaSqlPath = MigrateDumpCommand::getSchemaSqlPath( + $this->app['config']->get('database.connections.' . $this->dbDefault . '.driver') + ); $this->schemaSqlDirectory = dirname($this->schemaSqlPath); // Not leaving to tearDown since it can be useful to see result after @@ -44,7 +45,7 @@ protected function getEnvironmentSetUp($app) protected function getPackageProviders($app) { - return ['\AlwaysOpen\MigrationSnapshot\ServiceProvider']; + return [ServiceProvider::class]; } protected function createTestTablesWithoutMigrate() : void