Skip to content

Commit 0fe1a77

Browse files
Merge pull request #49668 from nextcloud/backport/47515/stable29
[stable29] fix(migration): Correctly sort migrations by version number
2 parents 7d5b288 + 0772605 commit 0fe1a77

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

lib/private/DB/MigrationService.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private function createMigrationTable(): bool {
180180
/**
181181
* Returns all versions which have already been applied
182182
*
183-
* @return string[]
183+
* @return list<string>
184184
* @codeCoverageIgnore - no need to test this
185185
*/
186186
public function getMigratedVersions() {
@@ -196,6 +196,8 @@ public function getMigratedVersions() {
196196
$rows = $result->fetchAll(\PDO::FETCH_COLUMN);
197197
$result->closeCursor();
198198

199+
usort($rows, [$this, 'sortMigrations']);
200+
199201
return $rows;
200202
}
201203

@@ -205,7 +207,23 @@ public function getMigratedVersions() {
205207
*/
206208
public function getAvailableVersions(): array {
207209
$this->ensureMigrationsAreLoaded();
208-
return array_map('strval', array_keys($this->migrations));
210+
$versions = array_map('strval', array_keys($this->migrations));
211+
usort($versions, [$this, 'sortMigrations']);
212+
return $versions;
213+
}
214+
215+
protected function sortMigrations(string $a, string $b): int {
216+
preg_match('/(\d+)Date(\d+)/', basename($a), $matchA);
217+
preg_match('/(\d+)Date(\d+)/', basename($b), $matchB);
218+
if (!empty($matchA) && !empty($matchB)) {
219+
$versionA = (int)$matchA[1];
220+
$versionB = (int)$matchB[1];
221+
if ($versionA !== $versionB) {
222+
return ($versionA < $versionB) ? -1 : 1;
223+
}
224+
return ($matchA[2] < $matchB[2]) ? -1 : 1;
225+
}
226+
return (basename($a) < basename($b)) ? -1 : 1;
209227
}
210228

211229
/**
@@ -226,17 +244,7 @@ protected function findMigrations(): array {
226244
\RegexIterator::GET_MATCH);
227245

228246
$files = array_keys(iterator_to_array($iterator));
229-
uasort($files, function ($a, $b) {
230-
preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA);
231-
preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB);
232-
if (!empty($matchA) && !empty($matchB)) {
233-
if ($matchA[1] !== $matchB[1]) {
234-
return ($matchA[1] < $matchB[1]) ? -1 : 1;
235-
}
236-
return ($matchA[2] < $matchB[2]) ? -1 : 1;
237-
}
238-
return (basename($a) < basename($b)) ? -1 : 1;
239-
});
247+
usort($files, [$this, 'sortMigrations']);
240248

241249
$migrations = [];
242250

0 commit comments

Comments
 (0)