Record latest migration ID in model snapshot as property to detect diverged migration trees#37689
Record latest migration ID in model snapshot as property to detect diverged migration trees#37689
Conversation
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
src/EFCore.Design/Migrations/Design/CSharpMigrationsGenerator.cs
Outdated
Show resolved
Hide resolved
src/EFCore.Design/Migrations/Design/CSharpMigrationsGenerator.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
src/EFCore.Design/Migrations/Design/CSharpMigrationsGenerator.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR implements a mechanism to detect diverged migration trees during source control merges by recording the latest migration ID as a property in model snapshots. When branches have different migration histories, the merge will create a conflict on this property, alerting developers to the divergence.
Changes:
- Added
LatestMigrationIdvirtual property toModelSnapshotbase class that returnsnullby default - Updated migrations code generator interfaces and implementations to accept an optional
latestMigrationIdparameter - Modified snapshot generation to emit the
LatestMigrationIdproperty override with helpful comments explaining merge conflict resolution - Updated
MigrationsScaffolderto pass the current migration ID when creating snapshots and the previous migration ID when removing migrations - Added comprehensive tests validating both the presence and absence of migration IDs in generated snapshots
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/EFCore.Relational/Infrastructure/ModelSnapshot.cs | Added virtual LatestMigrationId property returning null by default, with XML documentation pointing to migration conflicts documentation |
| src/EFCore.Design/Migrations/Design/IMigrationsCodeGenerator.cs | Added optional latestMigrationId parameter to GenerateSnapshot interface method |
| src/EFCore.Design/Migrations/Design/MigrationsCodeGenerator.cs | Added optional latestMigrationId parameter to abstract GenerateSnapshot base method |
| src/EFCore.Design/Migrations/Design/CSharpMigrationsGenerator.cs | Implemented code generation for LatestMigrationId property override with merge conflict guidance comments |
| src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs | Updated ScaffoldMigration to pass current migration ID and RemoveMigration to pass previous migration ID to snapshot generator |
| test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationsGeneratorTest.ModelSnapshot.cs | Added tests verifying snapshot generation with and without migration IDs, including compilation and runtime behavior |
|
@copilot |
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Fixed the failing tests. The MigrationTests were comparing against hardcoded expected snapshot code that didn't include the new |
IMigrationsCodeGenerator.GenerateSnapshotto accept latest migration ID parameterMigrationsCodeGenerator.GenerateSnapshotbase class to accept latest migration IDCSharpMigrationsGenerator.GenerateSnapshotto generate property with latest migration IDMigrationsScaffolder.ScaffoldMigrationto pass latest migration IDMigrationsScaffolder.RemoveMigrationto pass latest migration IDLatestMigrationIdproperty toModelSnapshotbase classChanges from Review Feedback
Changed the implementation from a model annotation to a property on the ModelSnapshot class:
modelBuilder.HasAnnotation("LatestMigrationId", "20240213120000_Migration");public override string LatestMigrationId => "20240213120000_Migration";Updated all documentation links to point to https://aka.ms/efcore-docs-migrations-conflicts for consistency.
Fixed failing tests by updating expected snapshot code to include the new
LatestMigrationIdproperty that is now generated for all migrations.This makes the migration ID directly accessible as a property on the snapshot instance while still causing merge conflicts when branches diverge.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.