-
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # lib/Doctrine/Migrations/Tools/Console/Command/StatusCommand.php
- Loading branch information
Showing
9 changed files
with
244 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
lib/Doctrine/Migrations/Tools/Console/Command/ListCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Migrations\Tools\Console\Command; | ||
|
||
use DateTimeInterface; | ||
use Doctrine\Migrations\Metadata\AvailableMigration; | ||
use Doctrine\Migrations\Metadata\AvailableMigrationsList; | ||
use Doctrine\Migrations\Metadata\ExecutedMigration; | ||
use Doctrine\Migrations\Metadata\ExecutedMigrationsSet; | ||
use Doctrine\Migrations\Version\Version; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Helper\TableCell; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use function array_map; | ||
use function array_merge; | ||
use function array_unique; | ||
use function uasort; | ||
|
||
/** | ||
* The StatusCommand class is responsible for outputting a list of all available migrations and their status. | ||
*/ | ||
class ListCommand extends DoctrineCommand | ||
{ | ||
/** @var string */ | ||
protected static $defaultName = 'migrations:list'; | ||
|
||
protected function configure() : void | ||
{ | ||
$this | ||
->setAliases(['list-migrations']) | ||
->setDescription('Display a list of all available migrations and their status.') | ||
->setHelp(<<<EOT | ||
The <info>%command.name%</info> command outputs a list of all available migrations and their status: | ||
<info>%command.full_name%</info> | ||
EOT | ||
); | ||
|
||
parent::configure(); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output) : ?int | ||
{ | ||
$storage = $this->getDependencyFactory()->getMetadataStorage(); | ||
$migrationRepo = $this->getDependencyFactory()->getMigrationRepository(); | ||
|
||
$availableMigrations = $migrationRepo->getMigrations(); | ||
$executedMigrations = $storage->getExecutedMigrations(); | ||
|
||
$this->showVersions($availableMigrations, $executedMigrations, $output); | ||
|
||
return 0; | ||
} | ||
|
||
private function showVersions( | ||
AvailableMigrationsList $availableMigrationsSet, | ||
ExecutedMigrationsSet $executedMigrationsSet, | ||
OutputInterface $output | ||
) : void { | ||
$versions = $this->getSortedVersions($availableMigrationsSet, $executedMigrationsSet); | ||
|
||
$table = new Table($output); | ||
$table->setHeaders( | ||
[ | ||
[new TableCell('Migration Versions', ['colspan' => 4])], | ||
['Migration', 'Migrated', 'Migrated At', 'Execution Time', 'Description'], | ||
] | ||
); | ||
|
||
foreach ($versions as $version) { | ||
$description = null; | ||
$executedAt = null; | ||
$executionTime = null; | ||
|
||
if ($executedMigrationsSet->hasMigration($version)) { | ||
$executedMigration = $executedMigrationsSet->getMigration($version); | ||
$executionTime = $executedMigration->getExecutionTime(); | ||
$executedAt = $executedMigration->getExecutedAt() instanceof DateTimeInterface | ||
? $executedMigration->getExecutedAt()->format('Y-m-d H:i:s') | ||
: null; | ||
} | ||
|
||
if ($availableMigrationsSet->hasMigration($version)) { | ||
$description = $availableMigrationsSet->getMigration($version)->getMigration()->getDescription(); | ||
} | ||
|
||
if ($executedMigrationsSet->hasMigration($version) && $availableMigrationsSet->hasMigration($version)) { | ||
$status = '<info>migrated</info>'; | ||
} elseif ($executedMigrationsSet->hasMigration($version)) { | ||
$status = '<error>migrated, not available</error>'; | ||
} else { | ||
$status = '<comment>not migrated</comment>'; | ||
} | ||
|
||
$table->addRow([ | ||
(string) $version, | ||
$status, | ||
(string) $executedAt, | ||
$executionTime !== null ? $executionTime . 's': '', | ||
$description, | ||
]); | ||
} | ||
|
||
$table->render(); | ||
} | ||
|
||
/** | ||
* @return Version[] | ||
*/ | ||
private function getSortedVersions(AvailableMigrationsList $availableMigrationsSet, ExecutedMigrationsSet $executedMigrationsSet) : array | ||
{ | ||
$availableVersions = array_map(static function (AvailableMigration $availableMigration) : Version { | ||
return $availableMigration->getVersion(); | ||
}, $availableMigrationsSet->getItems()); | ||
|
||
$executedVersions = array_map(static function (ExecutedMigration $executedMigration) : Version { | ||
return $executedMigration->getVersion(); | ||
}, $executedMigrationsSet->getItems()); | ||
|
||
$versions = array_unique(array_merge($availableVersions, $executedVersions)); | ||
|
||
$comparator = $this->getDependencyFactory()->getVersionComparator(); | ||
uasort($versions, static function (Version $a, Version $b) use ($comparator) : int { | ||
return $comparator->compare($a, $b); | ||
}); | ||
|
||
return $versions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
tests/Doctrine/Migrations/Tests/Tools/Console/Command/ListCommandTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Migrations\Tests\Tools\Console\Command; | ||
|
||
use DateTimeImmutable; | ||
use Doctrine\Migrations\AbstractMigration; | ||
use Doctrine\Migrations\Configuration\Configuration; | ||
use Doctrine\Migrations\Configuration\Connection\ExistingConnection; | ||
use Doctrine\Migrations\DependencyFactory; | ||
use Doctrine\Migrations\Metadata\Storage\MetadataStorage; | ||
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration; | ||
use Doctrine\Migrations\MigrationRepository; | ||
use Doctrine\Migrations\Tests\MigrationTestCase; | ||
use Doctrine\Migrations\Tools\Console\Command\ListCommand; | ||
use Doctrine\Migrations\Version\Direction; | ||
use Doctrine\Migrations\Version\ExecutionResult; | ||
use Doctrine\Migrations\Version\Version; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use function array_map; | ||
use function explode; | ||
use function sys_get_temp_dir; | ||
use function trim; | ||
|
||
class ListCommandTest extends MigrationTestCase | ||
{ | ||
/** @var ListCommand */ | ||
private $command; | ||
|
||
/** @var MigrationRepository */ | ||
private $migrationRepository; | ||
|
||
/** @var MetadataStorage */ | ||
private $metadataStorage; | ||
|
||
/** @var CommandTester */ | ||
private $commandTester; | ||
|
||
protected function setUp() : void | ||
{ | ||
$configuration = new Configuration(); | ||
$configuration->setMetadataStorageConfiguration(new TableMetadataStorageConfiguration()); | ||
$configuration->addMigrationsDirectory('DoctrineMigrations', sys_get_temp_dir()); | ||
|
||
$conn = $this->getSqliteConnection(); | ||
|
||
$dependencyFactory = DependencyFactory::fromConnection( | ||
new Configuration\ExistingConfiguration($configuration), | ||
new ExistingConnection($conn) | ||
); | ||
|
||
$this->migrationRepository = $dependencyFactory->getMigrationRepository(); | ||
$this->metadataStorage = $dependencyFactory->getMetadataStorage(); | ||
|
||
$this->metadataStorage->ensureInitialized(); | ||
|
||
$this->command = new ListCommand($dependencyFactory); | ||
$this->commandTester = new CommandTester($this->command); | ||
} | ||
|
||
public function testExecute() : void | ||
{ | ||
$migrationClass = $this->createMock(AbstractMigration::class); | ||
$migrationClass | ||
->expects(self::atLeastOnce()) | ||
->method('getDescription') | ||
->willReturn('foo'); | ||
|
||
$this->migrationRepository->registerMigrationInstance(new Version('1231'), $migrationClass); | ||
$this->migrationRepository->registerMigrationInstance(new Version('1230'), $migrationClass); | ||
|
||
$result = new ExecutionResult(new Version('1230'), Direction::UP, new DateTimeImmutable('2010-01-01 02:03:04')); | ||
$result->setTime(10.0); | ||
$this->metadataStorage->complete($result); | ||
|
||
$result = new ExecutionResult(new Version('1229'), Direction::UP, new DateTimeImmutable('2010-01-01 02:03:04')); | ||
$this->metadataStorage->complete($result); | ||
|
||
$this->commandTester->execute([]); | ||
|
||
$lines = array_map('trim', explode("\n", trim($this->commandTester->getDisplay(true)))); | ||
|
||
self::assertSame( | ||
[ | ||
0 => '+-----------+-------------------------+---------------------+----------------+-------------+', | ||
1 => '| Migration Versions | |', | ||
2 => '+-----------+-------------------------+---------------------+----------------+-------------+', | ||
3 => '| Migration | Migrated | Migrated At | Execution Time | Description |', | ||
4 => '+-----------+-------------------------+---------------------+----------------+-------------+', | ||
5 => '| 1229 | migrated, not available | 2010-01-01 02:03:04 | | |', | ||
6 => '| 1230 | migrated | 2010-01-01 02:03:04 | 10s | foo |', | ||
7 => '| 1231 | not migrated | | | foo |', | ||
8 => '+-----------+-------------------------+---------------------+----------------+-------------+', | ||
], | ||
$lines | ||
); | ||
} | ||
} |
Oops, something went wrong.