Skip to content

Commit

Permalink
Merge pull request #47515 from nextcloud/bugfix/noid/fix-migration-so…
Browse files Browse the repository at this point in the history
…rting

fix(migration): Correctly sort migrations by version number
  • Loading branch information
AndyScherzinger authored Aug 28, 2024
2 parents 3a565b3 + 46c8531 commit d404998
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions lib/private/DB/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private function createMigrationTable(): bool {
/**
* Returns all versions which have already been applied
*
* @return string[]
* @return list<string>
* @codeCoverageIgnore - no need to test this
*/
public function getMigratedVersions() {
Expand All @@ -174,6 +174,8 @@ public function getMigratedVersions() {
$rows = $result->fetchAll(\PDO::FETCH_COLUMN);
$result->closeCursor();

usort($rows, $this->sortMigrations(...));

return $rows;
}

Expand All @@ -183,7 +185,23 @@ public function getMigratedVersions() {
*/
public function getAvailableVersions(): array {
$this->ensureMigrationsAreLoaded();
return array_map('strval', array_keys($this->migrations));
$versions = array_map('strval', array_keys($this->migrations));
usort($versions, $this->sortMigrations(...));
return $versions;
}

protected function sortMigrations(string $a, string $b): int {
preg_match('/(\d+)Date(\d+)/', basename($a), $matchA);
preg_match('/(\d+)Date(\d+)/', basename($b), $matchB);
if (!empty($matchA) && !empty($matchB)) {
$versionA = (int)$matchA[1];
$versionB = (int)$matchB[1];
if ($versionA !== $versionB) {
return ($versionA < $versionB) ? -1 : 1;
}
return ($matchA[2] < $matchB[2]) ? -1 : 1;
}
return (basename($a) < basename($b)) ? -1 : 1;
}

/**
Expand All @@ -204,17 +222,7 @@ protected function findMigrations(): array {
\RegexIterator::GET_MATCH);

$files = array_keys(iterator_to_array($iterator));
uasort($files, function ($a, $b) {
preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA);
preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB);
if (!empty($matchA) && !empty($matchB)) {
if ($matchA[1] !== $matchB[1]) {
return ($matchA[1] < $matchB[1]) ? -1 : 1;
}
return ($matchA[2] < $matchB[2]) ? -1 : 1;
}
return (basename($a) < basename($b)) ? -1 : 1;
});
usort($files, $this->sortMigrations(...));

$migrations = [];

Expand Down

0 comments on commit d404998

Please sign in to comment.