-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
RedoCommand.php
121 lines (98 loc) · 3.92 KB
/
RedoCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace Yiisoft\Yii\Db\Migration\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
use Yiisoft\Yii\Db\Migration\Informer\ConsoleMigrationInformer;
use Yiisoft\Yii\Db\Migration\Migrator;
use Yiisoft\Yii\Db\Migration\Runner\DownRunner;
use Yiisoft\Yii\Db\Migration\Runner\UpdateRunner;
use Yiisoft\Yii\Db\Migration\Service\MigrationService;
use function array_keys;
use function array_reverse;
use function count;
/**
* Redoes the last few migrations.
*
* This command will first revert the specified migrations, and then apply
* them again. For example,
*
* ```
* yii migrate:redo # redo the last applied migration
* yii migrate:redo 3 # redo last 3 applied migrations
* yii migrate:redo all # redo all migrations
* ```
*/
#[AsCommand('migrate:redo', 'Redoes the last few migrations.')]
final class RedoCommand extends Command
{
public function __construct(
private MigrationService $migrationService,
private Migrator $migrator,
ConsoleMigrationInformer $informer,
private DownRunner $downRunner,
private UpdateRunner $updateRunner
) {
$this->migrator->setInformer($informer);
parent::__construct();
}
protected function configure(): void
{
$this
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Number of migrations to redo.', null);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->migrator->setIO($io);
$this->migrationService->setIO($io);
$this->downRunner->setIO($io);
$this->updateRunner->setIO($io);
$this->migrationService->before(self::getDefaultName() ?? '');
$limit = filter_var($input->getOption('limit'), FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
if ($limit < 0) {
$io->error('The step argument must be greater than 0.');
$this->migrationService->databaseConnection();
return Command::INVALID;
}
$migrations = $this->migrator->getHistory($limit);
if (empty($migrations)) {
$io->warning('No migration has been done before.');
return Command::FAILURE;
}
$migrations = array_keys($migrations);
$n = count($migrations);
$output->writeln("<warning>Total $n " . ($n === 1 ? 'migration' : 'migrations') . " to be redone:</warning>\n");
foreach ($migrations as $migration) {
$output->writeln("\t<info>$migration</info>");
}
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion(
"\n<fg=cyan>Redo the above " . ($n === 1 ? 'migration y/n: ' : 'migrations y/n: '),
true
);
if ($helper->ask($input, $output, $question)) {
/** @psalm-var class-string[] $migrations */
$instances = $this->migrationService->makeRevertibleMigrations($migrations);
foreach ($instances as $instance) {
$this->downRunner->run($instance);
}
foreach (array_reverse($instances) as $instance) {
$this->updateRunner->run($instance);
}
$output->writeln(
"\n<info> >>> $n " . ($n === 1 ? 'migration was' : 'migrations were') . " redone.</info>\n"
);
$io->success('Migration redone successfully.');
}
$this->migrationService->databaseConnection();
return Command::SUCCESS;
}
}