fix(infra): auto-apply app and dictionary migrations on startup
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 41s
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 41s
This commit is contained in:
@@ -30,17 +30,23 @@ public sealed class AppDataSeeder(
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task StartAsync(CancellationToken cancellationToken)
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (!_options.Enabled)
|
|
||||||
{
|
|
||||||
logger.LogInformation("AppSeed 未启用,跳过业务数据初始化");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
using var scope = serviceProvider.CreateScope();
|
using var scope = serviceProvider.CreateScope();
|
||||||
var appDbContext = scope.ServiceProvider.GetRequiredService<TakeoutAppDbContext>();
|
var appDbContext = scope.ServiceProvider.GetRequiredService<TakeoutAppDbContext>();
|
||||||
var dictionaryDbContext = scope.ServiceProvider.GetRequiredService<DictionaryDbContext>();
|
var dictionaryDbContext = scope.ServiceProvider.GetRequiredService<DictionaryDbContext>();
|
||||||
|
|
||||||
|
// 1. 启动时优先确保 App/Dictionary 数据库迁移到最新版本。
|
||||||
|
await appDbContext.Database.MigrateAsync(cancellationToken);
|
||||||
|
await dictionaryDbContext.Database.MigrateAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (!_options.Enabled)
|
||||||
|
{
|
||||||
|
logger.LogInformation("AppSeed 未启用,仅执行数据库迁移,跳过业务数据初始化");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var tenantContextAccessor = scope.ServiceProvider.GetRequiredService<ITenantContextAccessor>();
|
var tenantContextAccessor = scope.ServiceProvider.GetRequiredService<ITenantContextAccessor>();
|
||||||
|
|
||||||
|
// 2. 执行业务数据种子。
|
||||||
await EnsureSystemTenantAsync(appDbContext, cancellationToken);
|
await EnsureSystemTenantAsync(appDbContext, cancellationToken);
|
||||||
var defaultTenantId = await EnsureDefaultTenantAsync(appDbContext, cancellationToken);
|
var defaultTenantId = await EnsureDefaultTenantAsync(appDbContext, cancellationToken);
|
||||||
await EnsureDictionarySeedsAsync(dictionaryDbContext, tenantContextAccessor, defaultTenantId, cancellationToken);
|
await EnsureDictionarySeedsAsync(dictionaryDbContext, tenantContextAccessor, defaultTenantId, cancellationToken);
|
||||||
|
|||||||
Reference in New Issue
Block a user