Skip to content
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

New EF Core comment support #950

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ public override MethodCallCodeFragment GenerateFluentApi(IEntityType entityType,
Check.NotNull(entityType, nameof(entityType));
Check.NotNull(annotation, nameof(annotation));

#pragma warning disable 618
if (annotation.Name == NpgsqlAnnotationNames.Comment)
return new MethodCallCodeFragment(nameof(NpgsqlEntityTypeBuilderExtensions.ForNpgsqlHasComment), annotation.Value);
#pragma warning restore 618

if (annotation.Name == NpgsqlAnnotationNames.UnloggedTable)
return new MethodCallCodeFragment(nameof(NpgsqlEntityTypeBuilderExtensions.IsUnlogged), annotation.Value);
Expand Down Expand Up @@ -150,8 +152,10 @@ public override MethodCallCodeFragment GenerateFluentApi(IProperty property, IAn
throw new ArgumentOutOfRangeException();
}

#pragma warning disable 618
case NpgsqlAnnotationNames.Comment:
return new MethodCallCodeFragment(nameof(NpgsqlPropertyBuilderExtensions.ForNpgsqlHasComment), annotation.Value);
#pragma warning restore 618
}

return null;
Expand Down
68 changes: 33 additions & 35 deletions src/EFCore.PG/Extensions/NpgsqlEntityTypeBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,41 +130,6 @@ public static bool ForNpgsqlCanSetStorageParameter(

#endregion Storage parameters

#region Comment

/// <summary>
/// Configures the comment set on the table when targeting Npgsql.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param>
/// <param name="comment"> The name of the table. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static EntityTypeBuilder ForNpgsqlHasComment(
[NotNull] this EntityTypeBuilder entityTypeBuilder,
[CanBeNull] string comment)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
Check.NullButNotEmpty(comment, nameof(comment));

entityTypeBuilder.Metadata.SetNpgsqlComment(comment);

return entityTypeBuilder;
}

/// <summary>
/// Configures the comment set on the table when targeting Npgsql.
/// </summary>
/// <typeparam name="TEntity"> The entity type being configured. </typeparam>
/// <param name="entityTypeBuilder"> The builder for the entity type being configured. </param>
/// <param name="comment"> The name of the table. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static EntityTypeBuilder<TEntity> ForNpgsqlHasComment<TEntity>(
[NotNull] this EntityTypeBuilder<TEntity> entityTypeBuilder,
[CanBeNull] string comment)
where TEntity : class
=> (EntityTypeBuilder<TEntity>)ForNpgsqlHasComment((EntityTypeBuilder)entityTypeBuilder, comment);

#endregion Comment

#region Unlogged Table

/// <summary>
Expand Down Expand Up @@ -395,6 +360,39 @@ public static EntityTypeBuilder<TEntity> ForCockroachDbInterleaveInParent<TEntit
where TEntity : class
=> entityTypeBuilder.UseCockroachDbInterleaveInParent(parentTableType, interleavePrefix);

/// <summary>
/// Configures the comment set on the table when targeting Npgsql.
/// </summary>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="comment">The name of the table.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[Obsolete("Use HasComment")]
public static EntityTypeBuilder ForNpgsqlHasComment(
[NotNull] this EntityTypeBuilder entityTypeBuilder,
[CanBeNull] string comment)
{
Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
Check.NullButNotEmpty(comment, nameof(comment));

entityTypeBuilder.Metadata.SetNpgsqlComment(comment);

return entityTypeBuilder;
}

/// <summary>
/// Configures the comment set on the table when targeting Npgsql.
/// </summary>
/// <typeparam name="TEntity">The entity type being configured.</typeparam>
/// <param name="entityTypeBuilder"> The builder for the entity type being configured.</param>
/// <param name="comment">The name of the table.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[Obsolete("Use HasComment")]
public static EntityTypeBuilder<TEntity> ForNpgsqlHasComment<TEntity>(
[NotNull] this EntityTypeBuilder<TEntity> entityTypeBuilder,
[CanBeNull] string comment)
where TEntity : class
=> (EntityTypeBuilder<TEntity>)ForNpgsqlHasComment((EntityTypeBuilder)entityTypeBuilder, comment);

