Skip to content
Draft
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
117 changes: 60 additions & 57 deletions src/EFCore.PG/Design/Internal/NpgsqlAnnotationCodeGenerator.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -295,23 +295,18 @@ public static ModelBuilder UseKeySequences(
/// <param name="modelBuilder">The model builder in which to define the extension.</param>
/// <param name="schema">The schema in which to create the extension.</param>
/// <param name="name">The name of the extension to create.</param>
/// <param name="version">The version of the extension.</param>
/// <param name="version">The extension version. Defaults to the latest installed, and is very rarely needed.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/external-extensions.html
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="modelBuilder"/></exception>
public static ModelBuilder HasPostgresExtension(
this ModelBuilder modelBuilder,
string? schema,
string name,
string? version = null)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NullButNotEmpty(schema, nameof(schema));
Check.NotEmpty(name, nameof(name));

modelBuilder.Model.GetOrAddPostgresExtension(schema, name, version);
HasPostgresExtension(modelBuilder.Model, name, schema, version, ConfigurationSource.Explicit);

return modelBuilder;
}
Expand All @@ -325,11 +320,12 @@ public static ModelBuilder HasPostgresExtension(
/// <remarks>
/// See: https://www.postgresql.org/docs/current/external-extensions.html
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="modelBuilder"/></exception>
public static ModelBuilder HasPostgresExtension(
this ModelBuilder modelBuilder,
string name)
=> modelBuilder.HasPostgresExtension(null, name);
public static ModelBuilder HasPostgresExtension(this ModelBuilder modelBuilder, string name)
{
HasPostgresExtension(modelBuilder.Model, name, schema: null, version: null, ConfigurationSource.Explicit);

return modelBuilder;
}

/// <summary>
/// Registers a PostgreSQL extension in the model.
Expand All @@ -344,63 +340,41 @@ public static ModelBuilder HasPostgresExtension(
/// See: https://www.postgresql.org/docs/current/external-extensions.html
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="modelBuilder"/></exception>
public static IConventionModelBuilder? HasPostgresExtension(
public static IConventionModelBuilder HasPostgresExtension(
this IConventionModelBuilder modelBuilder,
string? schema,
string name,
string? schema = null,
string? version = null,
bool fromDataAnnotation = false)
{
if (modelBuilder.CanSetPostgresExtension(schema, name, version, fromDataAnnotation))
{
modelBuilder.Metadata.GetOrAddPostgresExtension(schema, name, version);
return modelBuilder;
}
HasPostgresExtension(
(IMutableModel)modelBuilder.Metadata,
name,
schema,
version,
fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);

return null;
return modelBuilder;
}

/// <summary>
/// Registers a PostgreSQL extension in the model.
/// </summary>
/// <param name="modelBuilder">The model builder in which to define the extension.</param>
/// <param name="name">The name of the extension to create.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/external-extensions.html
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="modelBuilder"/></exception>
public static IConventionModelBuilder? HasPostgresExtension(
this IConventionModelBuilder modelBuilder,
private static void HasPostgresExtension(
IMutableModel model,
string name,
bool fromDataAnnotation = false)
=> modelBuilder.HasPostgresExtension(schema: null, name, version: null, fromDataAnnotation);

/// <summary>
/// Returns a value indicating whether the given PostgreSQL extension can be registered in the model.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and SQL Azure databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="modelBuilder">The model builder.</param>
/// <param name="schema">The schema in which to create the extension.</param>
/// <param name="name">The name of the extension to create.</param>
/// <param name="version">The version of the extension.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the given value can be set as the default increment for SQL Server IDENTITY.</returns>
public static bool CanSetPostgresExtension(
this IConventionModelBuilder modelBuilder,
string? schema,
string name,
string? version = null,
bool fromDataAnnotation = false)
string? version,
ConfigurationSource configurationSource)
{
var annotationName = PostgresExtension.BuildAnnotationName(schema, name);
Check.NullButNotEmpty(schema, nameof(schema));
Check.NotEmpty(name, nameof(name));

var postgresExtension = (PostgresExtension?)PostgresExtension.FindPostgresExtension(model, name, schema);
if (postgresExtension is not null)
{
postgresExtension.UpdateConfigurationSource(configurationSource);
return;
}

return modelBuilder.CanSetAnnotation(annotationName, $"{schema},{name},{version}", fromDataAnnotation);
PostgresExtension.AddPostgresExtension(model, name, schema, version, configurationSource);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,35 +271,9 @@ public static void SetValueGenerationStrategy(this IMutableModel model, NpgsqlVa
/// 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 static PostgresExtension GetOrAddPostgresExtension(
this IMutableModel model,
string? schema,
string name,
string? version)
=> PostgresExtension.GetOrAddPostgresExtension(model, schema, name, version);

/// <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 static IReadOnlyList<PostgresExtension> GetPostgresExtensions(this IReadOnlyModel model)
public static IReadOnlyList<IPostgresExtension> GetPostgresExtensions(this IReadOnlyModel model)
=> PostgresExtension.GetPostgresExtensions(model).ToArray();

/// <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 static PostgresExtension GetOrAddPostgresExtension(
this IConventionModel model,
string? schema,
string name,
string? version)
=> PostgresExtension.GetOrAddPostgresExtension(model, schema, name, version);

#endregion

#region Enum types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,6 @@ public static IReadOnlyList<PostgresCollation> GetPostgresCollations(this AlterD
public static IReadOnlyList<PostgresCollation> GetOldPostgresCollations(this AlterDatabaseOperation operation)
=> PostgresCollation.GetCollations(operation.OldDatabase).ToArray();

/// <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 static IReadOnlyList<PostgresExtension> GetPostgresExtensions(this AlterDatabaseOperation operation)
=> PostgresExtension.GetPostgresExtensions(operation).ToArray();

/// <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 static IReadOnlyList<PostgresExtension> GetOldPostgresExtensions(this AlterDatabaseOperation operation)
=> PostgresExtension.GetPostgresExtensions(operation.OldDatabase).ToArray();

/// <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
Expand Down Expand Up @@ -79,17 +61,4 @@ public static IReadOnlyList<PostgresRange> GetPostgresRanges(this AlterDatabaseO
/// </summary>
public static IReadOnlyList<PostgresRange> GetOldPostgresRanges(this AlterDatabaseOperation operation)
=> PostgresRange.GetPostgresRanges(operation.OldDatabase).ToArray();

/// <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 static PostgresExtension GetOrAddPostgresExtension(
this AlterDatabaseOperation operation,
string? schema,
string name,
string? version)
=> PostgresExtension.GetOrAddPostgresExtension(operation, schema, name, version);
}
22 changes: 0 additions & 22 deletions src/EFCore.PG/Extensions/NpgsqlDatabaseModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,6 @@ namespace Microsoft.EntityFrameworkCore;
/// </summary>
public static class NpgsqlDatabaseModelExtensions
{
/// <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 static PostgresExtension GetOrAddPostgresExtension(
this DatabaseModel model,
string? schema,
string name,
string? version)
=> PostgresExtension.GetOrAddPostgresExtension(model, schema, name, version);

/// <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 static IReadOnlyList<PostgresExtension> GetPostgresExtensions(this DatabaseModel model)
=> PostgresExtension.GetPostgresExtensions(model).ToArray();

/// <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
Expand Down
49 changes: 0 additions & 49 deletions src/EFCore.PG/Extensions/NpgsqlMigrationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,4 @@ public static class NpgsqlMigrationBuilderExtensions
/// <returns>True if Npgsql is being used; false otherwise.</returns>
public static bool IsNpgsql(this MigrationBuilder builder)
=> builder.ActiveProvider == typeof(NpgsqlMigrationBuilderExtensions).GetTypeInfo().Assembly.GetName().Name;

/// <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 static MigrationBuilder EnsurePostgresExtension(
this MigrationBuilder builder,
string name,
string? schema = null,
string? version = null)
{
Check.NotEmpty(name, nameof(name));
Check.NullButNotEmpty(schema, nameof(schema));
Check.NullButNotEmpty(version, nameof(schema));

var op = new AlterDatabaseOperation();
op.GetOrAddPostgresExtension(schema, name, version);
builder.Operations.Add(op);

return builder;
}

/// <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>
[Obsolete("Use EnsurePostgresExtension instead")]
public static MigrationBuilder CreatePostgresExtension(
this MigrationBuilder builder,
string name,
string? schema = null,
string? version = null)
=> EnsurePostgresExtension(builder, name, schema, version);

/// <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>
[Obsolete("This no longer does anything and should be removed.")]
public static MigrationBuilder DropPostgresExtension(
this MigrationBuilder builder,
string name)
=> builder;
}
21 changes: 21 additions & 0 deletions src/EFCore.PG/Metadata/IConventionPostgresExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

/// <inheritdoc />
public interface IConventionPostgresExtension : IReadOnlyPostgresExtension
{
/// <summary>
/// Gets the <see cref="IConventionModel" /> in which this PostgreSQL extension is defined.
/// </summary>
new IConventionModel? Model { get; }

/// <summary>
/// Gets the configuration source for this <see cref="IConventionSequence" />.
/// </summary>
/// <returns>The configuration source for <see cref="IConventionSequence" />.</returns>
ConfigurationSource GetConfigurationSource();

// TODO: Schema, version?
}
15 changes: 15 additions & 0 deletions src/EFCore.PG/Metadata/IMutablePostgresExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

/// <inheritdoc />
public interface IMutablePostgresExtension : IReadOnlyPostgresExtension
{
/// <summary>
/// Gets the <see cref="IMutableModel" /> in which this PostgreSQL extension is defined.
/// </summary>
new IMutableModel? Model { get; }

// TODO: Schema?
}
13 changes: 13 additions & 0 deletions src/EFCore.PG/Metadata/IPostgresExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

/// <inheritdoc />
public interface IPostgresExtension : IReadOnlyPostgresExtension
{
/// <summary>
/// Gets the database schema that contains the extension.
/// </summary>
new IModel? Model { get; }
}
Loading