Skip to content

Commit

Permalink
allow custom sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Sep 7, 2019
1 parent 50c7d16 commit 0535cfe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
20 changes: 20 additions & 0 deletions lib/Doctrine/Migrations/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Doctrine\Migrations\QueryWriter;
use Doctrine\Migrations\Version\Version;
use function str_replace;
use function strcmp;
use function strlen;

/**
Expand Down Expand Up @@ -88,6 +89,9 @@ class Configuration
/** @var bool */
private $checkDbPlatform = true;

/** @var callable */
private $sortingStrategy;

public function __construct(
Connection $connection,
?OutputWriter $outputWriter = null,
Expand Down Expand Up @@ -128,6 +132,22 @@ public function getMigrationsTableName() : string
return $this->migrationsTableName;
}

public function setSortingStrategy(callable $sortingStrategy) : void
{
$this->sortingStrategy = $sortingStrategy;
}

public function getSortingStrategy() : callable
{
if ($this->sortingStrategy === null) {
$this->sortingStrategy = static function (Version $a, Version $b) : int {
return strcmp($a->getVersion(), $b->getVersion());
};
}

return $this->sortingStrategy;
}

public function setMigrationsColumnName(string $columnName) : void
{
$this->migrationsColumnName = $columnName;
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/Migrations/MigrationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Doctrine\Migrations\Finder\MigrationFinder;
use Doctrine\Migrations\Version\Factory;
use Doctrine\Migrations\Version\Version;
use const SORT_STRING;
use function array_diff;
use function array_keys;
use function array_map;
Expand All @@ -25,9 +24,9 @@
use function get_class;
use function implode;
use function is_array;
use function ksort;
use function sprintf;
use function substr;
use function uasort;

/**
* The MigrationRepository class is responsible for retrieving migrations, determing what the current migration
Expand Down Expand Up @@ -85,7 +84,7 @@ public function addVersion(Version $version) : void
{
$this->versions[$version->getVersion()] = $version;

ksort($this->versions, SORT_STRING);
uasort($this->versions, $this->configuration->getSortingStrategy());
}

/**
Expand Down

0 comments on commit 0535cfe

Please sign in to comment.