Skip to content

Reset a migration's state before executing it - #1603

Open
ChrisAbner wants to merge 1 commit into
doctrine:3.10.xfrom
ChrisAbner:fix/rerun-same-migration
Open

ChrisAbner wants to merge 1 commit into
doctrine:3.10.xfrom
ChrisAbner:fix/rerun-same-migration

Conversation

@ChrisAbner

Copy link
Copy Markdown
Q A
Type bug
BC Break no
Fixed issues #1593

Summary

FilesystemMigrationsRepository keeps one instance per migration, and DbalExecutor freezes that instance after collecting its SQL. Nothing ever unfreezes it or clears its planned SQL, so when one process runs the same migration a second time (migrate and then migrate first, as in #1593, or the pipeline in this comment on #1440), addSql() throws FrozenMigration. Without the freeze, the SQL from the previous run would be executed again, which is what #1593 describes. This PR adds AbstractMigration::reset(), which clears the planned SQL and unfreezes the migration, and calls it at the start of DbalExecutor::executeMigration().

The reset happens before preUp()/preDown(), so SQL added there is kept, and the migration is still frozen before postUp()/postDown(): addSql() in a post method keeps throwing, on every run. Resetting at the start rather than in a finally after the execution keeps the change to a few lines, and also covers a run that failed halfway. Both would need a new public method, since plannedSql and frozen are private. The method is the one @Tofandel proposed in the issue (their patch-1 branch), so they are credited as co-author.

Verification. A script that runs migrations:migrate four times in one console application, on SQLite in memory, with a migration that creates a table in up() and drops it in down():

Command Before After
migrate latest exit 0, table created exit 0, table created
migrate first exit 1, FrozenMigration, table still there exit 0, table dropped
migrate latest exit 0 exit 0, table created
migrate first exit 1, FrozenMigration exit 0, table dropped
  • New ExecutorTest::testExecuteSameMigrationUpThenDown and AbstractMigrationTest::testResetClearsSqlAndUnfreezes fail without the change and pass with it.
  • A migration that calls addSql() in postUp() still fails with FrozenMigration, on the first and on the second run.
  • Full PHPUnit suite on PHP 8.4 and 8.5 (434 tests), PHPCS, PHPStan and roave/backward-compatibility-check pass.

Not covered: I did not run the suite against the lowest dependencies or PHP 8.1. A subclass that already declares a reset() method with a different signature would conflict with the new method. The BC check does not report methods added to open classes, and I could not find such a subclass in public code, but it is possible.

The migrations repository reuses its migration instances, so running
the same migration again in one process (for instance migrate, then
migrate first) hit the frozen state from the previous run and threw
FrozenMigration. Its planned SQL was never cleared either.

Co-authored-by: Adrien Foulon <6115458+Tofandel@users.noreply.github.com>
@greg0ire
greg0ire changed the base branch from 3.9.x to 3.10.x November 24, 2026 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant