Skip to content

Commit e61cd5b

Browse files
authored
feat(config/migration-snapshot.php): Add after-dump closure. (#14)
* feat(config/migration-snapshot.php): Add `after-dump` closure. * fix(MigrateDumpTest): Remove trailing comma for compatibility with PHP 7.2.
1 parent 47aeadd commit e61cd5b

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

config/migration-snapshot.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
|
4545
*/
4646
'trim-underscores' => env('MIGRATION_SNAPSHOT_TRIM_UNDERSCORES', true),
47-
47+
4848
/*
4949
|--------------------------------------------------------------------------
5050
| Include Data
@@ -56,4 +56,18 @@
5656
|
5757
*/
5858
'data' => env('MIGRATION_SNAPSHOT_DATA', false),
59+
60+
/*
61+
|--------------------------------------------------------------------------
62+
| After Dump
63+
|--------------------------------------------------------------------------
64+
|
65+
| Run this closure after dumping snapshot. Helps when output may vary by
66+
| environment in unimportant ways which would just pollute the SCM history
67+
| with noisy changes.
68+
|
69+
| Must accept two arguments: `function ($schema_sql_path, $data_sql_path)`.
70+
|
71+
*/
72+
'after-dump' => null,
5973
];

src/Commands/MigrateDumpCommand.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,31 @@ public function handle()
6262

6363
$this->info('Dumped schema');
6464

65-
if (! $this->option('include-data')) {
66-
return;
67-
}
65+
$data_sql_path = null;
66+
if ($this->option('include-data')) {
67+
$this->info('Starting Data Dump');
6868

69-
$this->info('Starting Data Dump');
69+
$data_sql_path = database_path() . self::DATA_SQL_PATH_SUFFIX;
7070

71-
$data_sql_path = database_path() . self::DATA_SQL_PATH_SUFFIX;
71+
$method = $db_config['driver'] . 'DataDump';
72+
$exit_code = self::{$method}($db_config, $data_sql_path);
7273

73-
$method = $db_config['driver'] . 'DataDump';
74-
$exit_code = self::{$method}($db_config, $data_sql_path);
74+
if (0 !== $exit_code) {
75+
if (file_exists($data_sql_path)) {
76+
unlink($data_sql_path);
77+
}
7578

76-
if (0 !== $exit_code) {
77-
if (file_exists($data_sql_path)) {
78-
unlink($data_sql_path);
79+
exit($exit_code);
7980
}
80-
81-
exit($exit_code);
8281
}
8382

8483
$this->info('Dumped Data');
84+
85+
$after_dump = config('migration-snapshot.after-dump');
86+
if ($after_dump) {
87+
$after_dump($schema_sql_path, $data_sql_path);
88+
$this->info('Ran After-dump');
89+
}
8590
}
8691

8792
/**

tests/MigrateDumpTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use OrisIntel\MigrationSnapshot\Commands\MigrateDumpCommand;
4+
use OrisIntel\MigrationSnapshot\Tests\TestCase;
5+
6+
class MigrateDumpTest extends TestCase
7+
{
8+
protected function getEnvironmentSetUp($app)
9+
{
10+
parent::getEnvironmentSetUp($app);
11+
$app['config']->set(
12+
'migration-snapshot.after-dump',
13+
function ($schema_sql_path, $data_sql_path) {
14+
file_put_contents(
15+
$schema_sql_path,
16+
preg_replace(
17+
'~^/\*.*\*/;?[\r\n]+~mu', // Remove /**/ comments.
18+
'',
19+
file_get_contents($schema_sql_path)
20+
)
21+
);
22+
}
23+
);
24+
}
25+
26+
public function test_dump_callsAfterDumpClosure()
27+
{
28+
$this->createTestTablesWithoutMigrate();
29+
// TODO: Fix inclusion of `, ['--quiet' => true]` here breaking test.
30+
$result = \Artisan::call('migrate:dump');
31+
$this->assertEquals(0, $result);
32+
33+
$schema_sql = file_get_contents(
34+
database_path() . MigrateDumpCommand::SCHEMA_SQL_PATH_SUFFIX
35+
);
36+
$this->assertNotContains('/*', $schema_sql);
37+
}
38+
}

0 commit comments

Comments
 (0)