Skip to content
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

Moved migration sorting from MigrationRepository to MigrationPlanCalculator #969

Merged
merged 2 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/Doctrine/Migrations/DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ public function getMigrationRepository() : MigrationsRepository
$this->getConfiguration()->getMigrationClasses(),
$this->getConfiguration()->getMigrationDirectories(),
$this->getMigrationsFinder(),
$this->getMigrationFactory(),
$this->getVersionComparator()
$this->getMigrationFactory()
);
});
}
Expand Down Expand Up @@ -362,7 +361,7 @@ public function getVersionAliasResolver() : AliasResolver
{
return $this->getDependency(AliasResolver::class, function () : AliasResolver {
return new DefaultAliasResolver(
$this->getMigrationRepository(),
$this->getMigrationPlanCalculator(),
$this->getMetadataStorage(),
$this->getMigrationStatusCalculator()
);
Expand All @@ -373,7 +372,7 @@ public function getMigrationStatusCalculator() : MigrationStatusCalculator
{
return $this->getDependency(MigrationStatusCalculator::class, function () : MigrationStatusCalculator {
return new CurrentMigrationStatusCalculator(
$this->getMigrationRepository(),
$this->getMigrationPlanCalculator(),
$this->getMetadataStorage()
);
});
Expand All @@ -384,7 +383,8 @@ public function getMigrationPlanCalculator() : MigrationPlanCalculator
return $this->getDependency(MigrationPlanCalculator::class, function () : MigrationPlanCalculator {
return new SortedMigrationPlanCalculator(
$this->getMigrationRepository(),
$this->getMetadataStorage()
$this->getMetadataStorage(),
$this->getVersionComparator()
);
});
}
Expand Down Expand Up @@ -422,7 +422,7 @@ public function getMigrationStatusInfosHelper() : MigrationStatusInfosHelper
$this->getConfiguration(),
$this->getConnection(),
$this->getVersionAliasResolver(),
$this->getMigrationRepository(),
$this->getMigrationPlanCalculator(),
$this->getMigrationStatusCalculator(),
$this->getMetadataStorage()
);
Expand Down
26 changes: 9 additions & 17 deletions lib/Doctrine/Migrations/FilesystemMigrationsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
use Doctrine\Migrations\Exception\MigrationException;
use Doctrine\Migrations\Finder\MigrationFinder;
use Doctrine\Migrations\Metadata\AvailableMigration;
use Doctrine\Migrations\Metadata\AvailableMigrationsList;
use Doctrine\Migrations\Version\Comparator;
use Doctrine\Migrations\Metadata\AvailableMigrationsSet;
use Doctrine\Migrations\Version\MigrationFactory;
use Doctrine\Migrations\Version\Version;
use function class_exists;
use function uasort;

/**
* The MigrationRepository class is responsible for retrieving migrations, determining what the current migration
* The FilesystemMigrationsRepository class is responsible for retrieving migrations, determining what the current migration
* version, etc.
*
* @internal
Expand All @@ -39,24 +37,19 @@ class FilesystemMigrationsRepository implements MigrationsRepository
/** @var AvailableMigration[] */
private $migrations = [];

/** @var Comparator */
private $sorter;

/**
* @param array<string, string> $migrationDirectories
* @param string[] $classes
* @param array<string, string> $migrationDirectories
*/
public function __construct(
array $classes,
array $migrationDirectories,
MigrationFinder $migrationFinder,
MigrationFactory $versionFactory,
Comparator $sorter
MigrationFactory $versionFactory
) {
$this->migrationDirectories = $migrationDirectories;
$this->migrationFinder = $migrationFinder;
$this->versionFactory = $versionFactory;
$this->sorter = $sorter;

$this->registerMigrations($classes);
}
Expand Down Expand Up @@ -120,11 +113,14 @@ public function getMigration(Version $version) : AvailableMigration
return $this->migrations[(string) $version];
}

public function getMigrations() : AvailableMigrationsList
/**
* Returns a non-sorted set of migrations.
*/
public function getMigrations() : AvailableMigrationsSet
{
$this->loadMigrationsFromDirectories();

return new AvailableMigrationsList($this->migrations);
return new AvailableMigrationsSet($this->migrations);
}

/** @throws MigrationException */
Expand Down Expand Up @@ -152,9 +148,5 @@ private function loadMigrationsFromDirectories() : void
);
$this->registerMigrations($migrations);
}