#endregion Obsolete
}
}
19 changes: 13 additions & 6 deletions src/EFCore.PG/Extensions/NpgsqlEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
Expand Down Expand Up @@ -30,12 +31,6 @@ public static void SetNpgsqlStorageParameter([NotNull] this IMutableEntityType e
public static void SetNpgsqlStorageParameter([NotNull] this IConventionEntityType entityType, string parameterName, object parameterValue, bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(NpgsqlAnnotationNames.StorageParameterPrefix + parameterName, parameterValue, fromDataAnnotation);

public static string GetNpgsqlComment([NotNull] this IEntityType entityType)
=> (string)entityType[NpgsqlAnnotationNames.Comment];

public static void SetNpgsqlComment([NotNull] this IMutableEntityType entityType, [CanBeNull] string comment)
=> entityType.SetOrRemoveAnnotation(NpgsqlAnnotationNames.Comment, comment);

public static bool GetNpgsqlIsUnlogged([NotNull] this IEntityType entityType)
=> entityType[NpgsqlAnnotationNames.UnloggedTable] as bool? ?? false;

Expand All @@ -47,5 +42,17 @@ public static void SetNpgsqlIsUnlogged([NotNull] this IConventionEntityType enti

public static CockroachDbInterleaveInParent GetNpgsqlCockroachDbInterleaveInParent([NotNull] this IEntityType entityType)
=> new CockroachDbInterleaveInParent(entityType);

#region Obsolete

[Obsolete("Replaced by built-in EF Core support, use HasComment on EntityTypeBuilder")]
public static string GetNpgsqlComment([NotNull] this IEntityType entityType)
=> (string)entityType[NpgsqlAnnotationNames.Comment];

[Obsolete("Replaced by built-in EF Core support, use HasComment on EntityTypeBuilder")]
public static void SetNpgsqlComment([NotNull] this IMutableEntityType entityType, [CanBeNull] string comment)
=> entityType.SetOrRemoveAnnotation(NpgsqlAnnotationNames.Comment, comment);

#endregion Obsolete
}
}
64 changes: 31 additions & 33 deletions src/EFCore.PG/Extensions/NpgsqlPropertyBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,39 +394,6 @@ public static bool CanSetValueGenerationStrategy(

#endregion General value generation strategy

#region Comment

/// <summary>
/// Configures the comment set on the column when targeting Npgsql.
/// </summary>
/// <param name="propertyBuilder"> The builder for the property being configured.</param>
/// <param name="comment"> The comment of the column.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder ForNpgsqlHasComment(
[NotNull] this PropertyBuilder propertyBuilder,
[CanBeNull] string comment)
{
Check.NotNull(propertyBuilder, nameof(propertyBuilder));
Check.NullButNotEmpty(comment, nameof(comment));

propertyBuilder.Metadata.SetNpgsqlComment(comment);

return propertyBuilder;
}

/// <summary>
/// Configures the comment set on the column when targeting Npgsql.
/// </summary>
/// <param name="propertyBuilder"> The builder for the property being configured.</param>
/// <param name="comment"> The comment of the column.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder<TEntity> ForNpgsqlHasComment<TEntity>(
[NotNull] this PropertyBuilder<TEntity> propertyBuilder,
[CanBeNull] string comment)
=> (PropertyBuilder<TEntity>)ForNpgsqlHasComment((PropertyBuilder)propertyBuilder, comment);

#endregion Comment

#region Obsolete

/// <summary>
Expand Down Expand Up @@ -572,6 +539,37 @@ public static PropertyBuilder<TProperty> UseNpgsqlIdentityColumn<TProperty>(
[NotNull] this PropertyBuilder<TProperty> propertyBuilder)
=> propertyBuilder.UseIdentityColumn();

/// <summary>
/// Configures the comment set on the column when targeting Npgsql.
/// </summary>
/// <param name="propertyBuilder"> The builder for the property being configured.</param>
/// <param name="comment"> The comment of the column.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[Obsolete("Use HasComment")]
public static PropertyBuilder ForNpgsqlHasComment(
[NotNull] this PropertyBuilder propertyBuilder,
[CanBeNull] string comment)
{
Check.NotNull(propertyBuilder, nameof(propertyBuilder));
Check.NullButNotEmpty(comment, nameof(comment));

propertyBuilder.Metadata.SetNpgsqlComment(comment);

return propertyBuilder;
}

/// <summary>
/// Configures the comment set on the column when targeting Npgsql.
/// </summary>
/// <param name="propertyBuilder"> The builder for the property being configured.</param>
/// <param name="comment"> The comment of the column.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
[Obsolete("Use HasComment")]
public static PropertyBuilder<TEntity> ForNpgsqlHasComment<TEntity>(
[NotNull] this PropertyBuilder<TEntity> propertyBuilder,
[CanBeNull] string comment)
=> (PropertyBuilder<TEntity>)ForNpgsqlHasComment((PropertyBuilder)propertyBuilder, comment);

#endregion Obsolete
}
}
6 changes: 4 additions & 2 deletions src/EFCore.PG/Extensions/NpgsqlPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,16 @@ static bool IsIntegerForValueGeneration(this Type type)

#endregion Value generation

#region Comment
#region Obsolete

[Obsolete("Use HasComment")]
public static string GetNpgsqlComment([NotNull] this IProperty property)
=> (string)property[NpgsqlAnnotationNames.Comment];

[Obsolete("Use HasComment")]
public static void SetNpgsqlComment([NotNull] this IMutableProperty property, [CanBeNull] string comment)
=> property.SetOrRemoveAnnotation(NpgsqlAnnotationNames.Comment, comment);

#endregion Comment
#endregion Obsolete
}
}
4 changes: 3 additions & 1 deletion src/EFCore.PG/Metadata/Internal/NpgsqlAnnotationNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public static class NpgsqlAnnotationNames
public const string DatabaseTemplate = Prefix + "DatabaseTemplate";
public const string Tablespace = Prefix + "Tablespace";
public const string StorageParameterPrefix = Prefix + "StorageParameter:";
public const string Comment = Prefix + "Comment";
public const string UnloggedTable = Prefix + "UnloggedTable";

// Database model annotations
Expand All @@ -45,5 +44,8 @@ public static class NpgsqlAnnotationNames

[Obsolete("Replaced by ValueGenerationStrategy.SerialColumn")]
public const string ValueGeneratedOnAdd = Prefix + "ValueGeneratedOnAdd";

[Obsolete("Replaced by built-in EF Core support, use HasComment on EntityTypeBuilder")]
public const string Comment = Prefix + "Comment";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public NpgsqlMigrationsAnnotationProvider([NotNull] MigrationsAnnotationProvider

public override IEnumerable<IAnnotation> For(IEntityType entityType)
{
#pragma warning disable 618
if (entityType.GetNpgsqlComment() is string comment)
yield return new Annotation(NpgsqlAnnotationNames.Comment, comment);
#pragma warning restore 618
if (entityType.GetNpgsqlIsUnlogged())
yield return new Annotation(NpgsqlAnnotationNames.UnloggedTable, entityType.GetNpgsqlIsUnlogged());
if (entityType[CockroachDbAnnotationNames.InterleaveInParent] != null)
Expand All @@ -35,8 +37,10 @@ public override IEnumerable<IAnnotation> For(IProperty property)
{
if (property.GetNpgsqlValueGenerationStrategy() is NpgsqlValueGenerationStrategy npgsqlValueGenerationStrategy)
yield return new Annotation(NpgsqlAnnotationNames.ValueGenerationStrategy, npgsqlValueGenerationStrategy);
#pragma warning disable 618
if (property.GetNpgsqlComment() is string comment)
yield return new Annotation(NpgsqlAnnotationNames.Comment, comment);
#pragma warning restore 618
}

public override IEnumerable<IAnnotation> For(IIndex index)
Expand Down
Loading