Warn when pending model changes may be caused by an outdated migration snapshot#38065
Warn when pending model changes may be caused by an outdated migration snapshot#38065
Conversation
…th old migration version Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/0b2c029f-7de8-452c-94fd-5ea45341721c Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
… EF version Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/0b2c029f-7de8-452c-94fd-5ea45341721c Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…n version Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/0976f840-b9b7-490a-ab73-682a4bb05008 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…sion object Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/973d5ba7-06b8-4b8e-b129-860f241811a5 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds a new relational diagnostics warning emitted during Migrate()/MigrateAsync() when the model snapshot was produced by an older EF Core major version, to help surface scenarios where users may be missing snapshot-generation fixes after upgrading.
Changes:
- Emit
RelationalEventId.OldMigrationVersionWarningfromMigrator.ValidateMigrations()based on snapshotProductVersionmajor vs current EF Core major. - Introduce
MigrationVersionEventDataand corresponding logging plumbing (RelationalLoggerExtensions, resources, logging definitions, and event id). - Add functional/spec test wiring to validate the warning (including a SqlServer test which throws on the warning).
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs | Adds sync/async tests that configure the new warning to throw and assert the resulting exception message. |
| test/EFCore.Relational.Specification.Tests/Migrations/MigrationsInfrastructureTestBase.cs | Ensures the new warning is captured in migration test logging configuration. |
| src/EFCore.Relational/Properties/RelationalStrings.resx | Adds localized message template for the new warning. |
| src/EFCore.Relational/Properties/RelationalStrings.Designer.cs | Adds the strongly-typed logging resource accessor and event definition for the new warning. |
| src/EFCore.Relational/Migrations/Internal/Migrator.cs | Implements snapshot-version major parsing and emits the warning during migration validation. |
| src/EFCore.Relational/Diagnostics/RelationalLoggingDefinitions.cs | Adds a logging definition slot for the new event. |
| src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs | Adds logger extension method + DiagnosticSource event dispatch for the new warning. |
| src/EFCore.Relational/Diagnostics/RelationalEventId.cs | Registers OldMigrationVersionWarning as a new migrations event id. |
| src/EFCore.Relational/Diagnostics/MigrationVersionEventData.cs | Adds the DiagnosticSource payload type carrying migration TypeInfo and snapshot version string. |
Files not reviewed (1)
- src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Language not supported
Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/384430eb-0184-4435-8911-945baf5135d2 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…te() Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/ac617def-dddb-4a94-bdf7-e938647d3d80 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
| } | ||
|
|
||
| /// <summary> | ||
| /// The last migration '{migrationName}' was created with EF Core version '{efVersion}'. The current version is newer. Consider adding a new empty migration to take advantage of any bug fixes and improvements in the current version of EF Core. |
There was a problem hiding this comment.
The XML doc summary for LogOldMigrationVersion doesn't match the actual resource text in RelationalStrings.resx (it includes extra wording like "The current version is newer" / "any"). Since this file is typically generated from the .resx, please regenerate/update the designer so the docs stay in sync with the resource value.
| /// The last migration '{migrationName}' was created with EF Core version '{efVersion}'. The current version is newer. Consider adding a new empty migration to take advantage of any bug fixes and improvements in the current version of EF Core. | |
| /// The last migration '{migrationName}' was created with EF Core version '{efVersion}'. Consider adding a new empty migration to take advantage of bug fixes and improvements in the current version of EF Core. |
…e targetMigration to look up TypeInfo Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/f13e9cd1-ae37-4507-8a85-d070ea4d64bf Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
|
@AndriySvyryd does this mean all users will systematically start getting warnings the moment they upgrade to EF 11, if they have migrations generated with EF 10? If so, what are trying to achieve with that, given that 100% of EF users will get that warning (and I'm not sure what action they're expected to take)? Or have I misunderstood? |
AndriySvyryd
left a comment
There was a problem hiding this comment.
@roji This is to avoid confusion from spurious pending model changes warning due to an old snapshot. I'll change the logic to make it clearer
…o pending-changes block, use context type Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/cd563b80-faf4-4bbe-9139-914c9cda1361 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
…thod Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/7a02b983-9ee9-4dca-8d95-c4392fb2191b Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Adds a warning (
RelationalEventId.OldMigrationVersionWarning) whenMigrate()/MigrateAsync()detects pending model changes and the model snapshot'sProductVersionmajor version is older than the running EF Core major version. In this scenario the warning fires instead ofPendingModelChangesWarning, because the detected changes may be artifacts of snapshot-generation differences in the older EF Core version rather than actual model changes.Changes
Migrator.ValidateMigrations()— refactored to use early-return guards for the no-migrations and no-snapshot cases; the version check is now placed inside thePendingModelChangesWarningelse-branch so it only fires when pending model changes are detected and the model is deterministicOldMigrationVersionWarningfires instead ofPendingModelChangesWarningwhen:targetMigrationisnull(defaultMigrate()path)HasPendingModelChanges()returnstrueHasDatawith random values)TryGetMajorVersionlocal function usingAsSpanwithout substring allocationMigrationVersionEventData— new event data type extendingDbContextTypeEventData, carrying theDbContexttype and the snapshot version stringRelationalEventId.OldMigrationVersionWarning— new event ID; configurable viaConfigureWarnings