Skip to content

Commit

Permalink
[Feat] Run seeder after migration command (#260)
Browse files Browse the repository at this point in the history
* feat: run seeder after running migration

* refactor: default db seeder when class not defined

* fix: prevent run seeder in dry-run mode
  • Loading branch information
SonyPradana authored Dec 23, 2023
1 parent 6b81f37 commit f702436
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
45 changes: 36 additions & 9 deletions src/System/Integrate/Console/MigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
use function System\Console\warn;

/**
* @property ?int $take
* @property bool $force
* @property ?int $take
* @property bool $force
* @property string|bool $seed
*/
class MigrationCommand extends Command
{
Expand Down Expand Up @@ -77,14 +78,16 @@ public function printHelp()
'database:show' => 'Show database table',
],
'options' => [
'--dry-run' => 'Excute migration but olny get query output.',
'--force' => 'Force runing migration/database query in production',
'--dry-run' => 'Excute migration but olny get query output.',
'--force' => 'Force runing migration/database query in production.',
'--seed' => 'Run seeder after migration.',
'--seed-namespace' => 'Run seeder after migration using class namespace.',
],
'relation' => [
'migrate' => ['--dry-run', '--force'],
'migrate:fresh' => ['--dry-run', '--force'],
'migrate' => ['--seed', '--dry-run', '--force'],
'migrate:fresh' => ['--seed', '--dry-run', '--force'],
'migrate:reset' => ['--dry-run', '--force'],
'migrate:refresh' => ['--dry-run', '--force'],
'migrate:refresh' => ['--seed', '--dry-run', '--force'],
'migrate:rollback' => ['--dry-run', '--force'],
'database:create' => ['--force'],
'database:drop' => ['--force'],
Expand Down Expand Up @@ -198,7 +201,7 @@ public function migration(bool $silent = false): int

$print->out();

return 0;
return $this->seed();
}

public function fresh(bool $silent = false): int
Expand Down Expand Up @@ -251,7 +254,7 @@ public function fresh(bool $silent = false): int

$print->out();

return 0;
return $this->seed();
}

public function reset(bool $silent = false): int
Expand Down Expand Up @@ -457,4 +460,28 @@ public function tableShow(string $table): int

return 0;
}

/**
* Integrate seeder during run migration.
*/
private function seed(): int
{
if ($this->option('dry-run', false)) {
return 0;
}
if ($this->seed) {
$seed = true === $this->seed ? null : $this->seed;

return (new SeedCommand([], ['class' => $seed]))->main();
}

$namespace = $this->option('seed-namespace', false);
if ($namespace) {
$namespace = true === $namespace ? null : $namespace;

return (new SeedCommand([], ['name-space' => $namespace]))->main();
}

return 0;
}
}
12 changes: 8 additions & 4 deletions src/System/Integrate/Console/SeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public function main(): int
return 2;
}

if (null !== $class && null !== $namespace) {
warn('Use only one class or namespace, be specific.')->out(false);

return 1;
}

if (null === $class && null !== $namespace) {
$class = $namespace;
}
Expand All @@ -96,9 +102,7 @@ public function main(): int
}

if (null === $class && null === $namespace) {
warn('command db:seed require --class or --name-space flag follow by class name.')->out(false);

return 1;
$class = 'Database\\Seeders\\DatabaseSeeder';
}

if (false === class_exists($class)) {
Expand All @@ -111,7 +115,7 @@ public function main(): int
try {
app()->call([$class, 'run']);

ok('Success run seeder')->out(false);
ok('Success run seeder ' . $class)->out(false);
} catch (\Throwable $th) {
warn($th->getMessage())->out(false);
$exit = 1;
Expand Down

0 comments on commit f702436

Please sign in to comment.