-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaAwareCSharpMigrationsGenerator.cs
More file actions
112 lines (98 loc) · 4.33 KB
/
SchemaAwareCSharpMigrationsGenerator.cs
File metadata and controls
112 lines (98 loc) · 4.33 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
using System;
using System.Collections.Generic;
using System.Linq;
using DataContextContainer;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Utilities;
namespace Microsoft.EntityFrameworkCore.Migrations.Design
{
public class SchemaAwareCSharpMigrationsGenerator : CSharpMigrationsGenerator
{
private readonly ICSharpHelper _code;
private readonly ICSharpMigrationOperationGenerator _operationGenerator;
private readonly ISchemaNameProvider schemaNameProvider;
public SchemaAwareCSharpMigrationsGenerator(MigrationsCodeGeneratorDependencies dependencies,
CSharpMigrationsGeneratorDependencies csharpDependencies,
ISchemaNameProvider schemaNameProvider) : base(dependencies, csharpDependencies)
{
_code = csharpDependencies.CSharpHelper;
_operationGenerator = csharpDependencies.CSharpMigrationOperationGenerator;
this.schemaNameProvider = schemaNameProvider;
Console.WriteLine("----- SCHEMA AWARE MIGRATIONS GENERATOR ------");
Console.WriteLine($"Generating Migrations for {this.schemaNameProvider.SchemaName}");
}
public override string GenerateMigration(string migrationNamespace,
string migrationName,
IReadOnlyList<MigrationOperation> upOperations,
IReadOnlyList<MigrationOperation> downOperations)
{
var builder = new IndentedStringBuilder();
var isPublic = this.schemaNameProvider.SchemaName == "public";
var namespaces = new List<string>
{
"System",
"System.Collections.Generic",
"Microsoft.EntityFrameworkCore.Migrations"
};
namespaces.AddRange(GetNamespaces(upOperations.Concat(downOperations)));
foreach (var n in namespaces.Distinct())
{
builder
.Append("using ")
.Append(n)
.AppendLine(";");
}
builder
.AppendLine()
.Append("namespace ").AppendLine(_code.Namespace(migrationNamespace))
.AppendLine("{");
using (builder.Indent())
{
builder
.Append("public partial class ").Append(_code.Identifier(migrationName)).Append(" : Migration");
if (!isPublic)
{
builder.AppendLine(", IHaveSchemaName");
}
else
{
builder.AppendLine();
}
builder.AppendLine("{");
using (builder.Indent())
{
if (!isPublic)
{
builder.AppendLine($"public string SchemaName {{ get; set; }} = \"{this.schemaNameProvider.SchemaName}\";");
}
builder.AppendLine();
builder
.AppendLine("protected override void Up(MigrationBuilder migrationBuilder)")
.AppendLine("{");
using (builder.Indent())
{
_operationGenerator.Generate("migrationBuilder", upOperations, builder);
}
builder
.AppendLine()
.AppendLine("}")
.AppendLine()
.AppendLine("protected override void Down(MigrationBuilder migrationBuilder)")
.AppendLine("{");
using (builder.Indent())
{
_operationGenerator.Generate("migrationBuilder", downOperations, builder);
}
builder
.AppendLine()
.AppendLine("}");
}
builder.AppendLine("}");
}
builder.AppendLine("}");
return builder.ToString();
}
}
}