/
MigrationsCodeGenerator.cs
233 lines (205 loc) · 9.56 KB
/
MigrationsCodeGenerator.cs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.EntityFrameworkCore.Utilities;
namespace Microsoft.EntityFrameworkCore.Migrations.Design
{
/// <summary>
/// Used to generate code for migrations.
/// </summary>
public abstract class MigrationsCodeGenerator : IMigrationsCodeGenerator
{
/// <summary>
/// Initializes a new instance of the <see cref="MigrationsCodeGenerator" /> class.
/// </summary>
/// <param name="dependencies"> The dependencies. </param>
protected MigrationsCodeGenerator([NotNull] MigrationsCodeGeneratorDependencies dependencies)
{
Check.NotNull(dependencies, nameof(dependencies));
Dependencies = dependencies;
}
/// <summary>
/// Gets the file extension code files should use.
/// </summary>
/// <value> The file extension. </value>
public abstract string FileExtension { get; }
/// <summary>
/// Gets the programming language supported by this service.
/// </summary>
/// <value> The language. </value>
public virtual string Language
=> null;
/// <summary>
/// Parameter object containing dependencies for this service.
/// </summary>
protected virtual MigrationsCodeGeneratorDependencies Dependencies { get; }
/// <summary>
/// Generates the migration code.
/// </summary>
/// <param name="migrationNamespace"> The migration's namespace. </param>
/// <param name="migrationName"> The migration's name. </param>
/// <param name="upOperations"> The migration's up operations. </param>
/// <param name="downOperations"> The migration's down operations. </param>
/// <returns> The migration code. </returns>
public abstract string GenerateMigration(
string migrationNamespace,
string migrationName,
IReadOnlyList<MigrationOperation> upOperations,
IReadOnlyList<MigrationOperation> downOperations);
/// <summary>
/// Generates the migration metadata code.
/// </summary>
/// <param name="migrationNamespace"> The migration's namespace. </param>
/// <param name="contextType"> The migration's <see cref="DbContext" /> type. </param>
/// <param name="migrationName"> The migration's name. </param>
/// <param name="migrationId"> The migration's ID. </param>
/// <param name="targetModel"> The migration's target model. </param>
/// <returns> The migration metadata code. </returns>
public abstract string GenerateMetadata(
string migrationNamespace,
Type contextType,
string migrationName,
string migrationId,
IModel targetModel);
/// <summary>
/// Generates the model snapshot code.
/// </summary>
/// <param name="modelSnapshotNamespace"> The model snapshot's namespace. </param>
/// <param name="contextType"> The model snapshot's <see cref="DbContext" /> type. </param>
/// <param name="modelSnapshotName"> The model snapshot's name. </param>
/// <param name="model"> The model. </param>
/// <returns> The model snapshot code. </returns>
public abstract string GenerateSnapshot(
string modelSnapshotNamespace,
Type contextType,
string modelSnapshotName,
IModel model);
/// <summary>
/// Gets the namespaces required for a list of <see cref="MigrationOperation" /> objects.
/// </summary>
/// <param name="operations"> The operations. </param>
/// <returns> The namespaces. </returns>
protected virtual IEnumerable<string> GetNamespaces([NotNull] IEnumerable<MigrationOperation> operations)
=> operations.OfType<ColumnOperation>().SelectMany(GetColumnNamespaces)
.Concat(operations.OfType<CreateTableOperation>().SelectMany(o => o.Columns).SelectMany(GetColumnNamespaces))
.Concat(
operations.OfType<InsertDataOperation>().Select(o => o.Values)
.Concat(operations.OfType<UpdateDataOperation>().SelectMany(o => new[] { o.KeyValues, o.Values }))
.Concat(operations.OfType<DeleteDataOperation>().Select(o => o.KeyValues))
.SelectMany(GetDataNamespaces))
.Concat(GetAnnotationNamespaces(GetAnnotatables(operations)));
private static IEnumerable<string> GetColumnNamespaces(ColumnOperation columnOperation)
{
foreach (var ns in columnOperation.ClrType.GetNamespaces())
{
yield return ns;
}
var alterColumnOperation = columnOperation as AlterColumnOperation;
if (alterColumnOperation?.OldColumn != null)
{
foreach (var ns in alterColumnOperation.OldColumn.ClrType.GetNamespaces())
{
yield return ns;
}
}
}
private static IEnumerable<string> GetDataNamespaces(object[,] values)
{
for (var row = 0; row < values.GetLength(0); row++)
{
for (var column = 0; column < values.GetLength(1); column++)
{
var value = values[row, column];
if (value != null)
{
foreach (var ns in value.GetType().GetNamespaces())
{
yield return ns;
}
}
}
}
}
private static IEnumerable<IAnnotatable> GetAnnotatables(IEnumerable<MigrationOperation> operations)
{
foreach (var operation in operations)
{
yield return operation;
if (operation is CreateTableOperation createTableOperation)
{
foreach (var column in createTableOperation.Columns)
{
yield return column;
}
if (createTableOperation.PrimaryKey != null)
{
yield return createTableOperation.PrimaryKey;
}
foreach (var uniqueConstraint in createTableOperation.UniqueConstraints)
{
yield return uniqueConstraint;
}
foreach (var foreignKey in createTableOperation.ForeignKeys)
{
yield return foreignKey;
}
}
}
}
/// <summary>
/// Gets the namespaces required for an <see cref="IModel" />.
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The namespaces. </returns>
protected virtual IEnumerable<string> GetNamespaces([NotNull] IModel model)
=> model.GetEntityTypes().SelectMany(
e => e.GetDeclaredProperties()
.SelectMany(p => (FindValueConverter(p)?.ProviderClrType ?? p.ClrType).GetNamespaces()))
.Concat(GetAnnotationNamespaces(GetAnnotatables(model)));
private static IEnumerable<IAnnotatable> GetAnnotatables(IModel model)
{
yield return model;
foreach (var entityType in model.GetEntityTypes())
{
yield return entityType;
foreach (var property in entityType.GetDeclaredProperties())
{
yield return property;
}
foreach (var key in entityType.GetDeclaredKeys())
{
yield return key;
}
foreach (var foreignKey in entityType.GetDeclaredForeignKeys())
{
yield return foreignKey;
}
foreach (var index in entityType.GetDeclaredIndexes())
{
yield return index;
}
}
}
private IEnumerable<string> GetAnnotationNamespaces(IEnumerable<IAnnotatable> items)
=> items.SelectMany(
i => Dependencies.AnnotationCodeGenerator.FilterIgnoredAnnotations(i.GetAnnotations())
.Where(a => a.Value != null)
.Select(a => new { Annotatable = i, Annotation = a })
.SelectMany(a => GetProviderType(a.Annotatable, a.Annotation.Value.GetType()).GetNamespaces()));
private ValueConverter FindValueConverter(IProperty property)
=> (property.FindTypeMapping()
?? Dependencies.RelationalTypeMappingSource.FindMapping(property))?.Converter;
private Type GetProviderType(IAnnotatable annotatable, Type valueType)
=> annotatable is IProperty property
&& valueType.UnwrapNullableType() == property.ClrType.UnwrapNullableType()
? FindValueConverter(property)?.ProviderClrType ?? valueType
: valueType;
}
}