|
13 | 13 |
|
14 | 14 | namespace FiveLab\Component\Migrator\Console; |
15 | 15 |
|
16 | | -use FiveLab\Component\Migrator\MigrateDirection; |
17 | | -use FiveLab\Component\Migrator\MigrationExecutedState; |
18 | 16 | use FiveLab\Component\Migrator\MigratorRegistry; |
19 | 17 | use Symfony\Component\Console\Attribute\AsCommand; |
20 | 18 | use Symfony\Component\Console\Command\Command; |
21 | | -use Symfony\Component\Console\Input\InputArgument; |
22 | 19 | use Symfony\Component\Console\Input\InputInterface; |
23 | | -use Symfony\Component\Console\Input\InputOption; |
24 | 20 | use Symfony\Component\Console\Output\OutputInterface; |
25 | | -use Symfony\Component\Console\Style\SymfonyStyle; |
26 | 21 |
|
27 | 22 | #[AsCommand(name: 'migrations:migrate', description: 'Run migrations.')] |
28 | 23 | class MigrateCommand extends Command |
29 | 24 | { |
| 25 | + use CommandHelperTrait; |
| 26 | + |
30 | 27 | public function __construct(private readonly MigratorRegistry $registry) |
31 | 28 | { |
32 | | - parent::__construct(null); |
| 29 | + parent::__construct(); |
33 | 30 | } |
34 | 31 |
|
35 | 32 | protected function configure(): void |
36 | 33 | { |
37 | | - $this |
38 | | - ->addArgument('group', InputArgument::REQUIRED, 'The group in which to run migrations. ') |
39 | | - ->addArgument('version', InputArgument::OPTIONAL, 'The version to run migrations.') |
40 | | - ->addOption('down', null, InputOption::VALUE_NONE, 'Down migrations.'); |
| 34 | + $this->configureMigrateInput($this); |
41 | 35 | } |
42 | 36 |
|
43 | 37 | protected function execute(InputInterface $input, OutputInterface $output): int |
44 | 38 | { |
45 | | - $style = new SymfonyStyle($input, $output); |
46 | | - |
47 | | - $group = $input->getArgument('group'); |
48 | | - $toVersion = $input->getArgument('version'); |
49 | | - $direction = $input->getOption('down') ? MigrateDirection::Down : MigrateDirection::Up; |
| 39 | + [$group, $toVersion, $direction] = $this->readInput($input); |
50 | 40 |
|
51 | | - $question = sprintf( |
52 | | - '<comment>WARNING!</comment> You are about to execute a migration <comment>%s</comment> (<comment>%s</comment>). Are you sure you wish to continue?', |
| 41 | + $question = \sprintf( |
| 42 | + '<comment>WARNING!</comment> You are about to run a <comment>%s</comment> migrations (<comment>%s</comment>). Are you sure you wish to continue?', |
53 | 43 | $group, |
54 | 44 | $direction->name |
55 | 45 | ); |
56 | 46 |
|
57 | | - if ($input->isInteractive() && !$style->confirm($question, false)) { |
58 | | - $style->error('Migration canceled.'); |
59 | | - |
| 47 | + if (!$this->confirmExecuteMigration($input, $output, $question)) { |
60 | 48 | return self::FAILURE; |
61 | 49 | } |
62 | 50 |
|
63 | 51 | $migrator = $this->registry->get($group); |
64 | 52 |
|
65 | 53 | foreach ($migrator->migrate($direction, $toVersion) as $result) { |
66 | | - if ($result->state === MigrationExecutedState::Skipped) { |
67 | | - $output->writeln(\sprintf( |
68 | | - 'Skip migration <comment>%s</comment>.', |
69 | | - $result->metadata->class->name |
70 | | - ), OutputInterface::VERBOSITY_DEBUG); |
71 | | - } else { |
72 | | - $output->writeln(\sprintf( |
73 | | - 'Executed migration <comment>%s</comment> in %.2f seconds.', |
74 | | - $result->metadata->class->name, |
75 | | - $result->executeTime |
76 | | - ), OutputInterface::VERBOSITY_NORMAL); |
77 | | - } |
| 54 | + $this->outputMigrationResult($output, $result); |
78 | 55 | } |
79 | 56 |
|
80 | 57 | return self::SUCCESS; |
|
0 commit comments