Skip to content

Commit

Permalink
feat: add interactive migration selection for rollback command
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1Pro committed Jan 18, 2025
1 parent 54a67d7 commit 484ee2c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function handle()

$migrations = [];
if ($this->option('select')) {
$migrations = $this->getMigrationsForRollback();
$migrations = $this->getMigrationsForRollbacks();
}

$this->migrator->usingConnection($this->option('database'), function () use ($migrations) {
Expand Down Expand Up @@ -108,7 +108,8 @@ protected function getOptions()
*
* @return array<int, object> Returns an array of migration records, or empty array if no migrations exist
*/
private function getMigrationsForRollback()

private function getMigrationsForRollbacks()
{
$migrationsInstance = DB::table('migrations');

Expand Down
28 changes: 28 additions & 0 deletions src/Illuminate/Database/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,4 +758,32 @@ public function fireMigrationEvent($event)
{
$this->events?->dispatch($event);
}

protected function getMigrationBatches($migrations)
{
$options = $this->resolveOptions($migrations);
$selected = $options['selected'] ?? [];

if (count($selected) > 0) {
return collect($selected)->map(function ($migration) {
return (object) $migration;
})->pluck('batch', 'migration')->all();
}

return $this->repository->getRan();
}

protected function resolveOptions($migrations)
{
if (is_array($migrations)) {
return $migrations;
}

return [
'pretend' => false,
'step' => 0,
'batch' => 0,
'selected' => [],
];
}
}
2 changes: 1 addition & 1 deletion tests/Database/DatabaseMigrationRollbackCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testRollbackCommandCanBePretendedWithStepOption()
$migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
$migrator->shouldReceive('rollback')->once()->with([__DIR__ . DIRECTORY_SEPARATOR . 'migrations'], ['pretend' => true, 'step' => 2, 'batch' => 0, 'selected' => []]);

$this->runCommand($command, ['--pretend' => true, '--database' => 'foo', '--step' => 2, '--select' => true]);
$this->runCommand($command, ['--pretend' => true, '--step' => 2]);
}

protected function runCommand($command, $input = [])
Expand Down

0 comments on commit 484ee2c

Please sign in to comment.