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

This commit is contained in:
2026-02-18 20:41:22 +08:00
parent c853fd8edf
commit 2c086d1149

View File

@@ -30,17 +30,23 @@ public sealed class AppDataSeeder(
/// <inheritdoc />
public async Task StartAsync(CancellationToken cancellationToken)
{
if (!_options.Enabled)
{
logger.LogInformation("AppSeed 未启用,跳过业务数据初始化");
return;
}
using var scope = serviceProvider.CreateScope();
var appDbContext = scope.ServiceProvider.GetRequiredService<TakeoutAppDbContext>();
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>();
// 2. 执行业务数据种子。
await EnsureSystemTenantAsync(appDbContext, cancellationToken);
var defaultTenantId = await EnsureDefaultTenantAsync(appDbContext, cancellationToken);
await EnsureDictionarySeedsAsync(dictionaryDbContext, tenantContextAccessor, defaultTenantId, cancellationToken);