Symfony Doctrine Migrations - Use transactions to wrap migrations

422 views Asked by At

What I want to do : For every migrations executed with php bin/console doctrine:migrations:migrate to be wrapped in a proper transaction (START TRANSACTION / BEGIN / ROLLBACK / ...).

My config/doctrine_migrations.yml file contains the following options :

doctrine_migrations:
    migrations_paths:
        # namespace is arbitrary but should be different from App\Migrations
        # as migrations classes should NOT be autoloaded
        'DoctrineMigrations': '%kernel.project_dir%/migrations'
    enable_profiler: false
    # Run all migrations in a transaction.
    all_or_nothing: true
    # Adds an extra check in the generated migrations to ensure that is executed on the same database type.
    check_database_platform: true
    # Whether to wrap migrations in a single transaction.
    transactional: true

And in any case the isTransactional method from AbstractMigration returns true by default.

Am I missing something here ? Does "transactions" here have another meaning than what's I'm expecting ?

I'm running symfony 6.3.7 with doctine-migrations-bundle 3.2.4 on a 5.7.29 MySQL database

2

There are 2 answers

0
user1717044 On BEST ANSWER

Well it turns out the transactions are being set as expected but MySQL autocommits when executing a DDL statement (CREATE / ALTER TABLE / ...), so as soon as a DDL statement is run in a migration file, the transaction is commited and the transactionnal mode is turned off and there is no rollback to the previous state.

2
Jakumi On

The transactional configuration value is apparently only used when generating a new migration, as can be seen in the generator, when transactional is set to false, AbstractMigration::isTransactional will be overridden to return false instead.

However as user1717044 correctly notes, MySQL doesn't allow DDL statements (statements that change the schema) in transactions and will auto-commit those instead. This makes the feature largely useless for most people.