-
-
Notifications
You must be signed in to change notification settings - Fork 388
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
Migrations organised by year and date not working on multiple different namespaces #1144
Comments
I had the same problem, but in general, not only because of the Fact is, default ordering is simply using migration names (if I remember correctly). For changing this, please see the #1074 issue, more specifically @goetas comment: #1074 (comment) which links to to a tutorial on how to write a custom VersionComparator for ordering migrations. I had the exact same issue, we run various app versions at the same time (dev, qa, preprod and prod) and when we merge altogether branches we end up with migration not respecting the version and date order, so I did that (this is Symfony code, but may be adapted easily): services:
App\Infrastructure\Doctrine\Migrations\VersionComparator:
autowire: true
doctrine_migrations:
migrations_paths:
# ...
Migration\_0_9_0: "%kernel.project_dir%/src/Persistence/System/Migration/_0_9_0"
Migration\_0_10_0: "%kernel.project_dir%/src/Persistence/System/Migration/_0_11_0"
Migration\_0_11_0: "%kernel.project_dir%/src/Persistence/System/Migration/_0_11_0"
services:
'Doctrine\Migrations\Version\Comparator': App\Infrastructure\Doctrine\Migrations\VersionComparator Then custom <?php
declare(strict_types=1);
namespace App\Infrastructure\Doctrine\Migrations;
use Doctrine\Migrations\Version\Comparator;
use Doctrine\Migrations\Version\Version;
class VersionComparator implements Comparator
{
public function compare(Version $a, Version $b): int
{
list ($aDateStamp, $aAppVersion) = $this->extractComparableString($a);
list ($bDateStamp, $bAppVersion) = $this->extractComparableString($b);
if ($aAppVersion && $bAppVersion) {
if ($aAppVersion === $bAppVersion) {
return $aDateStamp <=> $bDateStamp;
} else {
return \version_compare($aAppVersion, $bAppVersion);
}
} else {
return $aDateStamp <=> $bDateStamp;
}
}
private function extractComparableString(Version $version): array
{
$pieces = \explode('\\', (string) $version);
$dateStamp = null;
$appVersion = null;
// Default is to use time instead.
foreach ($pieces as $piece) {
if (\preg_match('/^Version\d+$/', $piece)) {
$dateStamp = \substr($piece, 7);
}
}
if (!$dateStamp) {
// Now is good enough, it's impossible in theory to have
// migrations in the future.
$dateStamp = \date('YmdHis');
}
// Exemple: Migration\\_0_6_0\\Version20210312133201
if (isset($pieces[1]) && \preg_match('/^(_|)\d+_\d+(|_\d+)$/', $pieces[1])) {
$appVersion = \trim(\str_replace('_', '.', $pieces[1]), ".");
}
return [$dateStamp, $appVersion];
}
} And that's it (the original tutorial is not clear about how to implement it correctly). This is an example, you'd have to deal with your namespaces in another way of course, but it gives you a good place to start. But, in my opinion, doctrine migrations should provide per default a comparator that uses datestamps to order, not migration names. I think this limitation probably has caused many people quite some trouble. |
@pounard thanks for providing that - put me on the right track. |
May be fixed #1421 |
Bug Report
Summary
We have a bundle that holds migrations in our Application we access those migrations and want them to be execute int he order of year and month ideally also day but ok there is no option for it.
In general the sorting looks at the namespace first and afterwards at the date
Current behavior
Upon the migrate command the migrations are first sorted by namespace and afterwards by year and month
How to reproduce
Have a Bundle with Migrations + an application executing those and it's own.
DoctrineMigrations\Version20210305101226 vs Bundle\DoctrineMigrations\Version20200110165135
Expected behavior
Migrations are executed in Order the config defines
The text was updated successfully, but these errors were encountered: