-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrationProgram.cs
More file actions
135 lines (111 loc) · 5.07 KB
/
MigrationProgram.cs
File metadata and controls
135 lines (111 loc) · 5.07 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
129
130
131
132
133
134
135
/**
Copyright (c) 2016 Foundation.IO (https://github.com/foundationio). All rights reserved.
This work is licensed under the terms of the BSD license.
For a copy, see <https://opensource.org/licenses/BSD-3-Clause>.
**/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Framework.Infrastructure.Attributes;
namespace CodeGenerator
{
public static class MigrationProgram
{
public static void MainApp()
{
var fileName = Directory.GetCurrentDirectory() + "\\..\\..\\..\\Backend\\Shared\\Repository\\Migration\\AppDBMigrationSteps\\Mig_002.cs";
if (File.Exists(fileName))
{
File.Delete(fileName);
}
var sb = new StringBuilder();
var t = typeof(LogR.Common.Constants.ConfigurationConstants);
var assembly = t.Assembly;
if (assembly == null)
{
Console.WriteLine("Unabel to find the LogR.Common assembly");
return;
}
WriteHeader(ref sb);
var start = true;
foreach (var aClass in assembly.GetTypes())
{
if (!aClass.FullName.StartsWith("LogR.Common.Enums"))
continue;
if (!aClass.IsEnum)
{
sb.AppendLine(aClass.FullName + " is not enum");
continue;
}
var alst = aClass.GetCustomAttributes(typeof(TypeTableAttribute), true);
if (alst == null || alst.Length == 0)
{
sb.AppendLine(aClass.FullName + " does not have TypeTable attribute");
continue;
}
var aitem = alst[0] as TypeTableAttribute;
var tableName = aitem.TableName;
//Console.WriteLine(aClass.FullName);
var camelCaseClass = (aClass.Name.Substring(0, 1).ToLower() + aClass.Name.Substring(1));
camelCaseClass = camelCaseClass.Substring(0, camelCaseClass.Length - 1);
if (!start)
sb.AppendLine();
start = false;
sb.AppendLine($" dynamic[] {camelCaseClass}Params =");
sb.AppendLine(" {");
foreach (var enumName in System.Enum.GetNames(aClass))
{
var memberInfos = aClass.GetMember(enumName);
var enumValueMemberInfo = memberInfos.FirstOrDefault(m => m.DeclaringType == aClass);
var pKeyValue = "";
var pkeyLst = enumValueMemberInfo.GetCustomAttributes(typeof(ParentKeyAttribute), true);
if (pkeyLst != null && pkeyLst.Length > 0)
{
pKeyValue = GetPKeyValue(pkeyLst.Select(x => x as ParentKeyAttribute));
}
sb.AppendLine($" new {{ {tableName}Id = (int){aClass}.{enumName}, {tableName}Guid = Guid.NewGuid(), {tableName}Name = \"{enumName}\", {pKeyValue} CreatedDate = DateTime.UtcNow, CreatedBy = \"\" , ModifiedDate = DateTime.UtcNow , ModifiedBy = \"\" }} , ");
}
sb.AppendLine(" };");
sb.AppendLine($"");
sb.AppendLine($" InsertData(\"{tableName}\", {camelCaseClass}Params);");
}
WriteFooter(ref sb);
File.AppendAllText(fileName, sb.ToString());
Process.Start("notepad.exe", fileName);
}
static string GetPKeyValue(IEnumerable<ParentKeyAttribute> pkAttList)
{
var result = string.Join(",", pkAttList.Select(x => x.ParentKeyColumn + " = " + x.ParentKeyValue).ToArray());
return result + " , ";
}
static void WriteHeader(ref StringBuilder sb)
{
sb.AppendLine("using System;");
sb.AppendLine("using FluentMigrator;");
sb.AppendLine("using LogR.Common.Enums;");
sb.AppendLine();
sb.AppendLine("#pragma warning disable SA1028");
sb.AppendLine("#pragma warning disable S101 // Types should be named in PascalCase");
sb.AppendLine("#pragma warning disable SA1137");
sb.AppendLine("namespace LogR.Repository.Migration.Application");
sb.AppendLine("{");
sb.AppendLine(" [Migration(002)]");
sb.AppendLine(" public class Mig_002 : BaseMigration");
sb.AppendLine(" {");
sb.AppendLine(" public override void Up()");
sb.AppendLine(" {");
}
static void WriteFooter(ref StringBuilder sb)
{
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine("}");
sb.AppendLine("#pragma warning restore SA1137");
sb.AppendLine("#pragma warning restore S101 // Types should be named in PascalCase");
sb.AppendLine("#pragma warning restore SA1028");
}
}
}