|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeInterface; |
|
8
|
|
|
use Doctrine\DBAL\Connection; |
|
9
|
|
|
use Doctrine\Migrations\Configuration\Configuration; |
|
10
|
|
|
use Doctrine\Migrations\Metadata\ExecutedMigrationsList; |
|
11
|
|
|
use Doctrine\Migrations\Metadata\Storage\MetadataStorage; |
|
12
|
|
|
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration; |
|
13
|
|
|
use Doctrine\Migrations\Version\AliasResolver; |
|
14
|
|
|
use Doctrine\Migrations\Version\MigrationPlanCalculator; |
|
15
|
|
|
use Doctrine\Migrations\Version\MigrationStatusCalculator; |
|
16
|
|
|
use Doctrine\Migrations\Version\Version; |
|
17
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
18
|
|
|
use Symfony\Component\Console\Helper\TableCell; |
|
19
|
|
|
use Symfony\Component\Console\Helper\TableSeparator; |
|
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
21
|
|
|
use Throwable; |
|
22
|
|
|
use function array_unshift; |
|
23
|
|
|
use function count; |
|
24
|
|
|
use function get_class; |
|
25
|
|
|
use function sprintf; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The MigrationStatusInfosHelper class is responsible for building the array of information used when displaying |
|
29
|
|
|
* the status of your migrations. |
|
30
|
|
|
* |
|
31
|
|
|
* @internal |
|
32
|
|
|
* |
|
33
|
|
|
* @see Doctrine\Migrations\Tools\Console\Command\StatusCommand |
|
34
|
|
|
*/ |
|
35
|
|
|
class MigrationStatusInfosHelper |
|
36
|
|
|
{ |
|
37
|
|
|
/** @var Configuration */ |
|
38
|
|
|
private $configuration; |
|
39
|
|
|
|
|
40
|
|
|
/** @var Connection */ |
|
41
|
|
|
private $connection; |
|
42
|
|
|
|
|
43
|
|
|
/** @var AliasResolver */ |
|
44
|
|
|
private $aliasResolver; |
|
45
|
|
|
|
|
46
|
|
|
/** @var MetadataStorage */ |
|
47
|
|
|
private $metadataStorage; |
|
48
|
|
|
|
|
49
|
|
|
/** @var MigrationPlanCalculator */ |
|
50
|
|
|
private $migrationPlanCalculator; |
|
51
|
|
|
|
|
52
|
|
|
/** @var MigrationStatusCalculator */ |
|
53
|
|
|
private $statusCalculator; |
|
54
|
|
|
|
|
55
|
3 |
|
public function __construct( |
|
56
|
|
|
Configuration $configuration, |
|
57
|
|
|
Connection $connection, |
|
58
|
|
|
AliasResolver $aliasResolver, |
|
59
|
|
|
MigrationPlanCalculator $migrationPlanCalculator, |
|
60
|
|
|
MigrationStatusCalculator $statusCalculator, |
|
61
|
|
|
MetadataStorage $metadataStorage |
|
62
|
|
|
) { |
|
63
|
3 |
|
$this->configuration = $configuration; |
|
64
|
3 |
|
$this->connection = $connection; |
|
65
|
3 |
|
$this->aliasResolver = $aliasResolver; |
|
66
|
3 |
|
$this->migrationPlanCalculator = $migrationPlanCalculator; |
|
67
|
3 |
|
$this->metadataStorage = $metadataStorage; |
|
68
|
3 |
|
$this->statusCalculator = $statusCalculator; |
|
69
|
3 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param Version[] $versions |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function listVersions(array $versions, OutputInterface $output) : void |
|
75
|
|
|
{ |
|
76
|
2 |
|
$table = new Table($output); |
|
77
|
2 |
|
$table->setHeaders( |
|
78
|
|
|
[ |
|
79
|
2 |
|
[new TableCell('Migration Versions', ['colspan' => 4])], |
|
80
|
|
|
['Migration', 'Status', 'Migrated At', 'Execution Time', 'Description'], |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
2 |
|
$executedMigrations = $this->metadataStorage->getExecutedMigrations(); |
|
84
|
2 |
|
$availableMigrations = $this->migrationPlanCalculator->getMigrations(); |
|
85
|
|
|
|
|
86
|
2 |
|
foreach ($versions as $version) { |
|
87
|
2 |
|
$description = null; |
|
88
|
2 |
|
$executedAt = null; |
|
89
|
2 |
|
$executionTime = null; |
|
90
|
|
|
|
|
91
|
2 |
|
if ($executedMigrations->hasMigration($version)) { |
|
92
|
2 |
|
$executedMigration = $executedMigrations->getMigration($version); |
|
93
|
2 |
|
$executionTime = $executedMigration->getExecutionTime(); |
|
94
|
2 |
|
$executedAt = $executedMigration->getExecutedAt() instanceof DateTimeInterface |
|
95
|
2 |
|
? $executedMigration->getExecutedAt()->format('Y-m-d H:i:s') |
|
96
|
2 |
|
: null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
if ($availableMigrations->hasMigration($version)) { |
|
100
|
2 |
|
$description = $availableMigrations->getMigration($version)->getMigration()->getDescription(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
if ($executedMigrations->hasMigration($version) && $availableMigrations->hasMigration($version)) { |
|
104
|
1 |
|
$status = '<info>migrated</info>'; |
|
105
|
2 |
|
} elseif ($executedMigrations->hasMigration($version)) { |
|
106
|
2 |
|
$status = '<error>migrated, not available</error>'; |
|
107
|
|
|
} else { |
|
108
|
2 |
|
$status = '<comment>not migrated</comment>'; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
$table->addRow([ |
|
112
|
2 |
|
(string) $version, |
|
113
|
2 |
|
$status, |
|
114
|
2 |
|
(string) $executedAt, |
|
115
|
2 |
|
$executionTime !== null ? $executionTime . 's': '', |
|
116
|
2 |
|
$description, |
|
117
|
|
|
]); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
2 |
|
$table->render(); |
|
121
|
2 |
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
public function showMigrationsInfo(OutputInterface $output) : void |
|
124
|
|
|
{ |
|
125
|
1 |
|
$executedMigrations = $this->metadataStorage->getExecutedMigrations(); |
|
126
|
1 |
|
$availableMigrations = $this->migrationPlanCalculator->getMigrations(); |
|
127
|
|
|
|
|
128
|
1 |
|
$newMigrations = $this->statusCalculator->getNewMigrations(); |
|
129
|
1 |
|
$executedUnavailableMigrations = $this->statusCalculator->getExecutedUnavailableMigrations(); |
|
130
|
|
|
|
|
131
|
1 |
|
$storage = $this->configuration->getMetadataStorageConfiguration(); |
|
132
|
|
|
|
|
133
|
1 |
|
$table = new Table($output); |
|
134
|
1 |
|
$table->setHeaders( |
|
135
|
|
|
[ |
|
136
|
1 |
|
[new TableCell('Configuration', ['colspan' => 3])], |
|
137
|
|
|
] |
|
138
|
|
|
); |
|
139
|
|
|
|
|
140
|
|
|
$dataGroup = [ |
|
141
|
1 |
|
'Storage' => [ |
|
142
|
1 |
|
'Type' => $storage!== null ? get_class($storage) : null, |
|
143
|
|
|
], |
|
144
|
|
|
'Database' => [ |
|
145
|
1 |
|
'Driver' => get_class($this->connection->getDriver()), |
|
146
|
1 |
|
'Name' => $this->connection->getDatabase(), |
|
147
|
|
|
], |
|
148
|
|
|
'Versions' => [ |
|
149
|
1 |
|
'Previous' => $this->getFormattedVersionAlias('prev', $executedMigrations), |
|
150
|
1 |
|
'Current' => $this->getFormattedVersionAlias('current', $executedMigrations), |
|
151
|
1 |
|
'Next' => $this->getFormattedVersionAlias('next', $executedMigrations), |
|
152
|
1 |
|
'Latest' => $this->getFormattedVersionAlias('latest', $executedMigrations), |
|
153
|
|
|
], |
|
154
|
|
|
|
|
155
|
|
|
'Migrations' => [ |
|
156
|
1 |
|
'Executed' => count($executedMigrations), |
|
157
|
1 |
|
'Executed Unavailable' => count($executedUnavailableMigrations) > 0 ? ('<error>' . count($executedUnavailableMigrations) . '</error>') : '0', |
|
158
|
1 |
|
'Available' => count($availableMigrations), |
|
159
|
1 |
|
'New' => count($newMigrations) > 0 ? ('<question>' . count($newMigrations) . '</question>') : '0', |
|
160
|
|
|
], |
|
161
|
1 |
|
'Migration Namespaces' => $this->configuration->getMigrationDirectories(), |
|
162
|
|
|
|
|
163
|
|
|
]; |
|
164
|
1 |
|
if ($storage instanceof TableMetadataStorageConfiguration) { |
|
165
|
|
|
$dataGroup['Storage'] += [ |
|
166
|
1 |
|
'Table Name' => $storage->getTableName(), |
|
167
|
1 |
|
'Column Name' => $storage->getVersionColumnName(), |
|
168
|
|
|
]; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
1 |
|
$first = true; |
|
172
|
1 |
|
foreach ($dataGroup as $group => $dataValues) { |
|
173
|
1 |
|
$nsRows = []; |
|
174
|
1 |
|
foreach ($dataValues as $k => $v) { |
|
175
|
1 |
|
$nsRows[] = [ |
|
176
|
1 |
|
$k, |
|
177
|
1 |
|
$v, |
|
178
|
|
|
]; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
1 |
|
if (count($nsRows) <= 0) { |
|
182
|
|
|
continue; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
1 |
|
if (! $first) { |
|
186
|
1 |
|
$table->addRow([new TableSeparator(['colspan' => 3])]); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
1 |
|
$first = false; |
|
190
|
1 |
|
array_unshift( |
|
191
|
1 |
|
$nsRows[0], |
|
192
|
1 |
|
new TableCell('<info>' . $group . '</info>', ['rowspan' => count($dataValues)]) |
|
193
|
|
|
); |
|
194
|
1 |
|
$table->addRows($nsRows); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
1 |
|
$table->render(); |
|
198
|
1 |
|
} |
|
199
|
|
|
|
|
200
|
1 |
|
private function getFormattedVersionAlias(string $alias, ExecutedMigrationsList $executedMigrations) : string |
|
|
|
|
|
|
201
|
|
|
{ |
|
202
|
|
|
try { |
|
203
|
1 |
|
$version = $this->aliasResolver->resolveVersionAlias($alias); |
|
204
|
1 |
|
} catch (Throwable $e) { |
|
205
|
1 |
|
$version = null; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
// No version found |
|
209
|
1 |
|
if ($version === null) { |
|
210
|
1 |
|
if ($alias === 'next') { |
|
211
|
1 |
|
return 'Already at latest version'; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
if ($alias === 'prev') { |
|
215
|
|
|
return 'Already at first version'; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
// Before first version "virtual" version number |
|
220
|
1 |
|
if ((string) $version === '0') { |
|
221
|
|
|
return '<comment>0</comment>'; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
// Show normal version number |
|
225
|
1 |
|
return sprintf( |
|
226
|
1 |
|
'<comment>%s </comment>', |
|
227
|
1 |
|
(string) $version |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.