Fix bug where annotations added by initial migration are not removed when migration is reverted.#3714
Conversation
…when migration is reverted. Adds NpgsqlMigrationsAnnotationProvider to implement ForRemove(IRelationalModel). Fixes npgsql#3604 Fixes npgsql#3183 Fixes npgsql#2514
There was a problem hiding this comment.
Pull request overview
This PR ensures that provider-specific PostgreSQL annotations added to the model (enums, ranges, extensions, collations) are surfaced to EF Core’s migrations pipeline for removal when a migration is rolled back, fixing missing DROP operations for these objects.
Changes:
- Introduces
NpgsqlAnnotationHelper.IsRelationalModelAnnotationto centralize detection of model-level PostgreSQL relational annotations (extensions, enums, ranges, collation definitions). - Adds
NpgsqlMigrationsAnnotationProviderand wires it into DI soForRemove(IRelationalModel)returns the relevant PostgreSQL annotations used to generateDROPstatements on migration rollback. - Refactors
NpgsqlAnnotationProvider.For(IRelationalModel, bool)to reuse the new helper and adds tests verifyingForRemovebehavior and annotation filtering.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/EFCore.PG/Metadata/Internal/NpgsqlAnnotationHelper.cs | Adds a reusable helper to identify Npgsql relational model annotations (extensions, enums, ranges, collations) by their annotation name prefixes. |
| src/EFCore.PG/Metadata/Internal/NpgsqlAnnotationProvider.cs | Updates the For(IRelationalModel, bool) override to use NpgsqlAnnotationHelper.IsRelationalModelAnnotation, keeping behavior while centralizing the prefix logic. |
| src/EFCore.PG/Migrations/Internal/NpgsqlMigrationsAnnotationProvider.cs | Implements a migrations annotation provider whose ForRemove(IRelationalModel) returns the Npgsql relational model annotations so that rollback migrations can drop associated database objects. |
| src/EFCore.PG/Extensions/NpgsqlServiceCollectionExtensions.cs | Registers IMigrationsAnnotationProvider → NpgsqlMigrationsAnnotationProvider in the provider’s service collection so EF Core uses the new provider during migrations. |
| test/EFCore.PG.Tests/Migrations/NpgsqlMigrationsAnnotationProviderTest.cs | Adds unit tests verifying that ForRemove returns only the expected Npgsql relational model annotations and handles the no-annotations case correctly. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
roji
left a comment
There was a problem hiding this comment.
Thanks - this looks good, just one comment on the test.
test/EFCore.PG.Tests/Migrations/NpgsqlMigrationsAnnotationProviderTest.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Adds NpgsqlMigrationsAnnotationProvider to implement ForRemove(IRelationalModel).
Fixes #3604