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 c4e8505 commit df846ac
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public function handle()
'pretend' => $this->option('pretend'),
'step' => (int) $this->option('step'),
'batch' => (int) $this->option('batch'),
],
$migrations
'select' => $migrations,
]
);
});

Expand Down
5 changes: 2 additions & 3 deletions src/Illuminate/Database/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,13 @@ protected function runUp($file, $batch, $pretend)
* @param string[]|string $paths
* @param array<string, mixed> $options
* @return string[]
* @param array $userChosenMigrations
*/
public function rollback($paths = [], array $options = [], array $userChosenMigrations = [])
public function rollback($paths = [], array $options = [])
{
// We want to pull in the last batch of migrations that ran on the previous
// migration operation. We'll then reverse those migrations and run each
// of them "down" to reverse the last migration "operation" which ran.
$migrations = count($userChosenMigrations) > 0 ? $userChosenMigrations : $this->getMigrationsForRollback($options);
$migrations = count($options['select']) > 0 ? $options['select'] : $this->getMigrationsForRollback($options);

if (count($migrations) === 0) {
$this->fireMigrationEvent(new NoPendingMigrations('down'));
Expand Down
8 changes: 4 additions & 4 deletions tests/Database/DatabaseMigrationRollbackCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testRollbackCommandCallsMigratorWithProperArguments()
return $callback();
});
$migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
$migrator->shouldReceive('rollback')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => 0, 'batch' => 0]);
$migrator->shouldReceive('rollback')->once()->with([__DIR__ . DIRECTORY_SEPARATOR . 'migrations'], ['pretend' => false, 'step' => 0, 'batch' => 0]);

$this->runCommand($command);
}
Expand All @@ -44,7 +44,7 @@ public function testRollbackCommandCallsMigratorWithStepOption()
return $callback();
});
$migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
$migrator->shouldReceive('rollback')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => 2, 'batch' => 0]);
$migrator->shouldReceive('rollback')->once()->with([__DIR__ . DIRECTORY_SEPARATOR . 'migrations'], ['pretend' => false, 'step' => 2, 'batch' => 0]);

$this->runCommand($command, ['--step' => 2]);
}
Expand All @@ -60,7 +60,7 @@ public function testRollbackCommandCanBePretended()
return $callback();
});
$migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
$migrator->shouldReceive('rollback')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], true);
$migrator->shouldReceive('rollback')->once()->with([__DIR__ . DIRECTORY_SEPARATOR . 'migrations'], true);

$this->runCommand($command, ['--pretend' => true, '--database' => 'foo']);
}
Expand All @@ -76,7 +76,7 @@ public function testRollbackCommandCanBePretendedWithStepOption()
return $callback();
});
$migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
$migrator->shouldReceive('rollback')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => true, 'step' => 2, 'batch' => 0]);
$migrator->shouldReceive('rollback')->once()->with([__DIR__ . DIRECTORY_SEPARATOR . 'migrations'], ['pretend' => true, 'step' => 2, 'batch' => 0]);

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

0 comments on commit df846ac

Please sign in to comment.