Repository navigation
Reset a migration's state before executing it - #1603
Open
ChrisAbner wants to merge 1 commit into
Open
ChrisAbner wants to merge 1 commit into
ChrisAbner wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
FilesystemMigrationsRepositorykeeps one instance per migration, andDbalExecutorfreezes 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 (migrateand thenmigrate first, as in #1593, or the pipeline in this comment on #1440),addSql()throwsFrozenMigration. Without the freeze, the SQL from the previous run would be executed again, which is what #1593 describes. This PR addsAbstractMigration::reset(), which clears the planned SQL and unfreezes the migration, and calls it at the start ofDbalExecutor::executeMigration().The reset happens before
preUp()/preDown(), so SQL added there is kept, and the migration is still frozen beforepostUp()/postDown():addSql()in a post method keeps throwing, on every run. Resetting at the start rather than in afinallyafter the execution keeps the change to a few lines, and also covers a run that failed halfway. Both would need a new public method, sinceplannedSqlandfrozenare private. The method is the one @Tofandel proposed in the issue (theirpatch-1branch), so they are credited as co-author.Verification. A script that runs
migrations:migratefour times in one console application, on SQLite in memory, with a migration that creates a table inup()and drops it indown():migrate latestmigrate firstFrozenMigration, table still theremigrate latestmigrate firstFrozenMigrationExecutorTest::testExecuteSameMigrationUpThenDownandAbstractMigrationTest::testResetClearsSqlAndUnfreezesfail without the change and pass with it.addSql()inpostUp()still fails withFrozenMigration, on the first and on the second run.roave/backward-compatibility-checkpass.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.