Skip to content

Split old migration with transaction into two migrations #410

@eliot488995568

Description

@eliot488995568

Avoid Doctrine migrations mixing schema changes and data migrations

Problem

Some Symfony/Doctrine migrations mix schema changes and data migrations in the same migration file:

  • schema changes (ALTER TABLE, ADD COLUMN, DROP COLUMN, CHANGE COLUMN)
  • data migration (SELECT, UPDATE, INSERT, JSON transformations, loops in PHP, etc.)

On MySQL this can break Doctrine's transaction handling because DDL statements trigger implicit commits. When this happens, Doctrine may still try to use internal savepoints that no longer exist.

Example error encountered:

SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT DOCTRINE_10 does not exist

Observed with migration:

RZ\EventsApiModelsBundle\Migrations\Version20240213143149

Why splitting migrations helps

Splitting migrations into multiple steps avoids conflicts between MySQL implicit commits and Doctrine transactions.

Recommended structure:

  1. Schema migration

    • ADD COLUMN
    • CHANGE COLUMN
    • DROP COLUMN
    • index changes
  2. Data migration

    • SELECT
    • UPDATE
    • INSERT
    • backfilling or transforming existing data
  3. Cleanup migration (optional)

    • remove old columns once data migration is validated

Expected guideline

Define a project convention:

  • Do not mix DDL and complex data migrations in the same migration.
  • Avoid manual beginTransaction() when running ALTER TABLE.
  • Prefer small, focused migrations executed sequentially.
  • Use a separate cleanup migration when dropping legacy columns after data migration.

Benefits

  • More reliable migrations
  • Fewer transaction / savepoint issues with MySQL
  • Easier debugging and rollback
  • Clearer migration history
  • Safer data backfills
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions