-
Notifications
You must be signed in to change notification settings - Fork 256
Implement support for PG18 PERIOD #3711
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
Merged
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
191 changes: 191 additions & 0 deletions
191
src/EFCore.PG/Extensions/BuilderExtensions/NpgsqlForeignKeyBuilderExtensions.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,191 @@ | ||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
| using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal; | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
| namespace Microsoft.EntityFrameworkCore; | ||
|
|
||
| /// <summary> | ||
| /// Npgsql specific extension methods for relationship builders. | ||
| /// </summary> | ||
| public static class NpgsqlForeignKeyBuilderExtensions | ||
| { | ||
| #region Period | ||
|
|
||
| /// <summary> | ||
| /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. | ||
| /// The last column in the foreign key must be a PostgreSQL range type, and the referenced | ||
| /// principal key must have WITHOUT OVERLAPS configured. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. | ||
| /// </remarks> | ||
| /// <param name="referenceCollectionBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ReferenceCollectionBuilder WithPeriod( | ||
| this ReferenceCollectionBuilder referenceCollectionBuilder, | ||
| bool withPeriod = true) | ||
| { | ||
| Check.NotNull(referenceCollectionBuilder, nameof(referenceCollectionBuilder)); | ||
|
|
||
| referenceCollectionBuilder.Metadata.SetPeriod(withPeriod); | ||
|
|
||
| return referenceCollectionBuilder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. | ||
| /// The last column in the foreign key must be a PostgreSQL range type, and the referenced | ||
| /// principal key must have WITHOUT OVERLAPS configured. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. | ||
| /// </remarks> | ||
| /// <param name="referenceCollectionBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> | ||
| /// <typeparam name="TEntity">The principal entity type in this relationship.</typeparam> | ||
| /// <typeparam name="TRelatedEntity">The dependent entity type in this relationship.</typeparam> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ReferenceCollectionBuilder<TEntity, TRelatedEntity> WithPeriod<TEntity, TRelatedEntity>( | ||
| this ReferenceCollectionBuilder<TEntity, TRelatedEntity> referenceCollectionBuilder, | ||
| bool withPeriod = true) | ||
| where TEntity : class | ||
| where TRelatedEntity : class | ||
| => (ReferenceCollectionBuilder<TEntity, TRelatedEntity>)WithPeriod( | ||
| (ReferenceCollectionBuilder)referenceCollectionBuilder, withPeriod); | ||
|
|
||
| /// <summary> | ||
| /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. | ||
| /// The last column in the foreign key must be a PostgreSQL range type, and the referenced | ||
| /// principal key must have WITHOUT OVERLAPS configured. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. | ||
| /// </remarks> | ||
| /// <param name="referenceReferenceBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ReferenceReferenceBuilder WithPeriod( | ||
| this ReferenceReferenceBuilder referenceReferenceBuilder, | ||
| bool withPeriod = true) | ||
| { | ||
| Check.NotNull(referenceReferenceBuilder, nameof(referenceReferenceBuilder)); | ||
|
|
||
| referenceReferenceBuilder.Metadata.SetPeriod(withPeriod); | ||
|
|
||
| return referenceReferenceBuilder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. | ||
| /// The last column in the foreign key must be a PostgreSQL range type, and the referenced | ||
| /// principal key must have WITHOUT OVERLAPS configured. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. | ||
| /// </remarks> | ||
| /// <param name="referenceReferenceBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> | ||
| /// <typeparam name="TEntity">The entity type on one end of the relationship.</typeparam> | ||
| /// <typeparam name="TRelatedEntity">The entity type on the other end of the relationship.</typeparam> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ReferenceReferenceBuilder<TEntity, TRelatedEntity> WithPeriod<TEntity, TRelatedEntity>( | ||
| this ReferenceReferenceBuilder<TEntity, TRelatedEntity> referenceReferenceBuilder, | ||
| bool withPeriod = true) | ||
| where TEntity : class | ||
| where TRelatedEntity : class | ||
| => (ReferenceReferenceBuilder<TEntity, TRelatedEntity>)WithPeriod( | ||
| (ReferenceReferenceBuilder)referenceReferenceBuilder, withPeriod); | ||
|
|
||
| /// <summary> | ||
| /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. | ||
| /// The last column in the foreign key must be a PostgreSQL range type, and the referenced | ||
| /// principal key must have WITHOUT OVERLAPS configured. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. | ||
| /// </remarks> | ||
| /// <param name="ownershipBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static OwnershipBuilder WithPeriod( | ||
| this OwnershipBuilder ownershipBuilder, | ||
| bool withPeriod = true) | ||
| { | ||
| Check.NotNull(ownershipBuilder, nameof(ownershipBuilder)); | ||
|
|
||
| ownershipBuilder.Metadata.SetPeriod(withPeriod); | ||
|
|
||
| return ownershipBuilder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. | ||
| /// The last column in the foreign key must be a PostgreSQL range type, and the referenced | ||
| /// principal key must have WITHOUT OVERLAPS configured. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. | ||
| /// </remarks> | ||
| /// <param name="ownershipBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> | ||
| /// <typeparam name="TEntity">The entity type on one end of the relationship.</typeparam> | ||
| /// <typeparam name="TDependentEntity">The entity type on the other end of the relationship.</typeparam> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static OwnershipBuilder<TEntity, TDependentEntity> WithPeriod<TEntity, TDependentEntity>( | ||
| this OwnershipBuilder<TEntity, TDependentEntity> ownershipBuilder, | ||
| bool withPeriod = true) | ||
| where TEntity : class | ||
| where TDependentEntity : class | ||
| => (OwnershipBuilder<TEntity, TDependentEntity>)WithPeriod( | ||
| (OwnershipBuilder)ownershipBuilder, withPeriod); | ||
|
|
||
| /// <summary> | ||
| /// Configures the foreign key to use the PostgreSQL PERIOD feature for temporal foreign keys. | ||
| /// The last column in the foreign key must be a PostgreSQL range type, and the referenced | ||
| /// principal key must have WITHOUT OVERLAPS configured. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. | ||
| /// </remarks> | ||
| /// <param name="foreignKeyBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> | ||
| /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> | ||
| /// <returns> | ||
| /// The same builder instance if the configuration was applied, | ||
| /// <see langword="null" /> otherwise. | ||
| /// </returns> | ||
| public static IConventionForeignKeyBuilder? WithPeriod( | ||
| this IConventionForeignKeyBuilder foreignKeyBuilder, | ||
| bool? withPeriod = true, | ||
| bool fromDataAnnotation = false) | ||
| { | ||
| if (foreignKeyBuilder.CanSetPeriod(withPeriod, fromDataAnnotation)) | ||
| { | ||
| foreignKeyBuilder.Metadata.SetPeriod(withPeriod, fromDataAnnotation); | ||
|
|
||
| return foreignKeyBuilder; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns a value indicating whether PERIOD can be configured. | ||
| /// </summary> | ||
| /// <param name="foreignKeyBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="withPeriod">A value indicating whether to use PERIOD.</param> | ||
| /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> | ||
| /// <returns><see langword="true" /> if the foreign key can be configured with PERIOD.</returns> | ||
| public static bool CanSetPeriod( | ||
| this IConventionForeignKeyBuilder foreignKeyBuilder, | ||
| bool? withPeriod = true, | ||
| bool fromDataAnnotation = false) | ||
| { | ||
| Check.NotNull(foreignKeyBuilder, nameof(foreignKeyBuilder)); | ||
|
|
||
| return foreignKeyBuilder.CanSetAnnotation(NpgsqlAnnotationNames.Period, withPeriod, fromDataAnnotation); | ||
| } | ||
|
|
||
| #endregion Period | ||
| } |
61 changes: 61 additions & 0 deletions
61
src/EFCore.PG/Extensions/MetadataExtensions/NpgsqlForeignKeyExtensions.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,61 @@ | ||
| using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal; | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
| namespace Microsoft.EntityFrameworkCore; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for <see cref="IForeignKey" /> for Npgsql-specific metadata. | ||
| /// </summary> | ||
| public static class NpgsqlForeignKeyExtensions | ||
| { | ||
| #region Period | ||
|
|
||
| /// <summary> | ||
| /// Returns a value indicating whether the foreign key uses the PostgreSQL PERIOD feature for temporal foreign keys. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. | ||
| /// </remarks> | ||
| /// <param name="foreignKey">The foreign key.</param> | ||
| /// <returns><see langword="true" /> if the foreign key uses PERIOD.</returns> | ||
| public static bool? GetPeriod(this IReadOnlyForeignKey foreignKey) | ||
| => (bool?)foreignKey[NpgsqlAnnotationNames.Period]; | ||
|
|
||
| /// <summary> | ||
| /// Sets a value indicating whether the foreign key uses the PostgreSQL PERIOD feature for temporal foreign keys. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. | ||
| /// </remarks> | ||
| /// <param name="foreignKey">The foreign key.</param> | ||
| /// <param name="period">The value to set.</param> | ||
| public static void SetPeriod(this IMutableForeignKey foreignKey, bool? period) | ||
| => foreignKey.SetOrRemoveAnnotation(NpgsqlAnnotationNames.Period, period); | ||
|
|
||
| /// <summary> | ||
| /// Sets a value indicating whether the foreign key uses the PostgreSQL PERIOD feature for temporal foreign keys. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See https://www.postgresql.org/docs/current/sql-createtable.html for more information. | ||
| /// </remarks> | ||
| /// <param name="foreignKey">The foreign key.</param> | ||
| /// <param name="period">The value to set.</param> | ||
| /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> | ||
| /// <returns>The configured value.</returns> | ||
| public static bool? SetPeriod(this IConventionForeignKey foreignKey, bool? period, bool fromDataAnnotation = false) | ||
| { | ||
| foreignKey.SetOrRemoveAnnotation(NpgsqlAnnotationNames.Period, period, fromDataAnnotation); | ||
|
|
||
| return period; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the <see cref="ConfigurationSource" /> for whether the foreign key uses PERIOD. | ||
| /// </summary> | ||
| /// <param name="foreignKey">The foreign key.</param> | ||
| /// <returns>The <see cref="ConfigurationSource" />.</returns> | ||
| public static ConfigurationSource? GetPeriodConfigurationSource(this IConventionForeignKey foreignKey) | ||
| => foreignKey.FindAnnotation(NpgsqlAnnotationNames.Period)?.GetConfigurationSource(); | ||
|
|
||
| #endregion Period | ||
| } |
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
39 changes: 39 additions & 0 deletions
39
src/EFCore.PG/Metadata/Conventions/NpgsqlPeriodConvention.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,39 @@ | ||
| using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal; | ||
|
|
||
| namespace Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Conventions; | ||
|
|
||
| /// <summary> | ||
| /// A convention that sets the delete behavior to <see cref="DeleteBehavior.NoAction" /> for foreign keys with PERIOD, | ||
| /// since PostgreSQL does not support cascading deletes for temporal foreign keys. | ||
| /// </summary> | ||
| /// <param name="dependencies">Parameter object containing dependencies for this convention.</param> | ||
| /// <param name="relationalDependencies">Parameter object containing relational dependencies for this convention.</param> | ||
| public class NpgsqlPeriodConvention( | ||
| ProviderConventionSetBuilderDependencies dependencies, | ||
| RelationalConventionSetBuilderDependencies relationalDependencies) | ||
| : IForeignKeyAnnotationChangedConvention | ||
| { | ||
| /// <summary> | ||
| /// Dependencies for this service. | ||
| /// </summary> | ||
| protected virtual ProviderConventionSetBuilderDependencies Dependencies { get; } = dependencies; | ||
|
|
||
| /// <summary> | ||
| /// Relational provider-specific dependencies for this service. | ||
| /// </summary> | ||
| protected virtual RelationalConventionSetBuilderDependencies RelationalDependencies { get; } = relationalDependencies; | ||
|
|
||
| /// <inheritdoc /> | ||
| public virtual void ProcessForeignKeyAnnotationChanged( | ||
| IConventionForeignKeyBuilder relationshipBuilder, | ||
| string name, | ||
| IConventionAnnotation? annotation, | ||
| IConventionAnnotation? oldAnnotation, | ||
| IConventionContext<IConventionAnnotation> context) | ||
| { | ||
| if (name == NpgsqlAnnotationNames.Period && annotation?.Value is true) | ||
| { | ||
| relationshipBuilder.OnDelete(DeleteBehavior.NoAction); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.