1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Configuration\Configuration; |
8
|
|
|
use Doctrine\Migrations\Tools\Console\Helper\MigrationStatusInfosHelper; |
9
|
|
|
use Doctrine\Migrations\Version; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use function count; |
14
|
|
|
use function in_array; |
15
|
|
|
use function max; |
16
|
|
|
use function sprintf; |
17
|
|
|
use function str_repeat; |
18
|
|
|
use function strlen; |
19
|
|
|
|
20
|
|
|
class StatusCommand extends AbstractCommand |
21
|
|
|
{ |
22
|
11 |
|
protected function configure() : void |
23
|
|
|
{ |
24
|
|
|
$this |
25
|
11 |
|
->setName('migrations:status') |
26
|
11 |
|
->setDescription('View the status of a set of migrations.') |
27
|
11 |
|
->addOption( |
28
|
11 |
|
'show-versions', |
29
|
11 |
|
null, |
30
|
11 |
|
InputOption::VALUE_NONE, |
31
|
11 |
|
'This will display a list of all available migrations and their status' |
32
|
|
|
) |
33
|
11 |
|
->setHelp(<<<EOT |
34
|
11 |
|
The <info>%command.name%</info> command outputs the status of a set of migrations: |
35
|
|
|
|
36
|
|
|
<info>%command.full_name%</info> |
37
|
|
|
|
38
|
|
|
You can output a list of all available migrations and their status with <comment>--show-versions</comment>: |
39
|
|
|
|
40
|
|
|
<info>%command.full_name% --show-versions</info> |
41
|
|
|
EOT |
42
|
|
|
); |
43
|
|
|
|
44
|
11 |
|
parent::configure(); |
45
|
11 |
|
} |
46
|
|
|
|
47
|
9 |
|
public function execute(InputInterface $input, OutputInterface $output) : void |
48
|
|
|
{ |
49
|
9 |
|
$configuration = $this->getMigrationConfiguration($input, $output); |
50
|
|
|
|
51
|
9 |
|
$infos = new MigrationStatusInfosHelper($configuration); |
52
|
|
|
|
53
|
9 |
|
$output->writeln("\n <info>==</info> Configuration\n"); |
54
|
|
|
|
55
|
9 |
|
foreach ($infos->getMigrationsInfos() as $name => $value) { |
56
|
9 |
|
$string = (string) $value; |
57
|
|
|
|
58
|
9 |
|
if ($name === 'New Migrations') { |
59
|
9 |
|
$string = $value > 0 ? '<question>' . $value . '</question>' : '0'; |
60
|
|
|
} |
61
|
|
|
|
62
|
9 |
|
if ($name === 'Executed Unavailable Migrations') { |
63
|
9 |
|
$string = $value > 0 ? '<error>' . $value . '</error>' : '0'; |
64
|
|
|
} |
65
|
|
|
|
66
|
9 |
|
$this->writeStatusInfosLineAligned($output, $name, $string); |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
if (! $input->getOption('show-versions')) { |
70
|
8 |
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
$migrations = $configuration->getMigrations(); |
74
|
|
|
|
75
|
1 |
|
if ($migrations) { |
|
|
|
|
76
|
1 |
|
$output->writeln("\n <info>==</info> Available Migration Versions\n"); |
77
|
|
|
|
78
|
1 |
|
$this->showVersions($migrations, $configuration, $output); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
if (! count($infos->getExecutedUnavailableMigrations())) { |
82
|
1 |
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$output->writeln( |
86
|
|
|
"\n <info>==</info> Previously Executed Unavailable Migration Versions\n" |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
foreach ($infos->getExecutedUnavailableMigrations() as $executedUnavailableMigration) { |
90
|
|
|
$output->writeln( |
91
|
|
|
sprintf( |
92
|
|
|
' <comment>>></comment> %s (<comment>%s</comment>)', |
93
|
|
|
$configuration->getDateTime($executedUnavailableMigration), |
94
|
|
|
$executedUnavailableMigration |
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
9 |
|
private function writeStatusInfosLineAligned(OutputInterface $output, string $title, ?string $value) : void |
101
|
|
|
{ |
102
|
9 |
|
$output->writeln(sprintf( |
103
|
9 |
|
' <comment>>></comment> %s: %s%s', |
104
|
9 |
|
$title, |
105
|
9 |
|
str_repeat(' ', 50 - strlen($title)), |
106
|
9 |
|
$value |
107
|
|
|
)); |
108
|
9 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Version[] $migrations |
112
|
|
|
*/ |
113
|
1 |
|
private function showVersions(array $migrations, Configuration $configuration, OutputInterface $output) : void |
114
|
|
|
{ |
115
|
1 |
|
$migratedVersions = $configuration->getMigratedVersions(); |
116
|
|
|
|
117
|
1 |
|
foreach ($migrations as $version) { |
118
|
1 |
|
$isMigrated = in_array($version->getVersion(), $migratedVersions, true); |
119
|
1 |
|
$status = $isMigrated ? '<info>migrated</info>' : '<error>not migrated</error>'; |
120
|
|
|
|
121
|
1 |
|
$migrationDescription = $version->getMigration()->getDescription() |
122
|
|
|
? str_repeat(' ', 5) . $version->getMigration()->getDescription() |
123
|
1 |
|
: ''; |
124
|
|
|
|
125
|
1 |
|
$formattedVersion = $configuration->getDateTime($version->getVersion()); |
126
|
|
|
|
127
|
1 |
|
$output->writeln(sprintf( |
128
|
1 |
|
' <comment>>></comment> %s (<comment>%s</comment>)%s%s%s', |
129
|
1 |
|
$formattedVersion, |
130
|
1 |
|
$version->getVersion(), |
131
|
1 |
|
str_repeat(' ', max(1, 49 - strlen($formattedVersion) - strlen($version->getVersion()))), |
132
|
1 |
|
$status, |
133
|
1 |
|
$migrationDescription |
134
|
|
|
)); |
135
|
|
|
} |
136
|
1 |
|
} |
137
|
|
|
} |
138
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.