Skip to content

Commit

Permalink
feat(config/migration-snapshot.php): Add after-dump closure. (#14)
Browse files Browse the repository at this point in the history
* feat(config/migration-snapshot.php): Add `after-dump` closure.

* fix(MigrateDumpTest): Remove trailing comma for compatibility with PHP 7.2.
  • Loading branch information
paulrrogers authored Dec 9, 2019
1 parent 47aeadd commit e61cd5b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
16 changes: 15 additions & 1 deletion config/migration-snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
|
*/
'trim-underscores' => env('MIGRATION_SNAPSHOT_TRIM_UNDERSCORES', true),

/*
|--------------------------------------------------------------------------
| Include Data
Expand All @@ -56,4 +56,18 @@
|
*/
'data' => env('MIGRATION_SNAPSHOT_DATA', false),

/*
|--------------------------------------------------------------------------
| After Dump
|--------------------------------------------------------------------------
|
| Run this closure after dumping snapshot. Helps when output may vary by
| environment in unimportant ways which would just pollute the SCM history
| with noisy changes.
|
| Must accept two arguments: `function ($schema_sql_path, $data_sql_path)`.
|
*/
'after-dump' => null,
];
29 changes: 17 additions & 12 deletions src/Commands/MigrateDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,31 @@ public function handle()

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

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

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

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

$method = $db_config['driver'] . 'DataDump';
$exit_code = self::{$method}($db_config, $data_sql_path);
if (0 !== $exit_code) {
if (file_exists($data_sql_path)) {
unlink($data_sql_path);
}

if (0 !== $exit_code) {
if (file_exists($data_sql_path)) {
unlink($data_sql_path);
exit($exit_code);
}

exit($exit_code);
}

$this->info('Dumped Data');

$after_dump = config('migration-snapshot.after-dump');
if ($after_dump) {
$after_dump($schema_sql_path, $data_sql_path);
$this->info('Ran After-dump');
}
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/MigrateDumpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

class MigrateDumpTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);
$app['config']->set(
'migration-snapshot.after-dump',
function ($schema_sql_path, $data_sql_path) {
file_put_contents(
$schema_sql_path,
preg_replace(
'~^/\*.*\*/;?[\r\n]+~mu', // Remove /**/ comments.
'',
file_get_contents($schema_sql_path)
)
);
}
);
}

public function test_dump_callsAfterDumpClosure()
{
$this->createTestTablesWithoutMigrate();
// TODO: Fix inclusion of `, ['--quiet' => true]` here breaking test.
$result = \Artisan::call('migrate:dump');
$this->assertEquals(0, $result);

$schema_sql = file_get_contents(
database_path() . MigrateDumpCommand::SCHEMA_SQL_PATH_SUFFIX
);
$this->assertNotContains('/*', $schema_sql);
}
}

0 comments on commit e61cd5b

Please sign in to comment.