Skip to content

Commit 33968f7

Browse files
authored
Merge pull request #22 from bstanley-pec/feat/database-driver-specific-dump-file
Update both commands to read/write database driver-specific files
2 parents 1dfb334 + 6db43fe commit 33968f7

File tree

7 files changed

+52
-43
lines changed

7 files changed

+52
-43
lines changed

src/Commands/MigrateDumpCommand.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88

99
final class MigrateDumpCommand extends Command
1010
{
11-
public const SCHEMA_SQL_PATH_SUFFIX = '/migrations/sql/schema.sql';
12-
public const DATA_SQL_PATH_SUFFIX = '/migrations/sql/data.sql';
13-
1411
public const SUPPORTED_DB_DRIVERS = ['mysql', 'pgsql', 'sqlite'];
12+
protected const SCHEMA_SQL_PATH_SUFFIX = 'migrations/sql/schema.';
13+
protected const DATA_SQL_PATH_SUFFIX = 'migrations/sql/data.';
1514

1615
protected $signature = 'migrate:dump
1716
{--database= : The database connection to use}
@@ -22,23 +21,19 @@ final class MigrateDumpCommand extends Command
2221

2322
public function handle()
2423
{
25-
$exit_code = null;
26-
2724
$database = $this->option('database') ?: DB::getDefaultConnection();
2825
DB::setDefaultConnection($database);
2926
$db_config = DB::getConfig();
3027

31-
// CONSIDER: Ending with ".mysql" or "-mysql.sql" unless in
32-
// compatibility mode.
33-
$schema_sql_path = database_path() . self::SCHEMA_SQL_PATH_SUFFIX;
28+
$schema_sql_path = self::getSchemaSqlPath($db_config['driver']);
3429
$schema_sql_directory = dirname($schema_sql_path);
3530
if (! file_exists($schema_sql_directory)) {
3631
mkdir($schema_sql_directory, 0755);
3732
}
3833

3934
if (! in_array($db_config['driver'], self::SUPPORTED_DB_DRIVERS, true)) {
4035
throw new \InvalidArgumentException(
41-
'Unsupported DB driver ' . var_export($db_config['driver'], 1)
36+
'Unsupported database driver ' . var_export($db_config['driver'], 1)
4237
);
4338
}
4439

@@ -61,13 +56,13 @@ public function handle()
6156
exit($exit_code); // CONSIDER: Returning instead.
6257
}
6358

64-
$this->info('Dumped schema');
59+
$this->info('Dumped ' . $db_config['driver'] . ' schema');
6560

6661
$data_path = null;
6762
if ($this->option('include-data')) {
6863
$this->info('Starting Data Dump');
6964

70-
$data_path = database_path() . self::DATA_SQL_PATH_SUFFIX;
65+
$data_path = self::getDataSqlPath($db_config['driver']);
7166
if ('pgsql' === $db_config['driver']) {
7267
$data_path = preg_replace('/\.sql$/', '.pgdump', $data_path);
7368
}
@@ -134,6 +129,16 @@ public static function reorderMigrationRows(array $output) : array
134129
return $output;
135130
}
136131

132+
public static function getSchemaSqlPath(string $driver) : string
133+
{
134+
return database_path(self::SCHEMA_SQL_PATH_SUFFIX . $driver . '.sql');
135+
}
136+
137+
public static function getDataSqlPath(string $driver) : string
138+
{
139+
return database_path(self::DATA_SQL_PATH_SUFFIX . $driver . '.sql');
140+
}
141+
137142
/**
138143
* @param array $db_config like ['host' => , 'port' => ].
139144
* @param string $schema_sql_path like '.../schema.sql'

src/Commands/MigrateLoadCommand.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,28 @@ final class MigrateLoadCommand extends Command
2020

2121
public function handle()
2222
{
23-
$exit_code = null;
24-
2523
if (
2624
! $this->option('force')
2725
&& app()->environment('production')
28-
&& ! $this->confirm('Are you sure you want to load the DB schema from a file?')
26+
&& ! $this->confirm('Are you sure you want to load the database schema from a file?')
2927
) {
3028
return;
3129
}
3230

33-
$schema_sql_path = database_path() . MigrateDumpCommand::SCHEMA_SQL_PATH_SUFFIX;
34-
if (! file_exists($schema_sql_path)) {
35-
throw new InvalidArgumentException(
36-
'Schema-migrations path not found, run `migrate:dump` first.'
37-
);
38-
}
39-
4031
$database = $this->option('database') ?: DB::getDefaultConnection();
4132
DB::setDefaultConnection($database);
4233
$db_config = DB::getConfig();
4334

4435
if (! in_array($db_config['driver'], MigrateDumpCommand::SUPPORTED_DB_DRIVERS, true)) {
4536
throw new InvalidArgumentException(
46-
'Unsupported DB driver ' . var_export($db_config['driver'], 1)
37+
'Unsupported database driver ' . var_export($db_config['driver'], 1)
38+
);
39+
}
40+
41+
$schema_sql_path = MigrateDumpCommand::getSchemaSqlPath($db_config['driver']);
42+
if (! file_exists($schema_sql_path)) {
43+
throw new InvalidArgumentException(
44+
'No schema dump found for the current database driver. Run `migrate:dump --database=' . $database . '` before running this command.'
4745
);
4846
}
4947

@@ -71,9 +69,9 @@ public function handle()
7169
exit($exit_code); // CONSIDER: Returning instead.
7270
}
7371

74-
$this->info('Loaded schema');
72+
$this->info('Loaded ' . $db_config['driver'] . ' schema');
7573

76-
$data_path = database_path() . MigrateDumpCommand::DATA_SQL_PATH_SUFFIX;
74+
$data_path = MigrateDumpCommand::getDataSqlPath($db_config['driver']);
7775
if ('pgsql' === $db_config['driver']) {
7876
$data_path = preg_replace('/\.sql$/', '.pgdump', $data_path);
7977
}

src/EventServiceProvider.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33

44
namespace AlwaysOpen\MigrationSnapshot;
55

6+
use AlwaysOpen\MigrationSnapshot\Handlers\MigrateFinishedHandler;
7+
use AlwaysOpen\MigrationSnapshot\Handlers\MigrateStartingHandler;
8+
use Illuminate\Console\Events\CommandFinished;
9+
use Illuminate\Console\Events\CommandStarting;
10+
611
final class EventServiceProvider extends \Illuminate\Foundation\Support\Providers\EventServiceProvider
712
{
813
protected $listen = [
914
// CONSIDER: Only registering these when Laravel version doesn't have
1015
// more specific hooks.
11-
'Illuminate\Console\Events\CommandFinished' => ['AlwaysOpen\MigrationSnapshot\Handlers\MigrateFinishedHandler'],
12-
'Illuminate\Console\Events\CommandStarting' => ['AlwaysOpen\MigrationSnapshot\Handlers\MigrateStartingHandler'],
16+
CommandFinished::class => [MigrateFinishedHandler::class],
17+
CommandStarting::class => [MigrateStartingHandler::class],
1318
];
14-
}
19+
}

src/Handlers/MigrateStartingHandler.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public function handle(CommandStarting $event)
5959
// Never implicitly load fresh (from file) in production since it
6060
// would need to drop first, and that would be destructive.
6161
&& in_array(app()->environment(), explode(',', config('migration-snapshot.environments')), true)
62-
// No point in implicitly loading when it's not present.
63-
&& file_exists(database_path() . MigrateDumpCommand::SCHEMA_SQL_PATH_SUFFIX)
6462
) {
6563
// Must pass along options or it may use wrong DB or have
6664
// inconsistent output.
@@ -72,6 +70,12 @@ public function handle(CommandStarting $event)
7270
return;
7371
}
7472

73+
// No point in continuing to load when it's not present.
74+
if (!file_exists(MigrateDumpCommand::getSchemaSqlPath($db_driver))) {
75+
// CONSIDER: Logging or emitting console warning.
76+
return;
77+
}
78+
7579
// Only implicitly load when DB has *not* migrated any since load
7680
// would wipe existing data.
7781
$has_migrated_any = false;

tests/MigrateDumpTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use AlwaysOpen\MigrationSnapshot\Commands\MigrateDumpCommand;
43
use AlwaysOpen\MigrationSnapshot\Tests\TestCase;
54

65
class MigrateDumpTest extends TestCase
@@ -30,9 +29,7 @@ public function test_dump_callsAfterDumpClosure()
3029
$result = \Artisan::call('migrate:dump');
3130
$this->assertEquals(0, $result);
3231

33-
$schema_sql = file_get_contents(
34-
database_path() . MigrateDumpCommand::SCHEMA_SQL_PATH_SUFFIX
35-
);
32+
$schema_sql = file_get_contents($this->schemaSqlPath);
3633
$this->assertStringNotContainsString('/*', $schema_sql);
3734
}
3835
}

tests/MigrateHookTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace AlwaysOpen\MigrationSnapshot\Tests\Mysql;
54

65
use AlwaysOpen\MigrationSnapshot\Tests\TestCase;
@@ -24,8 +23,8 @@ public function test_handle()
2423
$this->assertEquals(0, $result);
2524

2625
$output_string = $output->fetch();
27-
$this->assertStringContainsString('Loaded schema', $output_string);
28-
$this->assertStringContainsString('Dumped schema', $output_string);
26+
$this->assertStringContainsString('Loaded ' . $this->dbDefault . ' schema', $output_string);
27+
$this->assertStringContainsString('Dumped ' . $this->dbDefault . ' schema', $output_string);
2928
}
3029

3130
public function test_handle_dumpsOnFresh()
@@ -43,7 +42,7 @@ public function test_handle_dumpsOnFresh()
4342
$this->assertEquals(0, $result);
4443

4544
$output_string = $output->fetch();
46-
$this->assertStringContainsString('Dumped schema', $output_string);
45+
$this->assertStringContainsString('Dumped ' . $this->dbDefault . ' schema', $output_string);
4746
}
4847

4948
public function test_handle_dumpsOnRollback()
@@ -62,7 +61,7 @@ public function test_handle_dumpsOnRollback()
6261
$this->assertEquals(0, $result);
6362

6463
$output_string = $output->fetch();
65-
$this->assertStringContainsString('Dumped schema', $output_string);
64+
$this->assertStringContainsString('Dumped ' . $this->dbDefault . ' schema', $output_string);
6665
}
6766

6867
public function test_handle_doesNotLoadWhenDbHasMigrated()
@@ -79,7 +78,7 @@ public function test_handle_doesNotLoadWhenDbHasMigrated()
7978
$this->assertEquals(0, $result);
8079

8180
$output_string = $output->fetch();
82-
$this->assertStringNotContainsString('Loaded schema', $output_string);
81+
$this->assertStringNotContainsString('Loaded ' . $this->dbDefault . ' schema', $output_string);
8382

8483
$this->assertEquals(1, \DB::table('test_ms')->count());
8584
}

tests/TestCase.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
use AlwaysOpen\MigrationSnapshot\Commands\MigrateDumpCommand;
8+
use AlwaysOpen\MigrationSnapshot\ServiceProvider;
89

910
class TestCase extends \Orchestra\Testbench\TestCase
1011
{
@@ -17,9 +18,9 @@ protected function setUp(): void
1718
{
1819
parent::setUp();
1920

20-
$this->schemaSqlPath = realpath(
21-
__DIR__ . '/../vendor/orchestra/testbench-core/laravel/database'
22-
) . MigrateDumpCommand::SCHEMA_SQL_PATH_SUFFIX;
21+
$this->schemaSqlPath = MigrateDumpCommand::getSchemaSqlPath(
22+
$this->app['config']->get('database.connections.' . $this->dbDefault . '.driver')
23+
);
2324
$this->schemaSqlDirectory = dirname($this->schemaSqlPath);
2425

2526
// Not leaving to tearDown since it can be useful to see result after
@@ -44,7 +45,7 @@ protected function getEnvironmentSetUp($app)
4445

4546
protected function getPackageProviders($app)
4647
{
47-
return ['\AlwaysOpen\MigrationSnapshot\ServiceProvider'];
48+
return [ServiceProvider::class];
4849
}
4950

5051
protected function createTestTablesWithoutMigrate() : void

0 commit comments

Comments
 (0)