diff --git a/config/migration-snapshot.php b/config/migration-snapshot.php index e3b0aeb..71fe945 100644 --- a/config/migration-snapshot.php +++ b/config/migration-snapshot.php @@ -44,7 +44,7 @@ | */ 'trim-underscores' => env('MIGRATION_SNAPSHOT_TRIM_UNDERSCORES', true), - + /* |-------------------------------------------------------------------------- | Include Data @@ -56,4 +56,15 @@ | */ 'data' => env('MIGRATION_SNAPSHOT_DATA', false), + + /* + |-------------------------------------------------------------------------- + | Data Excluded + |-------------------------------------------------------------------------- + | + | Comma separated table names to exclude when dumping with data. (The + | "migrations" table will always be excluded regardless.) + | + */ + 'data-excluded' => env('MIGRATION_SNAPSHOT_DATA_EXCLUDED', ''), ]; diff --git a/src/Commands/MigrateDumpCommand.php b/src/Commands/MigrateDumpCommand.php index feff008..c4cbe98 100644 --- a/src/Commands/MigrateDumpCommand.php +++ b/src/Commands/MigrateDumpCommand.php @@ -187,11 +187,16 @@ private static function mysqlSchemaDump(array $db_config, string $schema_sql_pat */ private static function mysqlDataDump(array $db_config, string $data_sql_path) : int { - passthru( - static::mysqlCommandPrefix($db_config) + $command = static::mysqlCommandPrefix($db_config) . ' --result-file=' . escapeshellarg($data_sql_path) - . ' --no-create-info --skip-triggers' - . ' --ignore-table=' . escapeshellarg($db_config['database'] . '.migrations'), + . ' --no-create-info --skip-comments --skip-triggers' + . ' --ignore-table=' . escapeshellarg($db_config['database'] . '.migrations'); + foreach (static::dataExcluded() as $table) { + $command .= ' --ignore-table=' . escapeshellarg($db_config['database'] . '.' . $table); + } + + passthru( + $command, $exit_code ); @@ -343,11 +348,15 @@ function ($line) { */ private static function pgsqlDataDump(array $db_config, string $data_sql_path) : int { - passthru( - static::pgsqlCommandPrefix($db_config) + $command = static::pgsqlCommandPrefix($db_config) . ' --file=' . escapeshellarg($data_sql_path) . ' --exclude-table=' . escapeshellarg($db_config['database'] . '.migrations') - . ' --data-only', + . ' --data-only'; + foreach (static::dataExcluded() as $table) { + $command .= ' --exclude-table=' . escapeshellarg($db_config['database'] . '.' . $table); + } + passthru( + $command, $exit_code ); @@ -452,7 +461,10 @@ private static function sqliteDataDump(array $db_config, string $data_sql_path) foreach ($tables as $table) { // We don't want to dump the migrations table here - if ('migrations' === $table) { + if ( + 'migrations' === $table + || in_array($table, static::dataExcluded(), true) + ) { continue; } @@ -479,4 +491,17 @@ private static function sqliteDataDump(array $db_config, string $data_sql_path) return $exit_code; } + + private static function dataExcluded() : array + { + $data_excluded = config('migration-snapshot.data-excluded'); + if (is_iterable($data_excluded)) { + return $data_excluded; + } + return array_filter( + explode(',', + $data_excluded + ) + ); + } }