forked from npgsql/efcore.pg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlRuntimeModelConvention.cs
More file actions
128 lines (113 loc) · 5.31 KB
/
NpgsqlRuntimeModelConvention.cs
File metadata and controls
128 lines (113 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.ChangeTracking.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Conventions;
/// <summary>
/// A convention that creates an optimized copy of the mutable model.
/// </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 NpgsqlRuntimeModelConvention(
ProviderConventionSetBuilderDependencies dependencies,
RelationalConventionSetBuilderDependencies relationalDependencies)
: RelationalRuntimeModelConvention(dependencies, relationalDependencies)
{
/// <inheritdoc />
protected override void ProcessModelAnnotations(
Dictionary<string, object?> annotations,
IModel model,
RuntimeModel runtimeModel,
bool runtime)
{
base.ProcessModelAnnotations(annotations, model, runtimeModel, runtime);
if (!runtime)
{
annotations.Remove(NpgsqlAnnotationNames.DatabaseTemplate);
annotations.Remove(NpgsqlAnnotationNames.Tablespace);
annotations.Remove(NpgsqlAnnotationNames.CollationDefinitionPrefix);
#pragma warning disable CS0618
annotations.Remove(NpgsqlAnnotationNames.DefaultColumnCollation);
#pragma warning restore CS0618
foreach (var annotationName in annotations.Keys.Where(
k =>
k.StartsWith(NpgsqlAnnotationNames.PostgresExtensionPrefix, StringComparison.Ordinal)
|| k.StartsWith(NpgsqlAnnotationNames.EnumPrefix, StringComparison.Ordinal)
|| k.StartsWith(NpgsqlAnnotationNames.RangePrefix, StringComparison.Ordinal)))
{
annotations.Remove(annotationName);
}
}
}
/// <inheritdoc />
protected override void ProcessEntityTypeAnnotations(
Dictionary<string, object?> annotations,
IEntityType entityType,
RuntimeEntityType runtimeEntityType,
bool runtime)
{
base.ProcessEntityTypeAnnotations(annotations, entityType, runtimeEntityType, runtime);
if (!runtime)
{
annotations.Remove(NpgsqlAnnotationNames.UnloggedTable);
foreach (var annotationName in annotations.Keys.Where(
k => k.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal)))
{
annotations.Remove(annotationName);
}
}
}
/// <inheritdoc />
protected override void ProcessPropertyAnnotations(
Dictionary<string, object?> annotations,
IProperty property,
RuntimeProperty runtimeProperty,
bool runtime)
{
base.ProcessPropertyAnnotations(annotations, property, runtimeProperty, runtime);
// NpgsqlRange<T> doesn't implement IComparable (ranges are only partially ordered), so we must
// provide a custom CurrentValueComparer for the runtime model. Without this, the update pipeline's
// ModificationCommandComparer would fail when trying to sort commands by key values.
if ((property.IsKey() || property.IsForeignKey())
&& property.FindTypeMapping() is NpgsqlRangeTypeMapping)
{
#pragma warning disable EF1001 // Internal EF Core API usage.
runtimeProperty.SetCurrentValueComparer(
new EntryCurrentValueComparer(runtimeProperty, new NpgsqlRangeCurrentValueComparer(property.ClrType)));
#pragma warning restore EF1001 // Internal EF Core API usage.
}
if (!runtime)
{
annotations.Remove(NpgsqlAnnotationNames.IdentityOptions);
annotations.Remove(NpgsqlAnnotationNames.TsVectorConfig);
annotations.Remove(NpgsqlAnnotationNames.TsVectorProperties);
if (!annotations.ContainsKey(NpgsqlAnnotationNames.ValueGenerationStrategy))
{
annotations[NpgsqlAnnotationNames.ValueGenerationStrategy] = property.GetValueGenerationStrategy();
}
}
}
/// <inheritdoc />
protected override void ProcessIndexAnnotations(
Dictionary<string, object?> annotations,
IIndex index,
RuntimeIndex runtimeIndex,
bool runtime)
{
base.ProcessIndexAnnotations(annotations, index, runtimeIndex, runtime);
if (!runtime)
{
annotations.Remove(NpgsqlAnnotationNames.IndexOperators);
annotations.Remove(NpgsqlAnnotationNames.IndexSortOrder);
annotations.Remove(NpgsqlAnnotationNames.IndexNullSortOrder);
annotations.Remove(NpgsqlAnnotationNames.IndexInclude);
annotations.Remove(NpgsqlAnnotationNames.CreatedConcurrently);
annotations.Remove(NpgsqlAnnotationNames.NullsDistinct);
foreach (var annotationName in annotations.Keys.Where(
k => k.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal)))
{
annotations.Remove(annotationName);
}
}
}
}