Migration replay - #1040
Merged
Merged
Conversation
Migrations are normally only ever tested against a database built by migrating an empty one. Deployed databases are not like that: a deployment applies state after migrating, such as change tracking, views, or grants, and that state can make DDL that is valid against an empty database fail against a real one. SQL Server refuses to drop a primary key while change tracking is on, so a migration doing that passes every test and then fails on deploy. ReplayRecentMigrations applies the last N migrations one at a time, running a callback after each, so each migration meets the conditions a deployment gives it. Everything before the window is applied in one hop. The one at a time part is load bearing. Applying the window in a single hop and running the callback once at the end is not equivalent, because a table created inside the window never has the state applied before a later migration alters it. ChangeTrackingReplayTests covers exactly that shape, and fails if the implementation is changed to the single hop version. Throws for a database that already has migrations applied, rather than letting EF revert them by running their Down. Uses only EF Core APIs, so it carries no provider specific dependency. The tests use SQL Server because change tracking is the motivating example, but the helper itself works against any provider with migrations.
This was referenced Aug 5, 2026
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.
No description provided.