Skip to content

Commit

Permalink
Merge pull request #1186 from doctrine/skipped-migrations
Browse files Browse the repository at this point in the history
Make sure to run migrations up if there are not executed migrations
  • Loading branch information
greg0ire authored Aug 2, 2021
2 parents c13e866 + b426bc0 commit ce8233b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/Doctrine/Migrations/Version/SortedMigrationPlanCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public function getPlanUntilVersion(Version $to): MigrationPlanList
throw MigrationClassNotFound::new((string) $to);
}

$availableMigrations = $this->getMigrations();
$availableMigrations = $this->getMigrations(); // migrations are sorted at this point
$executedMigrations = $this->metadataStorage->getExecutedMigrations();

$direction = $this->findDirection($to, $executedMigrations);
$direction = $this->findDirection($to, $executedMigrations, $availableMigrations);

$migrationsToCheck = $this->arrangeMigrationsForDirection($direction, $availableMigrations);

Expand All @@ -107,9 +107,23 @@ public function getMigrations(): AvailableMigrationsList
return new AvailableMigrationsList($availableMigrations);
}

private function findDirection(Version $to, ExecutedMigrationsList $executedMigrations): string
private function findDirection(Version $to, ExecutedMigrationsList $executedMigrations, AvailableMigrationsList $availableMigrations): string
{
if ((string) $to === '0' || ($executedMigrations->hasMigration($to) && ! $executedMigrations->getLast()->getVersion()->equals($to))) {
if ((string) $to === '0') {
return Direction::DOWN;
}

foreach ($availableMigrations->getItems() as $availableMigration) {
if ($availableMigration->getVersion()->equals($to)) {
break;
}

if (! $executedMigrations->hasMigration($availableMigration->getVersion())) {
return Direction::UP;
}
}

if ($executedMigrations->hasMigration($to) && ! $executedMigrations->getLast()->getVersion()->equals($to)) {
return Direction::DOWN;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,43 @@ public function getPlanUpWhenMigrations(): array
];
}

/**
* @param string[] $expectedPlan
*
* @dataProvider getPlanUpWhenMigrationsOutOfOrder
*/
public function testPlanWhenMigrationsOutOfOrder(string $to, array $expectedPlan, string $direction): void
{
$e1 = new ExecutedMigration(new Version('B'));
$e2 = new ExecutedMigration(new Version('C'));

$this->metadataStorage
->expects(self::atLeastOnce())
->method('getExecutedMigrations')
->willReturn(new ExecutedMigrationsList([$e1, $e2]));

$plan = $this->migrationPlanCalculator->getPlanUntilVersion(new Version($to));

self::assertCount(count($expectedPlan), $plan);

self::assertSame($direction, $plan->getDirection());

foreach ($expectedPlan as $itemN => $version) {
self::assertSame($direction, $plan->getItems()[$itemN]->getDirection());
self::assertEquals(new Version($version), $plan->getItems()[$itemN]->getVersion());
}
}

/**
* @return mixed[]
*/
public function getPlanUpWhenMigrationsOutOfOrder(): array
{
return [
['C',['A'],Direction::UP],
];
}

public function testCustomMigrationSorting(): void
{
$reverseSorter = new class implements Comparator {
Expand Down

0 comments on commit ce8233b

Please sign in to comment.