Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Allow custom sorting (used to implement migration dependencies) #855

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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