Skip to content

Commit cae91f3

Browse files
Refactor PostgresExtension metadata (#729)
1 parent d644272 commit cae91f3

12 files changed

Lines changed: 210 additions & 171 deletions

src/EFCore.PG/Design/Internal/NpgsqlAnnotationCodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public override MethodCallCodeFragment GenerateFluentApi(IModel model, IAnnotati
6767
Check.NotNull(model, nameof(model));
6868
Check.NotNull(annotation, nameof(annotation));
6969

70-
if (annotation.Name.StartsWith(NpgsqlAnnotationNames.PostgresExtensionPrefix))
70+
if (annotation.Name.StartsWith(NpgsqlAnnotationNames.PostgresExtensionPrefix, StringComparison.Ordinal))
7171
{
7272
var extension = new PostgresExtension(model, annotation.Name);
7373

src/EFCore.PG/Extensions/NpgsqlModelBuilderExtensions.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,53 @@ public static ModelBuilder ForNpgsqlUseIdentityColumns(
147147

148148
#region Extensions
149149

150+
/// <summary>
151+
/// Registers a PostgreSQL extension in the model.
152+
/// </summary>
153+
/// <param name="modelBuilder">The model builder in which to define the extension.</param>
154+
/// <param name="schema">The schema in which to create the extension.</param>
155+
/// <param name="name">The name of the extension to create.</param>
156+
/// <param name="version">The version of the extension.</param>
157+
/// <returns>
158+
/// The updated <see cref="ModelBuilder"/>.
159+
/// </returns>
160+
/// <remarks>
161+
/// See: https://www.postgresql.org/docs/current/external-extensions.html
162+
/// </remarks>
163+
/// <exception cref="ArgumentNullException"><paramref name="modelBuilder"/></exception>
164+
[NotNull]
150165
public static ModelBuilder HasPostgresExtension(
151166
[NotNull] this ModelBuilder modelBuilder,
152-
[NotNull] string name)
167+
[CanBeNull] string schema,
168+
[NotNull] string name,
169+
[CanBeNull] string version)
153170
{
154171
Check.NotNull(modelBuilder, nameof(modelBuilder));
172+
Check.NullButNotEmpty(schema, nameof(schema));
155173
Check.NotEmpty(name, nameof(name));
156174

157-
modelBuilder.Model.Npgsql().GetOrAddPostgresExtension(name);
175+
modelBuilder.Model.Npgsql().GetOrAddPostgresExtension(schema, name, version);
158176
return modelBuilder;
159177
}
160178

179+
/// <summary>
180+
/// Registers a PostgreSQL extension in the model.
181+
/// </summary>
182+
/// <param name="modelBuilder">The model builder in which to define the extension.</param>
183+
/// <param name="name">The name of the extension to create.</param>
184+
/// <returns>
185+
/// The updated <see cref="ModelBuilder"/>.
186+
/// </returns>
187+
/// <remarks>
188+
/// See: https://www.postgresql.org/docs/current/external-extensions.html
189+
/// </remarks>
190+
/// <exception cref="ArgumentNullException"><paramref name="modelBuilder"/></exception>
191+
[NotNull]
192+
public static ModelBuilder HasPostgresExtension(
193+
[NotNull] this ModelBuilder modelBuilder,
194+
[NotNull] string name)
195+
=> modelBuilder.HasPostgresExtension(null, name, null);
196+
161197
#endregion
162198

163199
#region Enums

src/EFCore.PG/Metadata/INpgsqlModelAnnotations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface INpgsqlModelAnnotations : IRelationalModelAnnotations
1414

1515
[NotNull]
1616
[ItemNotNull]
17-
IReadOnlyList<IPostgresExtension> PostgresExtensions { get; }
17+
IReadOnlyList<PostgresExtension> PostgresExtensions { get; }
1818

1919
[NotNull]
2020
[ItemNotNull]

src/EFCore.PG/Metadata/IPostgresExtension.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/EFCore.PG/Metadata/NpgsqlAlterDatabaseOperationAnnotations.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public class NpgsqlAlterDatabaseOperationAnnotations : RelationalAnnotations
1616

1717
[NotNull]
1818
[ItemNotNull]
19-
public virtual IReadOnlyList<IPostgresExtension> PostgresExtensions
19+
public virtual IReadOnlyList<PostgresExtension> PostgresExtensions
2020
=> PostgresExtension.GetPostgresExtensions(Metadata).ToArray();
2121

2222
[NotNull]
2323
[ItemNotNull]
24-
public virtual IReadOnlyList<IPostgresExtension> OldPostgresExtensions
24+
public virtual IReadOnlyList<PostgresExtension> OldPostgresExtensions
2525
=> PostgresExtension.GetPostgresExtensions(_oldDatabase).ToArray();
2626

2727
[NotNull]
@@ -48,5 +48,12 @@ public virtual IReadOnlyList<PostgresRange> OldPostgresRanges
4848
public NpgsqlAlterDatabaseOperationAnnotations([NotNull] AlterDatabaseOperation operation)
4949
: base(operation)
5050
=> _oldDatabase = operation.OldDatabase;
51+
52+
[NotNull]
53+
public virtual PostgresExtension GetOrAddPostgresExtension(
54+
[CanBeNull] string schema,
55+
[NotNull] string name,
56+
[CanBeNull] string version)
57+
=> PostgresExtension.GetOrAddPostgresExtension((IMutableAnnotatable)Metadata, schema, name, version);
5158
}
5259
}

src/EFCore.PG/Metadata/NpgsqlDatabaseModelAnnotations.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class NpgsqlDatabaseModelAnnotations : RelationalAnnotations
1010
{
1111
[NotNull]
1212
[ItemNotNull]
13-
public virtual IReadOnlyList<IPostgresExtension> PostgresExtensions
13+
public virtual IReadOnlyList<PostgresExtension> PostgresExtensions
1414
=> PostgresExtension.GetPostgresExtensions(Metadata).ToArray();
1515

1616
[NotNull]
@@ -25,5 +25,12 @@ public virtual IReadOnlyList<PostgresRange> PostgresRanges
2525

2626
/// <inheritdoc />
2727
public NpgsqlDatabaseModelAnnotations([NotNull] DatabaseModel model) : base(model) {}
28+
29+
[NotNull]
30+
public virtual PostgresExtension GetOrAddPostgresExtension(
31+
[CanBeNull] string schema,
32+
[NotNull] string name,
33+
[CanBeNull] string version)
34+
=> PostgresExtension.GetOrAddPostgresExtension((IMutableAnnotatable)Metadata, schema, name, version);
2835
}
2936
}

src/EFCore.PG/Metadata/NpgsqlModelAnnotations.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ protected virtual bool SetValueGenerationStrategy(NpgsqlValueGenerationStrategy?
6464

6565
#region PostgreSQL Extensions
6666

67-
public virtual PostgresExtension GetOrAddPostgresExtension([NotNull] string name)
68-
=> PostgresExtension.GetOrAddPostgresExtension((IMutableModel)Model, name);
67+
[NotNull]
68+
public virtual PostgresExtension GetOrAddPostgresExtension(
69+
[CanBeNull] string schema,
70+
[NotNull] string name,
71+
[CanBeNull] string version)
72+
=> PostgresExtension.GetOrAddPostgresExtension((IMutableAnnotatable)Model, schema, name, version);
6973

70-
public virtual IReadOnlyList<IPostgresExtension> PostgresExtensions
74+
public virtual IReadOnlyList<PostgresExtension> PostgresExtensions
7175
=> PostgresExtension.GetPostgresExtensions(Model).ToArray();
7276

7377
#endregion

0 commit comments

Comments
 (0)