1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Yiisoft\Yii\Cycle\Command\Migration; |
||
6 | |||
7 | use Cycle\Migrations\MigrationInterface; |
||
8 | use Cycle\Migrations\State; |
||
9 | use Psr\EventDispatcher\EventDispatcherInterface; |
||
10 | use Symfony\Component\Console\Helper\QuestionHelper; |
||
11 | use Symfony\Component\Console\Input\InputInterface; |
||
12 | use Symfony\Component\Console\Output\OutputInterface; |
||
13 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
||
14 | use Yiisoft\Yii\Console\ExitCode; |
||
15 | use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy; |
||
16 | use Yiisoft\Yii\Cycle\Event\AfterMigrate; |
||
17 | use Yiisoft\Yii\Cycle\Event\BeforeMigrate; |
||
18 | |||
19 | final class DownCommand extends BaseMigrationCommand |
||
20 | { |
||
21 | protected static $defaultName = 'migrate/down'; |
||
22 | protected static $defaultDescription = 'Rolls back the last applied migration'; |
||
23 | |||
24 | private EventDispatcherInterface $eventDispatcher; |
||
25 | |||
26 | 4 | public function __construct(CycleDependencyProxy $promise, EventDispatcherInterface $eventDispatcher) |
|
27 | { |
||
28 | 4 | $this->eventDispatcher = $eventDispatcher; |
|
29 | 4 | parent::__construct($promise); |
|
30 | } |
||
31 | |||
32 | 4 | protected function execute(InputInterface $input, OutputInterface $output): int |
|
33 | { |
||
34 | 4 | $migrations = $this->findMigrations($output); |
|
35 | // check any executed migration |
||
36 | 4 | foreach (array_reverse($migrations) as $migration) { |
|
37 | 3 | if ($migration->getState()->getStatus() === State::STATUS_EXECUTED) { |
|
38 | 3 | break; |
|
39 | } |
||
40 | } |
||
41 | 4 | if (!isset($migration)) { |
|
42 | 1 | $output->writeln('<fg=red>No migration found for rollback</>'); |
|
43 | 1 | return ExitCode::OK; |
|
44 | } |
||
45 | |||
46 | 3 | $migrator = $this->promise->getMigrator(); |
|
47 | |||
48 | // Confirm |
||
49 | 3 | if (!$migrator->getConfig()->isSafe()) { |
|
50 | 3 | $output->writeln('<fg=yellow>Migration to be reverted:</>'); |
|
51 | 3 | $output->writeln('— <fg=cyan>' . $migration->getState()->getName() . '</>'); |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
52 | 3 | if ($input->isInteractive()) { |
|
53 | /** @var QuestionHelper $qaHelper */ |
||
54 | 1 | $qaHelper = $this->getHelper('question'); |
|
55 | 1 | $question = new ConfirmationQuestion('Revert the above migration? (yes|no) ', false); |
|
56 | 1 | if (!$qaHelper->ask($input, $output, $question)) { |
|
57 | 1 | return ExitCode::OK; |
|
58 | } |
||
59 | } |
||
60 | } |
||
61 | |||
62 | 2 | $this->eventDispatcher->dispatch(new BeforeMigrate()); |
|
63 | try { |
||
64 | 2 | $migration = $migrator->rollback(); |
|
65 | 2 | if (!$migration instanceof MigrationInterface) { |
|
66 | 1 | throw new \Exception('Migration not found'); |
|
67 | } |
||
68 | |||
69 | 1 | $state = $migration->getState(); |
|
70 | 1 | $status = $state->getStatus(); |
|
71 | 1 | $output->writeln( |
|
72 | 1 | sprintf('<fg=cyan>%s</>: %s', $state->getName(), self::MIGRATION_STATUS[$status] ?? $status) |
|
73 | 1 | ); |
|
74 | } finally { |
||
75 | 2 | $this->eventDispatcher->dispatch(new AfterMigrate()); |
|
76 | } |
||
77 | 1 | return ExitCode::OK; |
|
78 | } |
||
79 | } |
||
80 |