doctrine/DoctrineMigrationsBundle

Migrations run out of chronological order if you have multiple namespaces defined

samuel-sol opened this issue · 1 comments

Let's say you have the follow doctrine_migrations.yaml file:

doctrine_migrations:
    migrations_paths:
        'DoctrineMigrations': '%kernel.project_dir%/migrations'
        'MyBundleDoctrineMigrations': '@MyBundle/migrations'
        'MyOtherBundleDoctrineMigrations': '@MyOtherBundle/migrations'
    enable_profiler: '%kernel.debug%'

When you run the doctrine:migrations:migrate command, doctrine will check all the directories, compare with the current table and get what it has to execute. The issue is that it sorts the migrations to be executed by its full path, and not just the VersionYYYYMMDDHIS part of the class name. That means it might run migrations out of chronological order .

For example, if the command finds the following migrations to execute:

DoctrineMigrations\Version20230101000000
DoctrineMigrations\Version20230201000000
DoctrineMigrations\Version20230301000000 
DoctrineMigrations\Version20230401000000
OtherBundleDoctrineMigrations\Version20230410000000
OtherBundleDoctrineMigrations\Version20230510000000
BundleDoctrineMigrations\Version20230120000000
BundleDoctrineMigrations\Version20230220000000
BundleDoctrineMigrations\Version20230520000000

They will be executed in the following order:

BundleDoctrineMigrations\Version20230120000000
BundleDoctrineMigrations\Version20230220000000
BundleDoctrineMigrations\Version20230520000000
DoctrineMigrations\Version20230101000000
DoctrineMigrations\Version20230201000000
DoctrineMigrations\Version20230301000000 
DoctrineMigrations\Version20230401000000
OtherBundleDoctrineMigrations\Version20230410000000
OtherBundleDoctrineMigrations\Version20230510000000

When chronological it should be

DoctrineMigrations\Version20230101000000
BundleDoctrineMigrations\Version20230120000000
DoctrineMigrations\Version20230201000000
BundleDoctrineMigrations\Version20230220000000
DoctrineMigrations\Version20230301000000 
DoctrineMigrations\Version20230401000000
OtherBundleDoctrineMigrations\Version20230410000000
BundleDoctrineMigrations\Version20230520000000
OtherBundleDoctrineMigrations\Version20230510000000

This is a problem if one of your bundles depends on tables maintained by another bundle. Even if you define on your composer.json the correct version minimum req, if both packages are added on the same deploy, the migrations will break because they will not run on chronological order.