|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Command; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeInterface; |
|
8
|
|
|
use Doctrine\Migrations\Metadata\AvailableMigration; |
|
9
|
|
|
use Doctrine\Migrations\Metadata\AvailableMigrationsList; |
|
10
|
|
|
use Doctrine\Migrations\Metadata\ExecutedMigration; |
|
11
|
|
|
use Doctrine\Migrations\Metadata\ExecutedMigrationsSet; |
|
12
|
|
|
use Doctrine\Migrations\Version\Version; |
|
13
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
14
|
|
|
use Symfony\Component\Console\Helper\TableCell; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
17
|
|
|
use function array_map; |
|
18
|
|
|
use function array_merge; |
|
19
|
|
|
use function array_unique; |
|
20
|
|
|
use function uasort; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The ListCommand class is responsible for outputting a list of all available migrations and their status. |
|
24
|
|
|
*/ |
|
25
|
|
|
final class ListCommand extends DoctrineCommand |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
protected static $defaultName = 'migrations:list'; |
|
29
|
|
|
|
|
30
|
1 |
|
protected function configure() : void |
|
31
|
|
|
{ |
|
32
|
|
|
$this |
|
33
|
1 |
|
->setAliases(['list-migrations']) |
|
34
|
1 |
|
->setDescription('Display a list of all available migrations and their status.') |
|
35
|
1 |
|
->setHelp(<<<EOT |
|
36
|
1 |
|
The <info>%command.name%</info> command outputs a list of all available migrations and their status: |
|
37
|
|
|
|
|
38
|
|
|
<info>%command.full_name%</info> |
|
39
|
|
|
EOT |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
1 |
|
parent::configure(); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function execute(InputInterface $input, OutputInterface $output) : int |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->showVersions( |
|
48
|
1 |
|
$this->getDependencyFactory()->getMigrationRepository()->getMigrations(), // available migrations |
|
49
|
1 |
|
$this->getDependencyFactory()->getMetadataStorage()->getExecutedMigrations(), // executed migrations |
|
50
|
1 |
|
$output |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
1 |
|
return 0; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
private function showVersions( |
|
57
|
|
|
AvailableMigrationsList $availableMigrations, |
|
58
|
|
|
ExecutedMigrationsSet $executedMigrations, |
|
59
|
|
|
OutputInterface $output |
|
60
|
|
|
) : void { |
|
61
|
1 |
|
$table = new Table($output); |
|
62
|
1 |
|
$table->setHeaders( |
|
63
|
|
|
[ |
|
64
|
1 |
|
[new TableCell('Migration Versions', ['colspan' => 4])], |
|
65
|
|
|
['Migration', 'Status', 'Migrated At', 'Execution Time', 'Description'], |
|
66
|
|
|
] |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
1 |
|
foreach ($this->getSortedVersions($availableMigrations, $executedMigrations) as $version) { |
|
70
|
1 |
|
$description = null; |
|
71
|
1 |
|
$executedAt = null; |
|
72
|
1 |
|
$executionTime = null; |
|
73
|
|
|
|
|
74
|
1 |
|
if ($executedMigrations->hasMigration($version)) { |
|
75
|
1 |
|
$executedMigration = $executedMigrations->getMigration($version); |
|
76
|
1 |
|
$executionTime = $executedMigration->getExecutionTime(); |
|
77
|
1 |
|
$executedAt = $executedMigration->getExecutedAt() instanceof DateTimeInterface |
|
78
|
1 |
|
? $executedMigration->getExecutedAt()->format('Y-m-d H:i:s') |
|
79
|
1 |
|
: null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
if ($availableMigrations->hasMigration($version)) { |
|
83
|
1 |
|
$description = $availableMigrations->getMigration($version)->getMigration()->getDescription(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
if ($executedMigrations->hasMigration($version) && $availableMigrations->hasMigration($version)) { |
|
87
|
1 |
|
$status = '<info>migrated</info>'; |
|
88
|
1 |
|
} elseif ($executedMigrations->hasMigration($version)) { |
|
89
|
1 |
|
$status = '<error>migrated, not available</error>'; |
|
90
|
|
|
} else { |
|
91
|
1 |
|
$status = '<comment>not migrated</comment>'; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
$table->addRow([ |
|
95
|
1 |
|
(string) $version, |
|
96
|
1 |
|
$status, |
|
97
|
1 |
|
(string) $executedAt, |
|
98
|
1 |
|
$executionTime !== null ? $executionTime . 's': '', |
|
99
|
1 |
|
$description, |
|
100
|
|
|
]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
$table->render(); |
|
104
|
1 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return Version[] |
|
108
|
|
|
*/ |
|
109
|
1 |
|
private function getSortedVersions(AvailableMigrationsList $availableMigrations, ExecutedMigrationsSet $executedMigrations) : array |
|
110
|
|
|
{ |
|
111
|
|
|
$availableVersions = array_map(static function (AvailableMigration $availableMigration) : Version { |
|
112
|
1 |
|
return $availableMigration->getVersion(); |
|
113
|
1 |
|
}, $availableMigrations->getItems()); |
|
114
|
|
|
|
|
115
|
|
|
$executedVersions = array_map(static function (ExecutedMigration $executedMigration) : Version { |
|
116
|
1 |
|
return $executedMigration->getVersion(); |
|
117
|
1 |
|
}, $executedMigrations->getItems()); |
|
118
|
|
|
|
|
119
|
1 |
|
$versions = array_unique(array_merge($availableVersions, $executedVersions)); |
|
120
|
|
|
|
|
121
|
1 |
|
$comparator = $this->getDependencyFactory()->getVersionComparator(); |
|
122
|
|
|
uasort($versions, static function (Version $a, Version $b) use ($comparator) : int { |
|
123
|
1 |
|
return $comparator->compare($a, $b); |
|
124
|
1 |
|
}); |
|
125
|
|
|
|
|
126
|
1 |
|
return $versions; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|