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

Split migration planner and extract relative interfaces #897

Merged
merged 4 commits into from
Jan 3, 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
18 changes: 16 additions & 2 deletions lib/Doctrine/Migrations/DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@
use Doctrine\Migrations\Tools\Console\Helper\MigrationStatusInfosHelper;
use Doctrine\Migrations\Tools\Console\MigratorConfigurationFactory;
use Doctrine\Migrations\Version\AliasResolver;
use Doctrine\Migrations\Version\CurrentMigrationStatusCalculator;
use Doctrine\Migrations\Version\DbalExecutor;
use Doctrine\Migrations\Version\DefaultAliasResolver;
use Doctrine\Migrations\Version\Executor;
use Doctrine\Migrations\Version\MigrationFactory;
use Doctrine\Migrations\Version\MigrationPlanCalculator;
use Doctrine\Migrations\Version\MigrationStatusCalculator;
use Doctrine\Migrations\Version\SortedMigrationPlanCalculator;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
Expand Down Expand Up @@ -285,15 +289,25 @@ public function getVersionAliasResolver() : AliasResolver
return new DefaultAliasResolver(
$this->getMigrationRepository(),
$this->getMetadataStorage(),
$this->getMigrationPlanCalculator()
$this->getMigrationStatusCalculator()
);
});
}

public function getMigrationStatusCalculator() : MigrationStatusCalculator
{
return $this->getDependency(MigrationStatusCalculator::class, function () : MigrationStatusCalculator {
return new CurrentMigrationStatusCalculator(
$this->getMigrationRepository(),
$this->getMetadataStorage()
);
});
}

