1: | <?php |
2: | |
3: | namespace Aurora\System\Console\Commands\Migrations; |
4: | |
5: | use Aurora\System\Console\Commands\BaseCommand; |
6: | use Illuminate\Database\Migrations\Migrator; |
7: | use Symfony\Component\Console\Command\Command; |
8: | use Symfony\Component\Console\Input\InputInterface; |
9: | use Symfony\Component\Console\Input\InputOption; |
10: | use Symfony\Component\Console\Output\OutputInterface; |
11: | use Symfony\Component\Console\Question\ConfirmationQuestion; |
12: | |
13: | class RollbackCommand extends BaseCommand |
14: | { |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | protected $migrator; |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | public function __construct(Migrator $migrator) |
29: | { |
30: | parent::__construct(); |
31: | |
32: | $this->migrator = $migrator; |
33: | } |
34: | |
35: | protected function configure(): void |
36: | { |
37: | $this->setName('migrate:rollback') |
38: | ->setDescription('Rollback the last database migration') |
39: | ->addOption('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use') |
40: | ->addOption('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production') |
41: | ->addOption('path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The path(s) to the migrations files to be executed') |
42: | ->addOption('realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths') |
43: | ->addOption('pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run') |
44: | ->addOption('step', null, InputOption::VALUE_OPTIONAL, 'The number of migrations to be reverted'); |
45: | } |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | protected function execute(InputInterface $input, OutputInterface $output): int |
53: | { |
54: | $defaultAnswer = $input->getOption('no-interaction'); |
55: | $helper = $this->getHelper('question'); |
56: | $question = new ConfirmationQuestion('Do you really wish to run this command? (Y/N)', $defaultAnswer); |
57: | if (!$helper->ask($input, $output, $question)) { |
58: | return Command::SUCCESS; |
59: | } |
60: | |
61: | $this->migrator->usingConnection($input->getOption('database'), function () use ($input, $output) { |
62: | $this->migrator->setOutput($output)->rollback( |
63: | $this->getMigrationPaths($input, $output), |
64: | [ |
65: | 'pretend' => $input->getOption('pretend'), |
66: | 'step' => (int) $input->getOption('step'), |
67: | ] |
68: | ); |
69: | }); |
70: | |
71: | return Command::SUCCESS; |
72: | } |
73: | } |
74: | |