Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/Commands/MigrateDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -22,23 +21,19 @@ 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);
}

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)
);
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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'
Expand Down
24 changes: 11 additions & 13 deletions src/Commands/MigrateLoadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
}

Expand Down Expand Up @@ -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);
}
Expand Down
11 changes: 8 additions & 3 deletions src/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}
}
8 changes: 6 additions & 2 deletions src/Handlers/MigrateStartingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions tests/MigrateDumpTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use AlwaysOpen\MigrationSnapshot\Commands\MigrateDumpCommand;
use AlwaysOpen\MigrationSnapshot\Tests\TestCase;

class MigrateDumpTest extends TestCase
Expand Down Expand Up @@ -30,9 +29,7 @@ public function test_dump_callsAfterDumpClosure()
$result = \Artisan::call('migrate:dump');
$this->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);
}
}
11 changes: 5 additions & 6 deletions tests/MigrateHookTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace AlwaysOpen\MigrationSnapshot\Tests\Mysql;

use AlwaysOpen\MigrationSnapshot\Tests\TestCase;
Expand All @@ -24,8 +23,8 @@ public function test_handle()
$this->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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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());
}
Expand Down
9 changes: 5 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use AlwaysOpen\MigrationSnapshot\Commands\MigrateDumpCommand;
use AlwaysOpen\MigrationSnapshot\ServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
{
Expand All @@ -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
Expand All @@ -44,7 +45,7 @@ protected function getEnvironmentSetUp($app)

protected function getPackageProviders($app)
{
return ['\AlwaysOpen\MigrationSnapshot\ServiceProvider'];
return [ServiceProvider::class];
}

protected function createTestTablesWithoutMigrate() : void
Expand Down