1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\Command\Migration; |
6
|
|
|
|
7
|
|
|
use Cycle\Migrations\State; |
8
|
|
|
use Cycle\Schema\Compiler; |
9
|
|
|
use Cycle\Schema\Generator\Migrations\GenerateMigrations; |
10
|
|
|
use Cycle\Schema\Generator\PrintChanges; |
11
|
|
|
use Cycle\Schema\Registry; |
12
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Input\StreamableInputInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
17
|
|
|
use Symfony\Component\Console\Question\Question; |
18
|
|
|
use Yiisoft\Yii\Console\ExitCode; |
19
|
|
|
use Yiisoft\Yii\Cycle\Schema\SchemaConveyorInterface; |
20
|
|
|
|
21
|
|
|
final class GenerateCommand extends BaseMigrationCommand |
22
|
|
|
{ |
23
|
|
|
protected static $defaultName = 'migrate/generate'; |
24
|
|
|
protected static $defaultDescription = 'Generates a migration'; |
25
|
|
|
|
26
|
5 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
27
|
|
|
{ |
28
|
5 |
|
$migrator = $this->promise->getMigrator(); |
29
|
|
|
// check existing unapplied migrations |
30
|
5 |
|
$listAfter = $migrator->getMigrations(); |
31
|
5 |
|
foreach ($listAfter as $migration) { |
32
|
1 |
|
if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) { |
33
|
1 |
|
$output->writeln('<fg=red>Outstanding migrations found, run `migrate/up` first.</>'); |
34
|
1 |
|
return ExitCode::OK; |
35
|
|
|
} |
36
|
|
|
} |
37
|
4 |
|
$conveyor = $this->promise->getSchemaConveyor(); |
38
|
|
|
|
39
|
|
|
// migrations generator |
40
|
4 |
|
$conveyor->addGenerator( |
41
|
4 |
|
SchemaConveyorInterface::STAGE_USERLAND, |
42
|
4 |
|
new GenerateMigrations($migrator->getRepository(), $this->promise->getMigrationConfig()) |
43
|
4 |
|
); |
44
|
|
|
// show DB changes |
45
|
4 |
|
$conveyor->addGenerator(SchemaConveyorInterface::STAGE_USERLAND, new PrintChanges($output)); |
46
|
|
|
// compile schema and convert diffs to new migrations |
47
|
4 |
|
(new Compiler())->compile(new Registry($this->promise->getDatabaseProvider()), $conveyor->getGenerators()); |
48
|
|
|
|
49
|
|
|
// compare migrations list before and after |
50
|
4 |
|
$listBefore = $migrator->getMigrations(); |
51
|
4 |
|
$added = count($listBefore) - count($listAfter); |
52
|
4 |
|
$output->writeln("<info>Added {$added} file(s)</info>"); |
53
|
|
|
|
54
|
|
|
// print added migrations |
55
|
4 |
|
if ($added > 0) { |
56
|
1 |
|
foreach ($listBefore as $migration) { |
57
|
1 |
|
if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) { |
58
|
1 |
|
$output->writeln($migration->getState()->getName()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} else { |
62
|
3 |
|
$output->writeln( |
63
|
3 |
|
'<info>If you want to create new empty migration, use <fg=yellow>migrate/create</></info>' |
64
|
3 |
|
); |
65
|
|
|
|
66
|
3 |
|
if ($input->isInteractive() && $input instanceof StreamableInputInterface) { |
67
|
|
|
/** @var QuestionHelper $qaHelper */ |
68
|
2 |
|
$qaHelper = $this->getHelper('question'); |
69
|
|
|
|
70
|
2 |
|
$question = new ConfirmationQuestion('Would you like to create empty migration right now? (Y/n)', true); |
71
|
2 |
|
$answer = $qaHelper->ask($input, $output, $question); |
72
|
2 |
|
if (!$answer) { |
73
|
|
|
return ExitCode::OK; |
74
|
|
|
} |
75
|
|
|
// get the name for a new migration |
76
|
2 |
|
$question = new Question('Please enter an unique name for the new migration: '); |
77
|
2 |
|
$name = $qaHelper->ask($input, $output, $question); |
78
|
2 |
|
if (empty($name)) { |
79
|
1 |
|
$output->writeln('<fg=red>You entered an empty name. Exit</>'); |
80
|
1 |
|
return ExitCode::OK; |
81
|
|
|
} |
82
|
|
|
// create an empty migration |
83
|
1 |
|
$this->createEmptyMigration($output, $name); |
84
|
|
|
} |
85
|
|
|
} |
86
|
3 |
|
return ExitCode::OK; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|