|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2014 Matthew Fitzgerald |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace AntiMattr\MongoDB\Migrations\Tools\Console\Command; |
|
13
|
|
|
|
|
14
|
|
|
use AntiMattr\MongoDB\Migrations\Configuration\Configuration; |
|
15
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Matthew Fitzgerald <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class StatusCommand extends AbstractCommand |
|
24
|
|
|
{ |
|
25
|
|
|
const NAME = 'mongodb:migrations:status'; |
|
26
|
|
|
|
|
27
|
2 |
|
protected function configure() |
|
28
|
|
|
{ |
|
29
|
|
|
$this |
|
30
|
2 |
|
->setName($this->getName()) |
|
31
|
2 |
|
->setDescription('View the status of a set of migrations.') |
|
32
|
2 |
|
->addOption( |
|
33
|
2 |
|
'show-versions', |
|
34
|
2 |
|
null, |
|
35
|
2 |
|
InputOption::VALUE_NONE, |
|
36
|
2 |
|
'This will display a list of all available migrations and their status' |
|
37
|
|
|
) |
|
38
|
2 |
|
->setHelp(<<<'EOT' |
|
39
|
2 |
|
The <info>%command.name%</info> command outputs the status of a set of migrations: |
|
40
|
|
|
|
|
41
|
|
|
<info>%command.full_name%</info> |
|
42
|
|
|
|
|
43
|
|
|
You can output a list of all available migrations and their status with <comment>--show-versions</comment>: |
|
44
|
|
|
|
|
45
|
|
|
<info>%command.full_name% --show-versions</info> |
|
46
|
|
|
EOT |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
2 |
|
parent::configure(); |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface |
|
54
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function execute(InputInterface $input, OutputInterface $output) |
|
57
|
|
|
{ |
|
58
|
2 |
|
$configuration = $this->getMigrationConfiguration($input, $output); |
|
59
|
2 |
|
$configMap = $configuration->getDetailsMap(); |
|
60
|
|
|
|
|
61
|
|
|
// Format current version string |
|
62
|
2 |
|
$currentVersion = $configMap['current_version']; |
|
63
|
2 |
|
if ($currentVersion) { |
|
64
|
2 |
|
$currentVersionFormatted = sprintf( |
|
65
|
2 |
|
'%s (<comment>%s</comment>)', |
|
66
|
2 |
|
Configuration::formatVersion($currentVersion), |
|
67
|
2 |
|
$currentVersion |
|
68
|
|
|
); |
|
69
|
|
|
} else { |
|
70
|
|
|
$currentVersionFormatted = 0; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Format latest version string |
|
74
|
2 |
|
$latestVersion = $configMap['latest_version']; |
|
75
|
2 |
|
if ($latestVersion) { |
|
76
|
2 |
|
$latestVersionFormatted = sprintf( |
|
77
|
2 |
|
'%s (<comment>%s</comment>)', |
|
78
|
2 |
|
Configuration::formatVersion($latestVersion), |
|
79
|
2 |
|
$latestVersion |
|
80
|
|
|
); |
|
81
|
|
|
} else { |
|
82
|
|
|
$latestVersionFormatted = 0; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
$output->writeln("\n <info>==</info> Configuration\n"); |
|
86
|
|
|
|
|
87
|
2 |
|
$numExecutedUnavailableMigrations = $configMap['num_executed_unavailable_migrations']; |
|
88
|
2 |
|
$numNewMigrations = $configMap['num_new_migrations']; |
|
89
|
|
|
|
|
90
|
|
|
$info = [ |
|
91
|
2 |
|
'Name' => $configMap['name'], |
|
92
|
2 |
|
'Database Driver' => $configMap['database_driver'], |
|
93
|
2 |
|
'Database Name' => $configMap['migrations_database_name'], |
|
94
|
2 |
|
'Configuration Source' => $configuration->getFile() ?: 'manually configured', |
|
95
|
2 |
|
'Version Collection Name' => $configMap['migrations_collection_name'], |
|
96
|
2 |
|
'Migrations Namespace' => $configMap['migrations_namespace'], |
|
97
|
2 |
|
'Migrations Directory' => $configMap['migrations_directory'], |
|
98
|
2 |
|
'Current Version' => $currentVersionFormatted, |
|
99
|
2 |
|
'Latest Version' => $latestVersionFormatted, |
|
100
|
2 |
|
'Executed Migrations' => $configMap['num_executed_migrations'], |
|
101
|
2 |
|
'Executed Unavailable Migrations' => $numExecutedUnavailableMigrations > 0 ? '<error>' . $numExecutedUnavailableMigrations . '</error>' : 0, |
|
102
|
2 |
|
'Available Migrations' => $configMap['num_available_migrations'], |
|
103
|
2 |
|
'New Migrations' => $numNewMigrations > 0 ? '<question>' . $numNewMigrations . '</question>' : 0, |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
2 |
|
foreach ($info as $name => $value) { |
|
107
|
2 |
|
$this->writeInfoLine($output, $name, $value); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
2 |
|
if ($input->getOption('show-versions')) { |
|
111
|
1 |
|
if ($migrations = $configuration->getMigrations()) { |
|
112
|
1 |
|
$output->writeln("\n <info>==</info> Available Migration Versions\n"); |
|
113
|
1 |
|
$rows = []; |
|
114
|
1 |
|
$migratedVersions = $configuration->getMigratedVersions(); |
|
115
|
|
|
|
|
116
|
1 |
|
foreach ($migrations as $version) { |
|
117
|
1 |
|
$isMigrated = in_array($version->getVersion(), $migratedVersions); |
|
118
|
1 |
|
$status = '<error>not migrated</error>'; |
|
119
|
|
|
|
|
120
|
1 |
|
if ($isMigrated) { |
|
121
|
1 |
|
$ts = $configuration->getMigratedTimestamp( |
|
122
|
1 |
|
$version->getVersion() |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
1 |
|
$status = sprintf( |
|
126
|
1 |
|
'<info>%s</info>', |
|
127
|
1 |
|
date('Y-m-d H:i', $ts) |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
$versionTxt = sprintf('<comment>%s</comment>', $version->getVersion()); |
|
132
|
1 |
|
$desc = $version->getMigration()->getDescription(); |
|
133
|
1 |
|
if (strlen($desc) > 80) { |
|
134
|
|
|
$desc = substr($desc, 0, 78) . '...'; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
$rows[] = [$versionTxt, $status, $desc]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
$table = new Table($output); |
|
141
|
1 |
|
$table->setHeaders(['Version', 'Date Migrated', 'Description']) |
|
142
|
1 |
|
->setRows($rows) |
|
143
|
1 |
|
->render(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
1 |
|
$executedUnavailableMigrations = $configuration->getUnavailableMigratedVersions(); |
|
147
|
1 |
|
if (!empty($executedUnavailableMigrations)) { |
|
148
|
1 |
|
$output->writeln("\n <info>==</info> Previously Executed Unavailable Migration Versions\n"); |
|
149
|
1 |
|
foreach ($executedUnavailableMigrations as $executedUnavailableMigration) { |
|
150
|
1 |
|
$output->writeln( |
|
151
|
1 |
|
sprintf( |
|
152
|
1 |
|
' <comment>>></comment> %s (<comment>%s</comment>)', |
|
153
|
1 |
|
Configuration::formatVersion($executedUnavailableMigration), |
|
154
|
1 |
|
$executedUnavailableMigration |
|
155
|
|
|
) |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
2 |
|
return 0; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string $name |
|
166
|
|
|
* @param string $value |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function writeInfoLine(OutputInterface $output, $name, $value) |
|
169
|
|
|
{ |
|
170
|
|
|
$whitespace = str_repeat(' ', 35 - strlen($name)); |
|
171
|
|
|
$output->writeln( |
|
172
|
|
|
sprintf( |
|
173
|
|
|
' <comment>>></comment> %s: %s%s', |
|
174
|
|
|
$name, |
|
175
|
|
|
$whitespace, |
|
176
|
|
|
$value |
|
177
|
|
|
) |
|
178
|
|
|
); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* getName. |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
2 |
|
public function getName() |
|
187
|
|
|
{ |
|
188
|
2 |
|
return self::NAME; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|