-
Notifications
You must be signed in to change notification settings - Fork 256
Fix bug where annotations added by initial migration are not removed when migration is reverted. #3714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+96
−6
Merged
Fix bug where annotations added by initial migration are not removed when migration is reverted. #3714
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal; | ||
|
|
||
| internal static class NpgsqlAnnotationHelper | ||
| { | ||
| internal static bool IsRelationalModelAnnotation(IAnnotation annotation) | ||
| => annotation.Name.StartsWith(NpgsqlAnnotationNames.PostgresExtensionPrefix, StringComparison.Ordinal) | ||
| || annotation.Name.StartsWith(NpgsqlAnnotationNames.EnumPrefix, StringComparison.Ordinal) | ||
| || annotation.Name.StartsWith(NpgsqlAnnotationNames.RangePrefix, StringComparison.Ordinal) | ||
| || annotation.Name.StartsWith(NpgsqlAnnotationNames.CollationDefinitionPrefix, StringComparison.Ordinal); | ||
| } |
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
34 changes: 34 additions & 0 deletions
34
src/EFCore.PG/Migrations/Internal/NpgsqlMigrationsAnnotationProvider.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal; | ||
|
|
||
| namespace Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal; | ||
|
|
||
| /// <summary> | ||
| /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
| /// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
| /// any release. You should only use it directly in your code with extreme caution and knowing that | ||
| /// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
| /// </summary> | ||
| public class NpgsqlMigrationsAnnotationProvider : MigrationsAnnotationProvider | ||
| { | ||
| /// <summary> | ||
| /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
| /// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
| /// any release. You should only use it directly in your code with extreme caution and knowing that | ||
| /// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
| /// </summary> | ||
| #pragma warning disable EF1001 // Internal EF Core API usage. | ||
| public NpgsqlMigrationsAnnotationProvider(MigrationsAnnotationProviderDependencies dependencies) | ||
| #pragma warning restore EF1001 | ||
| : base(dependencies) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
| /// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
| /// any release. You should only use it directly in your code with extreme caution and knowing that | ||
| /// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
| /// </summary> | ||
| public override IEnumerable<IAnnotation> ForRemove(IRelationalModel model) | ||
| => model.Model.GetAnnotations().Where(NpgsqlAnnotationHelper.IsRelationalModelAnnotation); | ||
| } | ||
50 changes: 50 additions & 0 deletions
50
test/EFCore.PG.Tests/Migrations/NpgsqlMigrationsAnnotationProviderTest.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
| using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal; | ||
| using Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal; | ||
|
|
||
| namespace Npgsql.EntityFrameworkCore.PostgreSQL.Migrations; | ||
|
|
||
| public class NpgsqlMigrationsAnnotationProviderTest | ||
| { | ||
| private readonly NpgsqlMigrationsAnnotationProvider _migrationsAnnotationProvider = new(new MigrationsAnnotationProviderDependencies()); | ||
brianpursley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [Fact] | ||
| public virtual void ForRemove_returns_relational_model_annotations() | ||
| { | ||
| var modelBuilder = new ModelBuilder() | ||
| .HasPostgresExtension("test_extension") | ||
| .HasPostgresExtension("test_schema2", "test_extension2") | ||
| .HasPostgresEnum("test_enum", ["A", "B", "C"]) | ||
| .HasPostgresEnum("test_schema2", "test_enum2", ["A", "B", "C"]) | ||
| .HasPostgresRange("test_range", "test_subtype") | ||
| .HasPostgresRange("test_schema2", "test_range2", "test_subtype2") | ||
| .HasCollation("test_collation", "test_locale") | ||
| .HasCollation("test_schema2", "test_collation2", "test_locale2", "test_provider2", false); | ||
|
|
||
| // Define a sequence, so we can verify that the annotation it creates is excluded from the ForRemove() result. | ||
| modelBuilder.HasSequence("test_sequence"); | ||
|
|
||
| var model = new RelationalModel(modelBuilder.FinalizeModel()); | ||
| var allAnnotations = model.Model.GetAnnotations().ToList(); | ||
|
|
||
| var annotations = _migrationsAnnotationProvider.ForRemove(model).ToList(); | ||
|
|
||
| Assert.Equal(9, allAnnotations.Count); | ||
| Assert.Equal(8, annotations.Count); | ||
| Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.PostgresExtensionPrefix}test_extension"); | ||
| Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.PostgresExtensionPrefix}test_schema2.test_extension2"); | ||
| Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.EnumPrefix}test_enum"); | ||
| Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.EnumPrefix}test_schema2.test_enum2"); | ||
| Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.RangePrefix}test_range"); | ||
| Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.RangePrefix}test_schema2.test_range2"); | ||
| Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.CollationDefinitionPrefix}test_collation"); | ||
| Assert.Contains(annotations, a => a.Name == $"{NpgsqlAnnotationNames.CollationDefinitionPrefix}test_schema2.test_collation2"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public virtual void ForRemove_handles_no_annotations() | ||
| { | ||
| var model = new RelationalModel(new Model()); | ||
| Assert.Empty(_migrationsAnnotationProvider.ForRemove(model)); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.