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