-
Notifications
You must be signed in to change notification settings - Fork 261
Fix tsvector columns over JSON-mapped navigations/complex properties #3893
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
Open
hostage2222
wants to merge
1
commit into
npgsql:main
Choose a base branch
from
hostage2222:fix_tsvector_over_json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
178 changes: 178 additions & 0 deletions
178
test/EFCore.PG.Tests/Migrations/NpgsqlAnnotationProviderTest.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,178 @@ | ||
| using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal; | ||
|
|
||
| namespace Npgsql.EntityFrameworkCore.PostgreSQL.Migrations; | ||
|
|
||
| public class NpgsqlAnnotationProviderTest | ||
| { | ||
| [Fact] | ||
| public void ForColumn_resolves_tsvector_properties_over_json_navigation() | ||
| { | ||
| var modelBuilder = NpgsqlTestHelpers.Instance.CreateConventionBuilder(); | ||
|
|
||
| modelBuilder.Entity<Blog>( | ||
| eb => | ||
| { | ||
| eb.OwnsOne(b => b.Owned).ToJson(); | ||
| eb.Property(b => b.SearchVector) | ||
| .IsGeneratedTsVectorColumn("english", nameof(Blog.Owned)); | ||
| }); | ||
|
|
||
| CheckTsVectorProperties(modelBuilder, ["Owned"]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ForColumn_resolves_tsvector_properties_over_renamed_json_navigation() | ||
| { | ||
| var modelBuilder = NpgsqlTestHelpers.Instance.CreateConventionBuilder(); | ||
|
|
||
| modelBuilder.Entity<Blog>( | ||
| eb => | ||
| { | ||
| eb.OwnsOne(b => b.Owned).ToJson("owned_renamed"); | ||
| eb.Property(b => b.SearchVector) | ||
| .IsGeneratedTsVectorColumn("english", nameof(Blog.Owned)); | ||
| }); | ||
|
|
||
| CheckTsVectorProperties(modelBuilder, ["owned_renamed"]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ForColumn_resolves_tsvector_properties_over_json_complex_property() | ||
| { | ||
| var modelBuilder = NpgsqlTestHelpers.Instance.CreateConventionBuilder(); | ||
|
|
||
| modelBuilder.Entity<Blog>().ComplexProperty(b => b.Owned).ToJson(); | ||
| modelBuilder.Entity<Blog>().Property(b => b.SearchVector).IsGeneratedTsVectorColumn("english", nameof(Blog.Owned)); | ||
|
|
||
| CheckTsVectorProperties(modelBuilder, ["Owned"]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ForColumn_resolves_tsvector_properties_over_renamed_json_complex_property() | ||
| { | ||
| var modelBuilder = NpgsqlTestHelpers.Instance.CreateConventionBuilder(); | ||
|
|
||
| modelBuilder.Entity<Blog>().ComplexProperty(b => b.Owned).ToJson("owned_renamed"); | ||
| modelBuilder.Entity<Blog>().Property(b => b.SearchVector).IsGeneratedTsVectorColumn("english", nameof(Blog.Owned)); | ||
|
|
||
| CheckTsVectorProperties(modelBuilder, ["owned_renamed"]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ForColumn_resolves_tsvector_properties_over_mixed_text_jsonb_and_json_navigation() | ||
| { | ||
| var modelBuilder = NpgsqlTestHelpers.Instance.CreateConventionBuilder(); | ||
|
|
||
| modelBuilder.Entity( | ||
| "Blogs", eb => | ||
| { | ||
| eb.Property<int>("Id"); | ||
| eb.Property<string>("Text"); | ||
| eb.Property<string>("Jsonb").HasColumnType("jsonb"); | ||
| eb.OwnsOne("Owned", "Owned").ToJson(); | ||
| eb.Property<NpgsqlTsVector>("SearchVector") | ||
| .IsGeneratedTsVectorColumn("english", "Text", "Jsonb", "Owned"); | ||
| }); | ||
|
|
||
| CheckTsVectorProperties(modelBuilder, ["Text", "Jsonb", "Owned"]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ForColumn_resolves_tsvector_properties_when_declared_on_complex_type() | ||
| { | ||
| var modelBuilder = NpgsqlTestHelpers.Instance.CreateConventionBuilder(); | ||
|
|
||
| modelBuilder.Entity<BlogWithComplexProperty>( | ||
| eb => | ||
| { | ||
| eb.ComplexProperty(b => b.ComplexProperty, cb => | ||
| { | ||
| cb.Property(cp => cp.Something).IsRequired(); | ||
| // using [nameof(ComplexProperty.Something)] leads to an exception, see #3892 | ||
| cb.Property(cp => cp.SearchVector).Metadata.SetTsVectorProperties(new string[] { nameof(ComplexProperty.Something) }); | ||
| }); | ||
| }); | ||
|
|
||
| CheckTsVectorProperties(modelBuilder, ["ComplexProperty_Something"], searchVectorName: "ComplexProperty_SearchVector"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ForColumn_throws_when_included_navigation_is_not_mapped_to_json() | ||
| { | ||
| var modelBuilder = NpgsqlTestHelpers.Instance.CreateConventionBuilder(); | ||
|
|
||
| modelBuilder.Entity( | ||
| "Blogs", eb => | ||
| { | ||
| eb.Property<int>("Id"); | ||
| eb.OwnsOne("Owned", "Owned"); | ||
| eb.Property<NpgsqlTsVector>("SearchVector") | ||
| .IsGeneratedTsVectorColumn("english", "Owned"); | ||
| }); | ||
|
|
||
| var exception = Assert.Throws<InvalidOperationException>(() => | ||
| { | ||
| var model = modelBuilder.FinalizeModel(designTime: true); | ||
| model.GetRelationalModel(); | ||
| }); | ||
| Assert.Equal( | ||
| "Could not find property, navigation or complex property 'Owned' on entity type 'Blogs (Dictionary<string, object>)' for generated tsvector column", | ||
| exception.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ForColumn_throws_when_included_name_does_not_resolve() | ||
| { | ||
| var modelBuilder = NpgsqlTestHelpers.Instance.CreateConventionBuilder(); | ||
|
|
||
| modelBuilder.Entity( | ||
| "Blogs", eb => | ||
| { | ||
| eb.Property<int>("Id"); | ||
| eb.OwnsOne("Owned", "Owned"); | ||
| eb.Property<NpgsqlTsVector>("SearchVector") | ||
| .IsGeneratedTsVectorColumn("english", "Owne"); | ||
| }); | ||
|
|
||
| var exception = Assert.Throws<InvalidOperationException>(() => | ||
| { | ||
| var model = modelBuilder.FinalizeModel(designTime: true); | ||
| model.GetRelationalModel(); | ||
| }); | ||
| Assert.Equal( | ||
| "Could not find property, navigation or complex property 'Owne' on entity type 'Blogs (Dictionary<string, object>)' for generated tsvector column", | ||
| exception.Message); | ||
| } | ||
|
|
||
| private class Blog | ||
| { | ||
| public int Id { get; set; } | ||
| public Owned Owned { get; set; } | ||
| public NpgsqlTsVector SearchVector { get; set; } | ||
| } | ||
|
|
||
| private class Owned { public string Something { get; set; } } | ||
|
|
||
| private class BlogWithComplexProperty | ||
| { | ||
| public int Id { get; set; } | ||
| public ComplexProperty ComplexProperty { get; set; } | ||
| } | ||
|
|
||
| private class ComplexProperty | ||
| { | ||
| public string Something { get; set; } | ||
| public NpgsqlTsVector SearchVector { get; set; } | ||
| } | ||
|
|
||
| private void CheckTsVectorProperties(TestHelpers.TestModelBuilder modelBuilder, IReadOnlyCollection<string> expectedProperties, | ||
| string searchVectorName = "SearchVector") | ||
| { | ||
| var model = modelBuilder.FinalizeModel(designTime: true); | ||
| var relationalModel = model.GetRelationalModel(); | ||
|
|
||
| var column = Assert.Single(relationalModel.Tables.Single().Columns, c => c.Name == searchVectorName); | ||
| var tsVectorProperties = (IReadOnlyCollection<string>)column.FindAnnotation(NpgsqlAnnotationNames.TsVectorProperties)!.Value!; | ||
| Assert.Equal(expectedProperties, tsVectorProperties); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't actually an issue. TargetEntityType and ComplexType are non-nullable properties. Due to how the null-conditional operator (?.) works, if FindNavigation(p2) or FindComplexProperty(p2) returns null, the entire expression short-circuits to null.