-
Notifications
You must be signed in to change notification settings - Fork 1
/
MigrationService.cs
209 lines (171 loc) · 7.58 KB
/
MigrationService.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
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MtdKey.OrderMaker.AppConfig;
using MtdKey.OrderMaker.Areas.Identity.Data;
using MtdKey.OrderMaker.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MtdKey.OrderMaker
{
public class MigrationService : IHostedService
{
private readonly IServiceScopeFactory serviceScopeFactory;
private readonly ILogger logger;
public MigrationService(IServiceScopeFactory serviceScopeFactory,
ILogger<MigrationService> logger)
{
this.serviceScopeFactory = serviceScopeFactory;
this.logger = logger;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = serviceScopeFactory.CreateScope();
var dbContext = scope.ServiceProvider
.GetRequiredService<OrderMakerContext>();
var idContext = scope.ServiceProvider
.GetRequiredService<IdentityDbContext>();
bool idMigration = false;
try
{
IEnumerable<string> idPending = await idContext.Database.GetPendingMigrationsAsync(cancellationToken);
idMigration = idPending.Any();
if (idMigration)
{
await idContext.Database.MigrateAsync(cancellationToken);
using var userManager = scope.ServiceProvider.GetRequiredService<UserManager<WebAppUser>>();
bool usersExists = await userManager.Users.AnyAsync(cancellationToken: cancellationToken);
if (usersExists == false)
{
using var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<WebAppRole>>();
await InitIdentity(scope.ServiceProvider, roleManager, userManager);
}
}
await MigrationHandlerAsync(dbContext);
var databaseIds = await idContext.Users
.Where(x => x.DatabaseId != Guid.Empty)
.GroupBy(x => x.DatabaseId)
.Select(x => x.Key)
.ToListAsync(cancellationToken);
foreach (var databaseId in databaseIds)
{
dbContext.SetDatabase(databaseId);
await MigrationHandlerAsync(dbContext);
}
}
catch (Exception ex)
{
string message = ex.Message;
logger.LogError("Error: {message}", message);
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public static async Task MigrationHandlerAsync(OrderMakerContext dbContext)
{
IEnumerable<string> pm = await dbContext.Database.GetPendingMigrationsAsync();
if (pm.Any())
{
await dbContext.Database.MigrateAsync();
bool existstData = await dbContext.MtdSysType.AnyAsync();
if (existstData == false)
{
await InitDatabaseAsync(dbContext);
}
}
}
private static async Task InitIdentity(IServiceProvider serviceProvider,
RoleManager<WebAppRole> roleManager,
UserManager<WebAppUser> userManager)
{
var roleAdmin = new WebAppRole
{
Name = "Admin",
NormalizedName = "ADMIN",
Title = "Administrator",
Seq = 30
};
var roleUser = new WebAppRole
{
Name = "User",
NormalizedName = "USER",
Title = "User",
Seq = 20
};
var roleGuest = new WebAppRole
{
Name = "Guest",
NormalizedName = "GUEST",
Title = "Guest",
Seq = 10
};
await roleManager.CreateAsync(roleAdmin);
await roleManager.CreateAsync(roleUser);
await roleManager.CreateAsync(roleGuest);
var config = serviceProvider.GetRequiredService<IOptions<ConfigSettings>>();
var defaultAdmin = new WebAppUser
{
Email = config.Value.EmailSupport,
EmailConfirmed = true,
Title = "Administrator",
UserName = config.Value.DefaultUSR,
};
await userManager.CreateAsync(defaultAdmin, config.Value.DefaultPWD);
await userManager.AddToRoleAsync(defaultAdmin, "Admin");
}
private static async Task InitDatabaseAsync(OrderMakerContext context)
{
var mtdGroupForm = new MtdCategoryForm
{
Id = "17101180-9250-4498-BE4E-4A941AD6713C",
Name = "Default",
Description = "Default Group",
Parent = "17101180-9250-4498-BE4E-4A941AD6713C"
};
await context.MtdCategoryForm.AddAsync(mtdGroupForm);
await context.SaveChangesAsync();
sbyte active = 1;
List<MtdSysType> mtdSysTypes = new() {
new MtdSysType{ Id = 1, Name="Text", Description="Text", Active=active },
new MtdSysType{ Id = 2, Name="Integer", Description="Integer", Active=active},
new MtdSysType{ Id = 3, Name="Decimal",Description="Decimal", Active=active},
new MtdSysType{ Id = 4, Name = "Memo",Description="Memo",Active=active},
new MtdSysType{ Id = 5, Name="Date",Description="Date",Active=active},
new MtdSysType{ Id = 6, Name="DateTime",Description="DateTime",Active=active},
new MtdSysType{ Id = 7, Name="File",Description="File",Active=active},
new MtdSysType{ Id = 8, Name="Image",Description="Image",Active=active},
new MtdSysType{ Id = 11, Name="List",Description="List",Active=active},
new MtdSysType{ Id = 12, Name="Checkbox",Description="Checkbox",Active=active},
new MtdSysType{ Id = 13, Name="Link",Description="Link",Active=active},
};
await context.MtdSysType.AddRangeAsync(mtdSysTypes);
await context.SaveChangesAsync();
var mtdSysTerms = new List<MtdSysTerm>
{
new MtdSysTerm {Id=1,Name="equal", Sign="=" },
new MtdSysTerm {Id=2,Name="less", Sign="<" },
new MtdSysTerm {Id=3,Name="more", Sign=">" },
new MtdSysTerm {Id=4,Name="contains", Sign="~" },
new MtdSysTerm {Id=5,Name="no equal", Sign="<>" },
};
await context.MtdSysTerm.AddRangeAsync(mtdSysTerms);
await context.SaveChangesAsync();
var mtdSysStyles = new List<MtdSysStyle>
{
new MtdSysStyle{Id=4,Name="Lines", Description="Lines", Active=(sbyte)1},
new MtdSysStyle{Id=5,Name="Columns", Description="Columns", Active=(sbyte)1}
};
await context.MtdSysStyle.AddRangeAsync(mtdSysStyles);
await context.SaveChangesAsync();
}
}
}