1
|
|
|
<?php |
2
|
|
|
namespace Yiisoft\Yii\Cycle\Command; |
3
|
|
|
|
4
|
|
|
use Spiral\Migrations\State; |
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Component\Console\Input\StreamableInputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
9
|
|
|
use Symfony\Component\Console\Question\Question; |
10
|
|
|
use Yiisoft\Yii\Cycle\Generator\ShowChangesGenerator; |
11
|
|
|
|
12
|
|
|
class GenerateCommand extends BaseMigrationCommand |
13
|
|
|
{ |
14
|
|
|
protected static $defaultName = 'migrate/generate'; |
15
|
|
|
|
16
|
|
|
public function configure(): void |
17
|
|
|
{ |
18
|
|
|
$this->setDescription('Generates a migration'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
22
|
|
|
{ |
23
|
|
|
// check existing unapplied migrations |
24
|
|
|
$listAfter = $this->migrator->getMigrations(); |
25
|
|
|
foreach ($listAfter as $migration) { |
26
|
|
|
if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) { |
27
|
|
|
$output->writeln('<fg=red>Outstanding migrations found, run `migrate/up` first.</>'); |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
// run generator |
32
|
|
|
$this->cycleOrmHelper->generateMigrations($this->migrator, $this->config, [ |
33
|
|
|
new ShowChangesGenerator($output), |
34
|
|
|
]); |
35
|
|
|
|
36
|
|
|
$listBefore = $this->migrator->getMigrations(); |
37
|
|
|
$added = count($listBefore) - count($listAfter); |
38
|
|
|
$output->writeln("<info>Added {$added} file(s)</info>"); |
39
|
|
|
|
40
|
|
|
// print added migrations |
41
|
|
|
if ($added > 0) { |
42
|
|
|
foreach ($listBefore as $migration) { |
43
|
|
|
if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) { |
44
|
|
|
$output->writeln($migration->getState()->getName()); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} else { |
48
|
|
|
$output->writeln('<info>If you want to create new empty migration, use <fg=yellow>migrate/create</></info>'); |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
$qaHelper = $this->getHelper('question'); |
52
|
|
|
|
53
|
|
|
if ($input->isInteractive() && $input instanceof StreamableInputInterface) { |
54
|
|
|
$question = new ConfirmationQuestion('Would you like to create empty migration right now? (Y/n)', true); |
55
|
|
|
$answer = $qaHelper->ask($input, $output, $question); |
56
|
|
|
if (!$answer) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
// get the name for a new migration |
60
|
|
|
$question = new Question('Please enter an unique name for the new migration: '); |
61
|
|
|
$name = $qaHelper->ask($input, $output, $question); |
62
|
|
|
if (empty($name)) { |
63
|
|
|
$output->writeln('<fg=red>You entered an empty name. Exit</>'); |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
// create an empty migration |
67
|
|
|
$this->createEmptyMigration($output, $name); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|