-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathOrderResolver.php
More file actions
executable file
·285 lines (232 loc) · 6.71 KB
/
OrderResolver.php
File metadata and controls
executable file
·285 lines (232 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php declare(strict_types = 1);
/**
* This file is part of the Nextras community extensions of Nette Framework
*
* @license New BSD License
* @link https://github.com/nextras/migrations
*/
namespace Nextras\Migrations\Engine;
use Nextras\Migrations\Entities\File;
use Nextras\Migrations\Entities\Group;
use Nextras\Migrations\Entities\Migration;
use Nextras\Migrations\LogicException;
class OrderResolver
{
/**
* @param list<Migration> $migrations
* @param list<Group> $groups
* @param list<File> $files
* @return list<File>
* @throws LogicException
*/
public function resolve(array $migrations, array $groups, array $files, string $mode): array
{
$groups = $this->getAssocGroups($groups);
$this->validateGroups($groups);
if ($mode === Runner::MODE_RESET) {
return $this->sortFiles($files, $groups);
} elseif ($mode !== Runner::MODE_CONTINUE) {
throw new LogicException('Unsupported mode.');
}
$migrations = $this->getAssocMigrations($migrations);
$files = $this->getAssocFiles($files);
$lastMigrations = [];
foreach ($migrations as $groupName => $mg) {
if (!isset($groups[$groupName])) {
throw new LogicException(sprintf(
'Existing migrations depend on unknown group "%s".',
$groupName
));
}
$group = $groups[$groupName];
foreach ($mg as $filename => $migration) {
if (!$migration->completed) {
throw new LogicException(sprintf(
'Previously executed migration "%s/%s" did not succeed. Please fix this manually or reset the migrations.',
$groupName, $filename
));
}
if (isset($files[$groupName][$filename])) {
$file = $files[$groupName][$filename];
if ($group->checkChecksum && $migration->checksum !== $file->checksum) {
throw new LogicException(sprintf(
'Previously executed migration "%s/%s" has been changed. File checksum is "%s", but executed migration had checksum "%s".',
$groupName, $filename, $file->checksum, $migration->checksum
));
}
unset($files[$groupName][$filename]);
} elseif ($group->checkMissingPreviousExecuted && $group->enabled) {
throw new LogicException(sprintf(
'Previously executed migration "%s/%s" is missing.',
$groupName, $filename
));
}
if (!isset($lastMigrations[$groupName]) || strcmp($filename, $lastMigrations[$groupName]) > 0) {
$lastMigrations[$groupName] = $filename;
}
}
}
$files = $this->getFlatFiles($files);
$files = $this->sortFiles($files, $groups);
foreach ($groups as $group) {
if (!isset($lastMigrations[$group->name])) {
continue;
}
if (!$group->checkDependMigration) {
continue;
}
foreach ($this->getFirstFiles($files) as $file) {
if (strcmp($file->name, $lastMigrations[$group->name]) >= 0) {
continue;
}
if ($this->isGroupDependentOn($groups, $file->group, $group) || $this->isGroupDependentOn($groups, $group, $file->group)) {
throw new LogicException(sprintf(
'New migration "%s/%s" must follow after the latest executed migration "%s/%s".',
$file->group->name, $file->name, $group->name, $lastMigrations[$group->name]
));
}
}
}
return $files;
}
/**
* @param list<File> $files
* @param array<string, Group> $groups (name => Group)
* @return list<File> sorted
*/
protected function sortFiles(array $files, array $groups): array
{
usort($files, function (File $a, File $b) use ($groups): int {
$cmp = strcmp($a->name, $b->name);
if ($cmp === 0 && $a !== $b) {
$cmpA = $this->isGroupDependentOn($groups, $a->group, $b->group);
$cmpB = $this->isGroupDependentOn($groups, $b->group, $a->group);
if ($cmpA xor $cmpB) {
$cmp = ($cmpA ? -1 : 1);
} else {
$names = [
"{$a->group->name}/{$a->name}",
"{$b->group->name}/{$b->name}",
];
sort($names);
throw new LogicException(sprintf(
'Unable to determine order for migrations "%s" and "%s".',
$names[0], $names[1]
));
}
}
return $cmp;
});
return $files;
}
/**
* Returns true if groupA depends on groupB.
*
* @param array<string, Group> $groups (name => Group)
*/
protected function isGroupDependentOn(array $groups, Group $groupA, Group $groupB): bool
{
$visited = [];
$queue = $groupB->dependencies;
$queue[] = $groupB->name;
while ($node = array_shift($queue)) {
if (isset($visited[$node])) {
continue;
}
if ($groupA->name === $node) {
return true;
}
$visited[$node] = true;
foreach ($groups[$node]->dependencies as $dep) {
$queue[] = $dep;
}
}
return false;
}
/**
* @param list<Migration> $migrations
* @return array<string, array<string, Migration>> (group => (filename => Migration))
*/
protected function getAssocMigrations(array $migrations): array
{
$assoc = [];
foreach ($migrations as $migration) {
$assoc[$migration->group][$migration->filename] = $migration;
}
return $assoc;
}
/**
* @param list<Group> $groups
* @return array<string, Group> (name => Group)
*/
protected function getAssocGroups(array $groups): array
{
$assoc = [];
foreach ($groups as $group) {
$assoc[$group->name] = $group;
}
return $assoc;
}
/**
* @param list<File> $files
* @return array<string, array<string, File>> (group => (filename => File))
*/
protected function getAssocFiles(array $files): array
{
$assoc = [];
foreach ($files as $file) {
$assoc[$file->group->name][$file->name] = $file;
}
return $assoc;
}
/**
* @param array<string, array<string, File>> $files
* @return list<File>
*/
protected function getFlatFiles(array $files): array
{
$flat = [];
foreach ($files as $tmp) {
foreach ($tmp as $file) {
$flat[] = $file;
}
}
return $flat;
}
/**
* @param list<File> $files
* @return array<string, File> (group => File)
*/
protected function getFirstFiles(array $files): array
{
$firstFiles = [];
foreach ($files as $file) {
if (!isset($firstFiles[$file->group->name])) {
$firstFiles[$file->group->name] = $file;
}
}
return $firstFiles;
}
/**
* @param array<Group> $groups
* @throws LogicException
*/
private function validateGroups(array $groups): void
{
foreach ($groups as $group) {
foreach ($group->dependencies as $dependency) {
if (!isset($groups[$dependency])) {
throw new LogicException(sprintf(
'Group "%s" depends on unknown group "%s".',
$group->name, $dependency
));
} elseif ($group->enabled && !$groups[$dependency]->enabled) {
throw new LogicException(sprintf(
'Group "%s" depends on disabled group "%s". Please enable group "%s" to continue.',
$group->name, $dependency, $dependency
));
}
}
}
}
}