public function getMigrationPlanCalculator() : MigrationPlanCalculator
{
return $this->getDependency(MigrationPlanCalculator::class, function () : MigrationPlanCalculator {
return new MigrationPlanCalculator(
return new SortedMigrationPlanCalculator(
$this->getMigrationRepository(),
$this->getMetadataStorage()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public function execute(InputInterface $input, OutputInterface $output) : ?int
}

$planCalculator = $this->getDependencyFactory()->getMigrationPlanCalculator();
$executedUnavailableMigrations = $planCalculator->getExecutedUnavailableMigrations();
$statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator();
$executedUnavailableMigrations = $statusCalculator->getExecutedUnavailableMigrations();

if ($this->checkExecutedUnavailableMigrations($executedUnavailableMigrations, $input, $output) === false) {
return 3;
Expand Down
10 changes: 5 additions & 5 deletions lib/Doctrine/Migrations/Tools/Console/Command/StatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ protected function configure() : void

public function execute(InputInterface $input, OutputInterface $output) : ?int
{
$storage = $this->getDependencyFactory()->getMetadataStorage();
$migrationRepo = $this->getDependencyFactory()->getMigrationRepository();
$planCalculator = $this->getDependencyFactory()->getMigrationPlanCalculator();
$storage = $this->getDependencyFactory()->getMetadataStorage();
$migrationRepo = $this->getDependencyFactory()->getMigrationRepository();

$availableMigrations = $migrationRepo->getMigrations();
$executedMigrations = $storage->getExecutedMigrations();

$newMigrations = $planCalculator->getNewMigrations();
$executedUnavailableMigrations = $planCalculator->getExecutedUnavailableMigrations();
$statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator();
$newMigrations = $statusCalculator->getNewMigrations();
$executedUnavailableMigrations = $statusCalculator->getExecutedUnavailableMigrations();

$infosHelper = $this->getDependencyFactory()->getMigrationStatusInfosHelper();
$infosHelper->showMigrationsInfo($output, $availableMigrations, $executedMigrations, $newMigrations, $executedUnavailableMigrations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ protected function configure() : void

public function execute(InputInterface $input, OutputInterface $output) : ?int
{
$planCalculator = $this->getDependencyFactory()->getMigrationPlanCalculator();
$executedUnavailableMigrations = $planCalculator->getExecutedUnavailableMigrations();
$newMigrations = $planCalculator->getNewMigrations();
$statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator();

$executedUnavailableMigrations = $statusCalculator->getExecutedUnavailableMigrations();

$newMigrations = $statusCalculator->getNewMigrations();

$newMigrationsCount = count($newMigrations);
$executedUnavailableMigrationsCount = count($executedUnavailableMigrations);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Version;

use Doctrine\Migrations\Metadata\AvailableMigration;
use Doctrine\Migrations\Metadata\AvailableMigrationsList;
use Doctrine\Migrations\Metadata\ExecutedMigration;
use Doctrine\Migrations\Metadata\ExecutedMigrationsSet;
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
use Doctrine\Migrations\MigrationRepository;
use function array_filter;

/**
* The MigrationPlanCalculator is responsible for calculating the plan for migrating from the current
* version to another version.
*/
final class CurrentMigrationStatusCalculator implements MigrationStatusCalculator
{
/** @var MigrationRepository */
private $migrationRepository;

/** @var MetadataStorage */
private $metadataStorage;

public function __construct(MigrationRepository $migrationRepository, MetadataStorage $metadataStorage)
{
$this->migrationRepository = $migrationRepository;
$this->metadataStorage = $metadataStorage;
}

public function getExecutedUnavailableMigrations() : ExecutedMigrationsSet
{
$executedMigrationsSet = $this->metadataStorage->getExecutedMigrations();
$availableMigrationsSet = $this->migrationRepository->getMigrations();

return new ExecutedMigrationsSet(array_filter($executedMigrationsSet->getItems(), static function (ExecutedMigration $migrationInfo) use ($availableMigrationsSet) : bool {
return ! $availableMigrationsSet->hasMigration($migrationInfo->getVersion());
}));
}

public function getNewMigrations() : AvailableMigrationsList
{
$executedMigrationsSet = $this->metadataStorage->getExecutedMigrations();
$availableMigrationsSet = $this->migrationRepository->getMigrations();

return new AvailableMigrationsList(array_filter($availableMigrationsSet->getItems(), static function (AvailableMigration $migrationInfo) use ($executedMigrationsSet) : bool {
return ! $executedMigrationsSet->hasMigration($migrationInfo->getVersion());
}));
}
}
24 changes: 13 additions & 11 deletions lib/Doctrine/Migrations/Version/DefaultAliasResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Doctrine\Migrations\Exception\NoMigrationsToExecute;
use Doctrine\Migrations\Exception\UnknownMigrationVersion;
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
use Doctrine\Migrations\MigrationPlanCalculator;
use Doctrine\Migrations\MigrationRepository;
use function count;
use function substr;
Expand All @@ -32,14 +31,17 @@ final class DefaultAliasResolver implements AliasResolver
/** @var MetadataStorage */
private $metadataStorage;

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

public function __construct(MigrationRepository $migrationRepository, MetadataStorage $metadataStorage, MigrationPlanCalculator $migrationPlanCalculator)
{
$this->migrationRepository = $migrationRepository;
$this->metadataStorage = $metadataStorage;
$this->migrationPlanCalculator = $migrationPlanCalculator;
/** @var MigrationStatusCalculator */
private $migrationStatusCalculator;

public function __construct(
MigrationRepository $migrationRepository,
MetadataStorage $metadataStorage,
MigrationStatusCalculator $migrationStatusCalculator
) {
$this->migrationRepository = $migrationRepository;
$this->metadataStorage = $metadataStorage;
$this->migrationStatusCalculator = $migrationStatusCalculator;
}

/**
Expand Down Expand Up @@ -82,7 +84,7 @@ public function resolveVersionAlias(string $alias) : Version
}
break;
case self::ALIAS_NEXT:
$newMigrations = $this->migrationPlanCalculator->getNewMigrations();
$newMigrations = $this->migrationStatusCalculator->getNewMigrations();

try {
return $newMigrations->getFirst()->getVersion();
Expand All @@ -106,7 +108,7 @@ public function resolveVersionAlias(string $alias) : Version
$val = (int) substr($alias, 7);
$targetMigration = null;
if ($val > 0) {
$newMigrations = $this->migrationPlanCalculator->getNewMigrations();
$newMigrations = $this->migrationStatusCalculator->getNewMigrations();

return $newMigrations->getFirst($val - 1)->getVersion();
}
Expand Down
18 changes: 18 additions & 0 deletions lib/Doctrine/Migrations/Version/MigrationPlanCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Version;

use Doctrine\Migrations\Metadata\MigrationPlanList;

/**
* The MigrationPlanCalculator is responsible for calculating the plan for migrating from the current
* version to another version.
*/
interface MigrationPlanCalculator
{
public function getPlanForExactVersion(Version $version, string $direction) : MigrationPlanList;

public function getPlanUntilVersion(?Version $to = null) : MigrationPlanList;
}
19 changes: 19 additions & 0 deletions lib/Doctrine/Migrations/Version/MigrationStatusCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Version;

use Doctrine\Migrations\Metadata\AvailableMigrationsList;
use Doctrine\Migrations\Metadata\ExecutedMigrationsSet;

/**
* The MigrationStatusCalculator is responsible for calculating the current status of
* migrated and not available versions.
*/
interface MigrationStatusCalculator
{
public function getExecutedUnavailableMigrations() : ExecutedMigrationsSet;

public function getNewMigrations() : AvailableMigrationsList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

declare(strict_types=1);

namespace Doctrine\Migrations;
namespace Doctrine\Migrations\Version;

use Doctrine\Migrations\Exception\NoMigrationsFoundWithCriteria;
use Doctrine\Migrations\Exception\NoMigrationsToExecute;
use Doctrine\Migrations\Metadata;
use Doctrine\Migrations\Metadata\AvailableMigration;
use Doctrine\Migrations\Metadata\AvailableMigrationsList;
use Doctrine\Migrations\Metadata\ExecutedMigration;
use Doctrine\Migrations\Metadata\ExecutedMigrationsSet;
use Doctrine\Migrations\Metadata\MigrationPlan;
use Doctrine\Migrations\Metadata\MigrationPlanList;
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
use Doctrine\Migrations\Version\Direction;
use Doctrine\Migrations\Version\Version;
use function array_filter;
use Doctrine\Migrations\MigrationRepository;
use function array_map;
use function array_reverse;

Expand All @@ -25,7 +21,7 @@
*
* @internal
*/
final class MigrationPlanCalculator
final class SortedMigrationPlanCalculator implements MigrationPlanCalculator
{
/** @var MigrationRepository */
private $migrationRepository;
Expand Down Expand Up @@ -113,24 +109,4 @@ private function findMigrationsToExecute(Version $to, array $migrationsToCheck,

return $toExecute;
}

public function getExecutedUnavailableMigrations() : ExecutedMigrationsSet
{
$executedMigrationsSet = $this->metadataStorage->getExecutedMigrations();
$availableMigrationsSet = $this->migrationRepository->getMigrations();

return new ExecutedMigrationsSet(array_filter($executedMigrationsSet->getItems(), static function (ExecutedMigration $migrationInfo) use ($availableMigrationsSet) {
return ! $availableMigrationsSet->hasMigration($migrationInfo->getVersion());
}));
}

public function getNewMigrations() : AvailableMigrationsList
{
$executedMigrationsSet = $this->metadataStorage->getExecutedMigrations();
$availableMigrationsSet = $this->migrationRepository->getMigrations();

return new AvailableMigrationsList(array_filter($availableMigrationsSet->getItems(), static function (AvailableMigration $migrationInfo) use ($executedMigrationsSet) {
return ! $executedMigrationsSet->hasMigration($migrationInfo->getVersion());
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
use Doctrine\Migrations\Metadata\MigrationPlanList;
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\MigrationPlanCalculator;
use Doctrine\Migrations\MigrationRepository;
use Doctrine\Migrations\Migrator;
use Doctrine\Migrations\MigratorConfiguration;
use Doctrine\Migrations\QueryWriter;
use Doctrine\Migrations\Tools\Console\Command\ExecuteCommand;
use Doctrine\Migrations\Version\SortedMigrationPlanCalculator;
use Doctrine\Migrations\Version\Version;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -149,7 +149,7 @@ protected function setUp() : void
->method('getMigration')
->willReturn($m1);

$planCalculator = new MigrationPlanCalculator($migrationRepository, $storage);
$planCalculator = new SortedMigrationPlanCalculator($migrationRepository, $storage);

$configuration = new Configuration();
$configuration->setMetadataStorageConfiguration(new TableMetadataStorageConfiguration());
Expand Down
11 changes: 6 additions & 5 deletions tests/Doctrine/Migrations/Tests/Version/AliasResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
use Doctrine\Migrations\Finder\RecursiveRegexFinder;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorage;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\MigrationPlanCalculator;
use Doctrine\Migrations\MigrationRepository;
use Doctrine\Migrations\Version\CurrentMigrationStatusCalculator;
use Doctrine\Migrations\Version\DefaultAliasResolver;
use Doctrine\Migrations\Version\Direction;
use Doctrine\Migrations\Version\ExecutionResult;
use Doctrine\Migrations\Version\MigrationFactory;
use Doctrine\Migrations\Version\MigrationStatusCalculator;
use Doctrine\Migrations\Version\Version;
use PHPUnit\Framework\TestCase;
use function sys_get_temp_dir;
Expand All @@ -34,8 +35,8 @@ final class AliasResolverTest extends TestCase
/** @var TableMetadataStorage */
private $metadataStorage;

/** @var MigrationPlanCalculator */
private $planCalculator;
/** @var MigrationStatusCalculator */
private $statusCalculator;

/**
* @dataProvider getAliases
Expand Down Expand Up @@ -137,11 +138,11 @@ protected function setUp() : void
$versionFactory
);
$this->metadataStorage = new TableMetadataStorage($conn);
$this->planCalculator = new MigrationPlanCalculator($this->migrationRepository, $this->metadataStorage);
$this->statusCalculator = new CurrentMigrationStatusCalculator($this->migrationRepository, $this->metadataStorage);
$this->versionAliasResolver = new DefaultAliasResolver(
$this->migrationRepository,
$this->metadataStorage,
$this->planCalculator
$this->statusCalculator
);
}

Expand Down
Loading