|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Daikon\Dbal\Migration; |
|
4
|
|
|
|
|
5
|
|
|
use Assert\Assertion; |
|
6
|
|
|
use Daikon\Dbal\Exception\MigrationException; |
|
7
|
|
|
|
|
8
|
|
|
final class MigrationTarget implements MigrationTargetInterface |
|
9
|
|
|
{ |
|
10
|
|
|
private $name; |
|
11
|
|
|
|
|
12
|
|
|
private $enabled; |
|
13
|
|
|
|
|
14
|
|
|
private $migrationAdapter; |
|
15
|
|
|
|
|
16
|
|
|
private $migrationLoader; |
|
17
|
|
|
|
|
18
|
|
|
private $migrationList; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct( |
|
21
|
|
|
string $name, |
|
22
|
|
|
bool $enabled, |
|
23
|
|
|
MigrationAdapterInterface $migrationAdapter, |
|
24
|
|
|
MigrationLoaderInterface $migrationLoader |
|
25
|
|
|
) { |
|
26
|
|
|
$this->name = $name; |
|
27
|
|
|
$this->enabled = $enabled; |
|
28
|
|
|
$this->migrationAdapter = $migrationAdapter; |
|
29
|
|
|
$this->migrationLoader = $migrationLoader; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getName(): string |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->name; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function isEnabled(): bool |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->enabled === true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getMigrationList(): MigrationList |
|
43
|
|
|
{ |
|
44
|
|
|
if (!$this->migrationList) { |
|
45
|
|
|
$availableMigrations = $this->migrationLoader->load(); |
|
46
|
|
|
$executedMigrations = $this->migrationAdapter->read($this->name); |
|
47
|
|
|
$pendingMigrations = $availableMigrations->diff($executedMigrations); |
|
|
|
|
|
|
48
|
|
|
$this->migrationList = $executedMigrations->merge($pendingMigrations); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
return $this->migrationList; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function migrate(string $direction, string $version = null): MigrationList |
|
54
|
|
|
{ |
|
55
|
|
|
Assertion::true($this->isEnabled()); |
|
56
|
|
|
|
|
57
|
|
|
if ($direction === MigrationInterface::MIGRATE_DOWN) { |
|
58
|
|
|
$executedMigrations = $this->migrateDown($version); |
|
59
|
|
|
} else { |
|
60
|
|
|
$executedMigrations = $this->migrateUp($version); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $executedMigrations; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
View Code Duplication |
private function migrateUp(int $version = null): MigrationList |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
$migrationList = $this->getMigrationList(); |
|
69
|
|
|
$pendingMigrations = $migrationList->getPendingMigrations(); |
|
70
|
|
|
$executedMigrations = $migrationList->getExecutedMigrations(); |
|
71
|
|
|
|
|
72
|
|
|
if ($version && !$pendingMigrations->containsVersion($version)) { |
|
|
|
|
|
|
73
|
|
|
throw new MigrationException(sprintf('Target version %s not found in pending migrations.', $version)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$completedMigrations = []; |
|
77
|
|
|
$connector = $this->migrationAdapter->getConnector(); |
|
78
|
|
|
foreach ($pendingMigrations as $migration) { |
|
79
|
|
|
$migration->execute($connector, MigrationInterface::MIGRATE_UP); |
|
80
|
|
|
$executedMigrations = $executedMigrations->push($migration); |
|
81
|
|
|
$this->migrationAdapter->write($this->name, $executedMigrations); |
|
82
|
|
|
$completedMigrations[] = $migration; |
|
83
|
|
|
if ($version && $migration->getVersion() === $version) { |
|
|
|
|
|
|
84
|
|
|
break; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return new MigrationList($completedMigrations); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
View Code Duplication |
private function migrateDown(int $version = null): MigrationList |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$migrationList = $this->getMigrationList(); |
|
94
|
|
|
$executedMigrations = $migrationList->getExecutedMigrations(); |
|
95
|
|
|
|
|
96
|
|
|
if ($version && !$executedMigrations->containsVersion($version)) { |
|
|
|
|
|
|
97
|
|
|
throw new MigrationException(sprintf('Target version %s not found in executed migrations.', $version)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$completedMigrations = []; |
|
101
|
|
|
$connector = $this->migrationAdapter->getConnector(); |
|
102
|
|
|
foreach ($executedMigrations->reverse() as $migration) { |
|
103
|
|
|
if ($version && $migration->getVersion() === $version) { |
|
|
|
|
|
|
104
|
|
|
break; |
|
105
|
|
|
} |
|
106
|
|
|
$migration->execute($connector, MigrationInterface::MIGRATE_DOWN); |
|
107
|
|
|
$executedMigrations = $executedMigrations->remove($migration); |
|
108
|
|
|
if ($executedMigrations->count() > 0) { |
|
109
|
|
|
/* |
|
110
|
|
|
* Do not write version info after reversing initial migration since we have |
|
111
|
|
|
* an assumption that the database was deleted! |
|
112
|
|
|
*/ |
|
113
|
|
|
$this->migrationAdapter->write($this->name, $executedMigrations); |
|
114
|
|
|
} |
|
115
|
|
|
$completedMigrations[] = $migration; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return new MigrationList($completedMigrations); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: