1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Version; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Exception\NoMigrationsFoundWithCriteria; |
8
|
|
|
use Doctrine\Migrations\Exception\NoMigrationsToExecute; |
9
|
|
|
use Doctrine\Migrations\Metadata; |
10
|
|
|
use Doctrine\Migrations\Metadata\AvailableMigration; |
11
|
|
|
use Doctrine\Migrations\Metadata\MigrationPlan; |
12
|
|
|
use Doctrine\Migrations\Metadata\MigrationPlanList; |
13
|
|
|
use Doctrine\Migrations\Metadata\Storage\MetadataStorage; |
14
|
|
|
use Doctrine\Migrations\MigrationRepository; |
15
|
|
|
use function array_map; |
16
|
|
|
use function array_reverse; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The MigrationPlanCalculator is responsible for calculating the plan for migrating from the current |
20
|
|
|
* version to another version. |
21
|
|
|
* |
22
|
|
|
* @internal |
23
|
|
|
*/ |
24
|
|
|
final class SortedMigrationPlanCalculator implements MigrationPlanCalculator |
25
|
|
|
{ |
26
|
|
|
/** @var MigrationRepository */ |
27
|
|
|
private $migrationRepository; |
28
|
|
|
|
29
|
|
|
/** @var MetadataStorage */ |
30
|
|
|
private $metadataStorage; |
31
|
|
|
|
32
|
26 |
|
public function __construct(MigrationRepository $migrationRepository, MetadataStorage $metadataStorage) |
33
|
|
|
{ |
34
|
26 |
|
$this->migrationRepository = $migrationRepository; |
35
|
26 |
|
$this->metadataStorage = $metadataStorage; |
36
|
26 |
|
} |
37
|
|
|
|
38
|
5 |
|
public function getPlanForExactVersion(Version $version, string $direction) : MigrationPlanList |
39
|
|
|
{ |
40
|
5 |
|
$migration = $this->migrationRepository->getMigration($version); |
41
|
|
|
|
42
|
5 |
|
$planItem = new MigrationPlan($migration->getVersion(), $migration->getMigration(), $direction); |
43
|
|
|
|
44
|
5 |
|
return new MigrationPlanList([$planItem], $direction); |
45
|
|
|
} |
46
|
|
|
|
47
|
20 |
|
public function getPlanUntilVersion(?Version $to = null) : MigrationPlanList |
48
|
|
|
{ |
49
|
20 |
|
$availableMigrations = $this->migrationRepository->getMigrations(); |
50
|
20 |
|
$executedMigrations = $this->metadataStorage->getExecutedMigrations(); |
51
|
|
|
|
52
|
|
|
try { |
53
|
20 |
|
$to = $to ?: $availableMigrations->getLast()->getVersion(); |
54
|
1 |
|
} catch (NoMigrationsFoundWithCriteria $e) { |
55
|
1 |
|
throw NoMigrationsToExecute::new($e); |
56
|
|
|
} |
57
|
|
|
|
58
|
19 |
|
$direction = $this->findDirection($to, $executedMigrations); |
59
|
|
|
|
60
|
19 |
|
$migrationsToCheck = $this->arrangeMigrationsForDirection($direction, $availableMigrations); |
61
|
|
|
|
62
|
19 |
|
$toExecute = $this->findMigrationsToExecute($to, $migrationsToCheck, $direction, $executedMigrations); |
63
|
|
|
|
64
|
|
|
return new MigrationPlanList(array_map(static function (AvailableMigration $migration) use ($direction) { |
65
|
15 |
|
return new MigrationPlan($migration->getVersion(), $migration->getMigration(), $direction); |
66
|
19 |
|
}, $toExecute), $direction); |
67
|
|
|
} |
68
|
|
|
|
69
|
19 |
|
private function findDirection(Version $to, Metadata\ExecutedMigrationsSet $executedMigrations) : string |
70
|
|
|
{ |
71
|
19 |
|
if ((string) $to === '0' || ($executedMigrations->hasMigration($to) && ! $executedMigrations->getLast()->getVersion()->equals($to))) { |
72
|
2 |
|
return Direction::DOWN; |
73
|
|
|
} |
74
|
|
|
|
75
|
17 |
|
return Direction::UP; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return AvailableMigration[] |
80
|
|
|
*/ |
81
|
19 |
|
private function arrangeMigrationsForDirection(string $direction, Metadata\AvailableMigrationsList $availableMigrations) : array |
82
|
|
|
{ |
83
|
19 |
|
return $direction === Direction::UP ? $availableMigrations->getItems() : array_reverse($availableMigrations->getItems()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param AvailableMigration[] $migrationsToCheck |
88
|
|
|
* |
89
|
|
|
* @return AvailableMigration[] |
90
|
|
|
*/ |
91
|
19 |
|
private function findMigrationsToExecute(Version $to, array $migrationsToCheck, string $direction, Metadata\ExecutedMigrationsSet $executedMigrations) : array |
92
|
|
|
{ |
93
|
19 |
|
$toExecute = []; |
94
|
19 |
|
foreach ($migrationsToCheck as $availableMigration) { |
95
|
19 |
|
if ($direction === Direction::DOWN && $availableMigration->getVersion()->equals($to)) { |
96
|
1 |
|
break; |
97
|
|
|
} |
98
|
|
|
|
99
|
19 |
|
if ($direction === Direction::UP && ! $executedMigrations->hasMigration($availableMigration->getVersion())) { |
100
|
13 |
|
$toExecute[] = $availableMigration; |
101
|
8 |
|
} elseif ($direction === Direction::DOWN && $executedMigrations->hasMigration($availableMigration->getVersion())) { |
102
|
2 |
|
$toExecute[] = $availableMigration; |
103
|
|
|
} |
104
|
|
|
|
105
|
19 |
|
if ($direction === Direction::UP && $availableMigration->getVersion()->equals($to)) { |
106
|
17 |
|
break; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
19 |
|
return $toExecute; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|