-
-
Notifications
You must be signed in to change notification settings - Fork 395
Add List migrations command #908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
128 changes: 128 additions & 0 deletions
128
lib/Doctrine/Migrations/Tools/Console/Command/ListCommand.php
This file contains hidden or 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,128 @@ | ||
| <?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 ListCommand class is responsible for outputting a list of all available migrations and their status. | ||
| */ | ||
| final 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 | ||
| { | ||
| $this->showVersions( | ||
| $this->getDependencyFactory()->getMigrationRepository()->getMigrations(), // available migrations | ||
| $this->getDependencyFactory()->getMetadataStorage()->getExecutedMigrations(), // executed migrations | ||
| $output | ||
| ); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| private function showVersions( | ||
| AvailableMigrationsList $availableMigrations, | ||
| ExecutedMigrationsSet $executedMigrations, | ||
| OutputInterface $output | ||
| ) : void { | ||
| $table = new Table($output); | ||
| $table->setHeaders( | ||
| [ | ||
| [new TableCell('Migration Versions', ['colspan' => 4])], | ||
| ['Migration', 'Status', 'Migrated At', 'Execution Time', 'Description'], | ||
| ] | ||
| ); | ||
|
|
||
| foreach ($this->getSortedVersions($availableMigrations, $executedMigrations) as $version) { | ||
| $description = null; | ||
| $executedAt = null; | ||
| $executionTime = null; | ||
|
|
||
| if ($executedMigrations->hasMigration($version)) { | ||
| $executedMigration = $executedMigrations->getMigration($version); | ||
| $executionTime = $executedMigration->getExecutionTime(); | ||
| $executedAt = $executedMigration->getExecutedAt() instanceof DateTimeInterface | ||
| ? $executedMigration->getExecutedAt()->format('Y-m-d H:i:s') | ||
| : null; | ||
| } | ||
|
|
||
| if ($availableMigrations->hasMigration($version)) { | ||
| $description = $availableMigrations->getMigration($version)->getMigration()->getDescription(); | ||
| } | ||
|
|
||
| if ($executedMigrations->hasMigration($version) && $availableMigrations->hasMigration($version)) { | ||
| $status = '<info>migrated</info>'; | ||
| } elseif ($executedMigrations->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 $availableMigrations, ExecutedMigrationsSet $executedMigrations) : array | ||
| { | ||
| $availableVersions = array_map(static function (AvailableMigration $availableMigration) : Version { | ||
| return $availableMigration->getVersion(); | ||
| }, $availableMigrations->getItems()); | ||
|
|
||
| $executedVersions = array_map(static function (ExecutedMigration $executedMigration) : Version { | ||
| return $executedMigration->getVersion(); | ||
| }, $executedMigrations->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 hidden or 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 hidden or 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 hidden or 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 | Status | 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.