Merge pull request '😁优化初始化逻辑' (#344) from jasondom/Admin.NET.Pro:v2-dev into v2
Reviewed-on: https://code.adminnet.top/Admin.NET/Admin.NET.Pro/pulls/344
This commit is contained in:
commit
cc3df148f8
@ -362,10 +362,17 @@ public static class SqlSugarSetup
|
||||
{
|
||||
var dbProvider = db.GetConnectionScope(config.ConfigId);
|
||||
|
||||
// 若第一次启动系统则强制初始化数据库表和种子数据
|
||||
bool isFirstRun = false;
|
||||
try { isFirstRun = dbProvider.CurrentConnectionConfig.ConfigId.ToString() == SqlSugarConst.MainConfigId && !dbProvider.DbMaintenance.IsAnyTable(dbProvider.EntityMaintenance.GetTableName(typeof(SysConfig))); }
|
||||
catch { isFirstRun = true; }
|
||||
// 判断是否第一次启动
|
||||
bool isFirstRun = true;
|
||||
try
|
||||
{
|
||||
dbProvider.DbMaintenance.IsAnySystemTablePermissions();
|
||||
isFirstRun = false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
|
||||
// 初始化/创建数据库
|
||||
if (config.DbSettings.EnableInitDb || isFirstRun)
|
||||
@ -380,6 +387,7 @@ public static class SqlSugarSetup
|
||||
// 初始化表结构
|
||||
if (config.TableSettings.EnableInitTable || isFirstRun)
|
||||
{
|
||||
var totalWatch = Stopwatch.StartNew(); // 开始总计时
|
||||
Log.Information($"初始化表结构 {config.DbType} - {config.ConfigId}");
|
||||
var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false))
|
||||
.Where(u => !u.GetCustomAttributes<IgnoreTableAttribute>().Any())
|
||||
@ -392,6 +400,15 @@ public static class SqlSugarSetup
|
||||
else
|
||||
entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定义的库
|
||||
|
||||
// 删除视图再初始化表结构,防止因为视图导致无法同步表结构
|
||||
var viewTypeList = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarView)))).ToList();
|
||||
foreach (var viewType in viewTypeList)
|
||||
{
|
||||
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(viewType) ?? throw new Exception("获取视图实体配置有误");
|
||||
if (dbProvider.DbMaintenance.GetViewInfoList(false).Any(it => it.Name.EqualIgnoreCase(entityInfo.DbTableName)))
|
||||
dbProvider.DbMaintenance.DropView(entityInfo.DbTableName);
|
||||
}
|
||||
|
||||
int taskIndex = 0, size = entityTypes.Count;
|
||||
var taskList = entityTypes.Select(entityType => Task.Run(() =>
|
||||
{
|
||||
@ -405,6 +422,10 @@ public static class SqlSugarSetup
|
||||
Console.WriteLine($"初始化表 {entityType,-64} ({config.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003}) 耗时:{stopWatch.ElapsedMilliseconds} ms");
|
||||
}));
|
||||
Task.WaitAll(taskList.ToArray());
|
||||
|
||||
totalWatch.Stop(); // 停止总计时
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"初始化表结构 {config.DbType} - {config.ConfigId} 总耗时:{totalWatch.ElapsedMilliseconds} ms");
|
||||
}
|
||||
|
||||
// 初始化视图
|
||||
@ -512,6 +533,7 @@ public static class SqlSugarSetup
|
||||
/// <param name="enableIncreSeed"></param>
|
||||
private static void InitSeedData(SqlSugarScopeProvider dbProvider, bool enableIncreSeed)
|
||||
{
|
||||
var totalWatch = Stopwatch.StartNew(); // 开始总计时
|
||||
Log.Information($"初始化种子数据 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId}");
|
||||
var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
|
||||
.Where(u => !u.IsDefined(typeof(TenantSeedAttribute), false))
|
||||
@ -533,6 +555,10 @@ public static class SqlSugarSetup
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"初始化种子数据 {seedType.FullName,-58} ({dbProvider.CurrentConnectionConfig.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003},数据量:{tuple.Value.Item1:D003},新增 {tuple.Value.Item2:D003} 条记录,更新 {tuple.Value.Item3:D003} 条记录,耗时:{stopWatch.ElapsedMilliseconds:N0} ms)");
|
||||
}
|
||||
|
||||
totalWatch.Stop(); // 停止总计时
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"初始化种子数据 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId} 总耗时:{totalWatch.ElapsedMilliseconds:N0} ms");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -541,6 +567,7 @@ public static class SqlSugarSetup
|
||||
/// <param name="dbProvider"></param>
|
||||
private static void InitView(SqlSugarScopeProvider dbProvider)
|
||||
{
|
||||
var totalWatch = Stopwatch.StartNew(); // 开始总计时
|
||||
Log.Information($"初始化视图 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId}");
|
||||
var viewTypeList = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarView)))).ToList();
|
||||
|
||||
@ -554,7 +581,7 @@ public static class SqlSugarSetup
|
||||
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(viewType) ?? throw new Exception("获取视图实体配置有误");
|
||||
|
||||
// 如果视图存在,则删除视图
|
||||
if (dbProvider.DbMaintenance.GetViewInfoList().Any(it => it.Name.EqualIgnoreCase(entityInfo.DbTableName)))
|
||||
if (dbProvider.DbMaintenance.GetViewInfoList(false).Any(it => it.Name.EqualIgnoreCase(entityInfo.DbTableName)))
|
||||
dbProvider.DbMaintenance.DropView(entityInfo.DbTableName);
|
||||
|
||||
// 获取初始化视图查询SQL
|
||||
@ -570,6 +597,10 @@ public static class SqlSugarSetup
|
||||
Console.WriteLine($"初始化视图 {viewType.FullName,-58} ({dbProvider.CurrentConnectionConfig.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{size:D003},耗时:{stopWatch.ElapsedMilliseconds:N0} ms)");
|
||||
}));
|
||||
Task.WaitAll(taskList.ToArray());
|
||||
|
||||
totalWatch.Stop(); // 停止总计时
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"初始化视图 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId} 总耗时:{totalWatch.ElapsedMilliseconds:N0} ms");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user