uasort($this->migrations, function (AvailableMigration $a, AvailableMigration $b) : int {
return $this->sorter->compare($a->getVersion(), $b->getVersion());
});
}
}
4 changes: 4 additions & 0 deletions lib/Doctrine/Migrations/Metadata/AvailableMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Doctrine\Migrations\AbstractMigration;
use Doctrine\Migrations\Version\Version;

/**
* Available migrations may or may not be already executed
* The migration might be already executed or not.
*/
final class AvailableMigration
{
/** @var Version */
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/Migrations/Metadata/AvailableMigrationsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use function array_values;
use function count;

/**
* Represents a sorted list of migrations that may or maybe not be already executed.
*/
final class AvailableMigrationsList implements Countable
{
/** @var AvailableMigration[] */
Expand Down
63 changes: 63 additions & 0 deletions lib/Doctrine/Migrations/Metadata/AvailableMigrationsSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Metadata;

use Countable;
use Doctrine\Migrations\Exception\MigrationNotAvailable;
use Doctrine\Migrations\Version\Version;
use function array_values;
use function count;

/**
* Represents a non sorted list of migrations that may or may not be already executed.
*/
final class AvailableMigrationsSet implements Countable
{
/** @var AvailableMigration[] */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have array_values

Suggested change
/** @var AvailableMigration[] */
/** @var list<AvailableMigration> */

Copy link
Member Author

@goetas goetas May 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this compatible with common IDEs ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's compatible with vim :P

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative suggested here:

Suggested change
/** @var AvailableMigration[] */
/**
* @var AvailableMigration[]
* @phpstan-var AvailableMigration[]
* @psalm-var AvailableMigration[]
*/

Not that the second one is superflous: both tools read each other's annotations

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally do not like this "tool" specific annotations... if a third tool will emerge will need to add them too... i suggest to leave AvailableMigration[] as it is

private $items = [];

/**
* @param AvailableMigration[] $items
*/
public function __construct(array $items)
{
$this->items = array_values($items);
}

/**
* @return AvailableMigration[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return AvailableMigration[]
* @return list<AvailableMigration>

*/
public function getItems() : array
{
return $this->items;
}

public function count() : int
{
return count($this->items);
}

public function hasMigration(Version $version) : bool
{
foreach ($this->items as $migration) {
if ($migration->getVersion()->equals($version)) {
return true;
}
}

return false;
}

public function getMigration(Version $version) : AvailableMigration
{
foreach ($this->items as $migration) {
if ($migration->getVersion()->equals($version)) {
return $migration;
}
}

throw MigrationNotAvailable::forVersion($version);
}
}
4 changes: 4 additions & 0 deletions lib/Doctrine/Migrations/Metadata/ExecutedMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use DateTimeImmutable;
use Doctrine\Migrations\Version\Version;

/**
* Represents an already executed migration.
* The migration might be not available anymore.
*/
final class ExecutedMigration
{
/** @var Version */
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/Migrations/Metadata/ExecutedMigrationsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
use function array_values;
use function count;

/**
* Represents a sorted list of executed migrations.
* The migrations in this set might be not available anymore.
*/
final class ExecutedMigrationsList implements Countable
{
/** @var ExecutedMigration[] */
Expand Down
4 changes: 3 additions & 1 deletion lib/Doctrine/Migrations/Metadata/MigrationPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Doctrine\Migrations\Version\ExecutionResult;
use Doctrine\Migrations\Version\Version;

/**
* Represents an available migration to be executed in a specific direction.
*/
final class MigrationPlan
{
/** @var string */
Expand All @@ -17,7 +20,6 @@ final class MigrationPlan
private $version;
/** @var AbstractMigration */
private $migration;

/** @var ExecutionResult */
public $result;

Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/Migrations/Metadata/MigrationPlanList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use function end;
use function reset;

/**
* Represents a sorted list of MigrationPlan instances to execute.
*/
final class MigrationPlanList implements Countable
{
/** @var string */
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/Migrations/MigrationsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Doctrine\Migrations;

use Doctrine\Migrations\Metadata\AvailableMigration;
use Doctrine\Migrations\Metadata\AvailableMigrationsList;
use Doctrine\Migrations\Metadata\AvailableMigrationsSet;
use Doctrine\Migrations\Version\Version;

interface MigrationsRepository
Expand All @@ -14,5 +14,5 @@ public function hasMigration(string $version) : bool;

public function getMigration(Version $version) : AvailableMigration;

public function getMigrations() : AvailableMigrationsList;
public function getMigrations() : AvailableMigrationsSet;
}
2 changes: 1 addition & 1 deletion lib/Doctrine/Migrations/Rollup.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function rollup() : Version

$this->metadataStorage->reset();

$result = new ExecutionResult($versions->getFirst()->getVersion());
$result = new ExecutionResult($versions->getItems()[0]->getVersion());
$this->metadataStorage->complete($result);

return $result->getVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function configure() : void
protected function execute(InputInterface $input, OutputInterface $output) : int
{
$versions = $this->getSortedVersions(
$this->getDependencyFactory()->getMigrationRepository()->getMigrations(), // available migrations
$this->getDependencyFactory()->getMigrationPlanCalculator()->getMigrations(), // available migrations
$this->getDependencyFactory()->getMetadataStorage()->getExecutedMigrations() // executed migrations
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private function markVersions(InputInterface $input, OutputInterface $output) :
}

$executedMigrations = $this->getDependencyFactory()->getMetadataStorage()->getExecutedMigrations();
$availableVersions = $this->getDependencyFactory()->getMigrationRepository()->getMigrations();
$availableVersions = $this->getDependencyFactory()->getMigrationPlanCalculator()->getMigrations();
if ($allOption === true) {
if ($input->getOption('delete') === true) {
foreach ($executedMigrations->getItems() as $availableMigration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Doctrine\Migrations\Metadata\ExecutedMigrationsList;
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\MigrationsRepository;
use Doctrine\Migrations\Version\AliasResolver;
use Doctrine\Migrations\Version\MigrationPlanCalculator;
use Doctrine\Migrations\Version\MigrationStatusCalculator;
use Doctrine\Migrations\Version\Version;
use Symfony\Component\Console\Helper\Table;
Expand Down Expand Up @@ -46,8 +46,8 @@ class MigrationStatusInfosHelper
/** @var MetadataStorage */
private $metadataStorage;

/** @var MigrationsRepository */
private $migrationRepository;
/** @var MigrationPlanCalculator */
private $migrationPlanCalculator;

/** @var MigrationStatusCalculator */
private $statusCalculator;
Expand All @@ -56,16 +56,16 @@ public function __construct(
Configuration $configuration,
Connection $connection,
AliasResolver $aliasResolver,
MigrationsRepository $migrationRepository,
MigrationPlanCalculator $migrationPlanCalculator,
MigrationStatusCalculator $statusCalculator,
MetadataStorage $metadataStorage
) {
$this->configuration = $configuration;
$this->connection = $connection;
$this->aliasResolver = $aliasResolver;
$this->migrationRepository = $migrationRepository;
$this->metadataStorage = $metadataStorage;
$this->statusCalculator = $statusCalculator;
$this->configuration = $configuration;
$this->connection = $connection;
$this->aliasResolver = $aliasResolver;
$this->migrationPlanCalculator = $migrationPlanCalculator;
$this->metadataStorage = $metadataStorage;
$this->statusCalculator = $statusCalculator;
}

/**
Expand All @@ -81,7 +81,7 @@ public function listVersions(array $versions, OutputInterface $output) : void
]
);
$executedMigrations = $this->metadataStorage->getExecutedMigrations();
$availableMigrations = $this->migrationRepository->getMigrations();
$availableMigrations = $this->migrationPlanCalculator->getMigrations();

foreach ($versions as $version) {
$description = null;
Expand Down Expand Up @@ -123,7 +123,7 @@ public function listVersions(array $versions, OutputInterface $output) : void
public function showMigrationsInfo(OutputInterface $output) : void
{
$executedMigrations = $this->metadataStorage->getExecutedMigrations();
$availableMigrations = $this->migrationRepository->getMigrations();
$availableMigrations = $this->migrationPlanCalculator->getMigrations();

$newMigrations = $this->statusCalculator->getNewMigrations();
$executedUnavailableMigrations = $this->statusCalculator->getExecutedUnavailableMigrations();
Expand Down
Loading