😎优化数据库初始化流程
This commit is contained in:
parent
5705289ea9
commit
5d72b895f6
@ -20,28 +20,6 @@ public class Startup : AppStartup
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 初始化与升级数据,在初始化表结构之前调用
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="dbProvider"></param>
|
|
||||||
/// <param name="oldVerion">旧版本,0表示从来没有设置过,-1表示当前连接不是主数据库,从SysConfig表获取的</param>
|
|
||||||
/// <param name="currentVersion">当前版本,由ConfigConst.SysCurrentVersion转换的整数版本</param>
|
|
||||||
public void BeforeInitTable(SqlSugarScopeProvider dbProvider, long oldVerion, long currentVersion)
|
|
||||||
{
|
|
||||||
// 比较版本号对数据库进行升级结构、种子数据等
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 初始化与升级数据,在初始化种子数据之后调用
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="dbProvider"></param>
|
|
||||||
/// <param name="oldVerion">旧版本,0表示从来没有设置过,-1表示当前连接不是主数据库,从SysConfig表获取的</param>
|
|
||||||
/// <param name="currentVersion">当前版本,由ConfigConst.SysCurrentVersion转换的整数版本</param>
|
|
||||||
public void AfterInitSeed(SqlSugarScopeProvider dbProvider, long oldVerion, long currentVersion)
|
|
||||||
{
|
|
||||||
// 比较版本号对数据库进行升级结构、种子数据等
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构建 WebApplication 对象过程中装载中间件
|
/// 构建 WebApplication 对象过程中装载中间件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -52,33 +30,4 @@ public class Startup : AppStartup
|
|||||||
{
|
{
|
||||||
WebApplication webApplication = application as WebApplication;
|
WebApplication webApplication = application as WebApplication;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 测试InitDatas,Order越大,InitDatas执行越靠前
|
|
||||||
/// </summary>
|
|
||||||
[AppStartup(1000)]
|
|
||||||
public class TestStartup : AppStartup
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 初始化与升级数据,在初始化表结构之前调用
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="dbProvider"></param>
|
|
||||||
/// <param name="oldVerion">旧版本,0表示从来没有设置过,-1表示当前连接不是主数据库,从SysConfig表获取的</param>
|
|
||||||
/// <param name="currentVersion">当前版本,由ConfigConst.SysCurrentVersion转换的整数版本</param>
|
|
||||||
public void BeforeInitTable(SqlSugarScopeProvider dbProvider, long oldVerion, long currentVersion)
|
|
||||||
{
|
|
||||||
// 比较版本号对数据库进行升级结构、种子数据等
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 初始化与升级数据,在初始化种子数据之后调用
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="dbProvider"></param>
|
|
||||||
/// <param name="oldVerion">旧版本,0表示从来没有设置过,-1表示当前连接不是主数据库,从SysConfig表获取的</param>
|
|
||||||
/// <param name="currentVersion">当前版本,由ConfigConst.SysCurrentVersion转换的整数版本</param>
|
|
||||||
public void AfterInitSeed(SqlSugarScopeProvider dbProvider, long oldVerion, long currentVersion)
|
|
||||||
{
|
|
||||||
// 比较版本号对数据库进行升级结构、种子数据等
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -43,7 +43,7 @@ public class SysConfigSeedData : ISqlSugarEntitySeedData<SysConfig>
|
|||||||
new SysConfig{ Id=1300000000291, Name="闲置超时时间", Code=ConfigConst.SysIdleTimeout, Value="0", SysFlag=YesNoEnum.Y, Remark="闲置超时时间(秒),超时强制退出,0 表示不限制", OrderNo=240, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2024-12-20 00:00:00") },
|
new SysConfig{ Id=1300000000291, Name="闲置超时时间", Code=ConfigConst.SysIdleTimeout, Value="0", SysFlag=YesNoEnum.Y, Remark="闲置超时时间(秒),超时强制退出,0 表示不限制", OrderNo=240, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2024-12-20 00:00:00") },
|
||||||
new SysConfig{ Id=1300000000301, Name="开启上线通知", Code=ConfigConst.SysOnlineNotice, Value="True", SysFlag=YesNoEnum.Y, Remark="开启用户上线、下线通知", OrderNo=250, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2025-06-06 00:00:00") },
|
new SysConfig{ Id=1300000000301, Name="开启上线通知", Code=ConfigConst.SysOnlineNotice, Value="True", SysFlag=YesNoEnum.Y, Remark="开启用户上线、下线通知", OrderNo=250, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2025-06-06 00:00:00") },
|
||||||
|
|
||||||
new SysConfig{ Id=1300000000999, Name="系统版本号", Code=ConfigConst.SysVersion, Value="0", SysFlag=YesNoEnum.Y, Remark= "系统版本号,用于自动升级,请勿手动填写", OrderNo=1000, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2025-04-10 00:00:00") },
|
new SysConfig{ Id=1300000000999, Name="系统版本号", Code=ConfigConst.SysVersion, Value="v1.0", SysFlag=YesNoEnum.Y, Remark= "系统版本号", OrderNo=1000, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2025-04-10 00:00:00") },
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -123,16 +123,19 @@ public static class SqlSugarSetup
|
|||||||
IsAutoRemoveDataCache = true, // 启用自动删除缓存,所有增删改会自动调用.RemoveDataCache()
|
IsAutoRemoveDataCache = true, // 启用自动删除缓存,所有增删改会自动调用.RemoveDataCache()
|
||||||
IsAutoDeleteQueryFilter = true, // 启用删除查询过滤器
|
IsAutoDeleteQueryFilter = true, // 启用删除查询过滤器
|
||||||
IsAutoUpdateQueryFilter = true, // 启用更新查询过滤器
|
IsAutoUpdateQueryFilter = true, // 启用更新查询过滤器
|
||||||
SqlServerCodeFirstNvarchar = true // 采用Nvarchar
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 若库类型是人大金仓则默认设置PG模式
|
// 当库类型是人大金仓
|
||||||
if (config.DbType == SqlSugar.DbType.Kdbndp)
|
if (config.DbType == SqlSugar.DbType.Kdbndp)
|
||||||
config.MoreSettings.DatabaseModel = SqlSugar.DbType.PostgreSQL; // 配置PG模式主要是兼容系统表差异
|
config.MoreSettings.DatabaseModel = SqlSugar.DbType.PostgreSQL; // 配置PG模式主要是兼容系统表差异
|
||||||
|
|
||||||
// 若库类型是Oracle则默认主键名字和参数名字最大长度
|
// 当库类型是Oracle
|
||||||
if (config.DbType == SqlSugar.DbType.Oracle)
|
if (config.DbType == SqlSugar.DbType.Oracle)
|
||||||
config.MoreSettings.MaxParameterNameLength = 30;
|
config.MoreSettings.MaxParameterNameLength = 30; // 默认主键名字和参数名字最大长度
|
||||||
|
|
||||||
|
// 当库类型是SqlServer
|
||||||
|
if (config.DbType == SqlSugar.DbType.SqlServer)
|
||||||
|
config.MoreSettings.SqlServerCodeFirstNvarchar = true; // SqlServer默认使用Nvarchar
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -346,36 +349,21 @@ public static class SqlSugarSetup
|
|||||||
{
|
{
|
||||||
var dbProvider = db.GetConnectionScope(config.ConfigId);
|
var dbProvider = db.GetConnectionScope(config.ConfigId);
|
||||||
|
|
||||||
// 判断是否第一次启动
|
|
||||||
bool isFirstRun = true;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
dbProvider.DbMaintenance.IsAnySystemTablePermissions();
|
|
||||||
isFirstRun = false;
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
|
|
||||||
// 初始化/创建数据库
|
// 初始化/创建数据库
|
||||||
if (config.DbSettings.EnableInitDb || isFirstRun)
|
if (config.DbSettings.EnableInitDb)
|
||||||
{
|
{
|
||||||
Log.Information($"初始化数据库 {config.DbType} - {config.ConfigId} - {config.ConnectionString}");
|
Log.Information($"初始化数据库 {config.DbType} - {config.ConfigId} - {config.ConnectionString}");
|
||||||
if (config.DbType != SqlSugar.DbType.Oracle) dbProvider.DbMaintenance.CreateDatabase();
|
if (config.DbType != SqlSugar.DbType.Oracle) dbProvider.DbMaintenance.CreateDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化表结构之前——系统版本号
|
|
||||||
var (startups, oldVerion, currentVersion) = BeforeInitTable(dbProvider);
|
|
||||||
|
|
||||||
// 初始化表结构
|
// 初始化表结构
|
||||||
if (config.TableSettings.EnableInitTable || isFirstRun) InitTable(dbProvider, config.TableSettings.EnableIncreTable);
|
if (config.TableSettings.EnableInitTable) InitTable(dbProvider, config.TableSettings.EnableIncreTable);
|
||||||
|
|
||||||
// 初始化视图
|
// 初始化视图
|
||||||
if (config.DbSettings.EnableInitView || isFirstRun) InitView(dbProvider);
|
if (config.DbSettings.EnableInitView) InitView(dbProvider);
|
||||||
|
|
||||||
// 初始化种子数据
|
// 初始化种子数据
|
||||||
if (config.SeedSettings.EnableInitSeed || isFirstRun) InitSeedData(dbProvider, config.SeedSettings.EnableIncreSeed);
|
if (config.SeedSettings.EnableInitSeed) InitSeedData(dbProvider, config.SeedSettings.EnableIncreSeed);
|
||||||
|
|
||||||
// 初始化种子数据之后——系统版本号
|
|
||||||
AfterInitSeed(dbProvider, startups, oldVerion, currentVersion);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -418,9 +406,7 @@ public static class SqlSugarSetup
|
|||||||
var taskList = entityTypes.Select(entityType => Task.Run(() =>
|
var taskList = entityTypes.Select(entityType => Task.Run(() =>
|
||||||
{
|
{
|
||||||
var stopWatch = Stopwatch.StartNew(); // 开始计时
|
var stopWatch = Stopwatch.StartNew(); // 开始计时
|
||||||
|
|
||||||
dbProvider.InitTable(entityType); // 初始化表结构
|
dbProvider.InitTable(entityType); // 初始化表结构
|
||||||
|
|
||||||
stopWatch.Stop(); // 停止计时
|
stopWatch.Stop(); // 停止计时
|
||||||
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
@ -433,93 +419,6 @@ public static class SqlSugarSetup
|
|||||||
Console.WriteLine($"初始化表结构 {config.DbType} - {config.ConfigId} 总耗时:{totalWatch.ElapsedMilliseconds} ms");
|
Console.WriteLine($"初始化表结构 {config.DbType} - {config.ConfigId} 总耗时:{totalWatch.ElapsedMilliseconds} ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 初始化表结构之前(版本号)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="dbProvider"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private static (IOrderedEnumerable<Type> startups, long oldVerion, long currentVersion) BeforeInitTable(SqlSugarScopeProvider dbProvider)
|
|
||||||
{
|
|
||||||
// 扫描所有继承 AppStartup 的类(排序执行顺序)
|
|
||||||
var startups = App.EffectiveTypes
|
|
||||||
.Where(u => typeof(AppStartup).IsAssignableFrom(u) && u.IsClass && !u.IsAbstract && !u.IsGenericType && (u.GetMethod("BeforeInitTable") != null || u.GetMethod("AfterInitSeed") != null))
|
|
||||||
.OrderByDescending(u => !u.IsDefined(typeof(AppStartupAttribute), true) ? 0 : u.GetCustomAttribute<AppStartupAttribute>(true)!.Order);
|
|
||||||
if (!startups!.Any()) return (startups, 0, 0);
|
|
||||||
|
|
||||||
long oldVerion = 0, currentVersion = 0;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// 获取系统版本号
|
|
||||||
if (dbProvider.CurrentConnectionConfig.ConfigId.ToString() == SqlSugarConst.MainConfigId && dbProvider.DbMaintenance.IsAnyTable(dbProvider.EntityMaintenance.GetTableName(typeof(SysConfig))))
|
|
||||||
{
|
|
||||||
var versionCfg = dbProvider.Queryable<SysConfig>().Where(u => u.Code == ConfigConst.SysVersion).First();
|
|
||||||
oldVerion = versionCfg != null ? CommonHelper.ConvertVersionToLong(versionCfg.Value) : 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
oldVerion = -1;
|
|
||||||
}
|
|
||||||
currentVersion = CommonHelper.ConvertVersionToLong(ConfigConst.SysCurrentVersion);
|
|
||||||
|
|
||||||
foreach (var type in startups)
|
|
||||||
{
|
|
||||||
var startup = Activator.CreateInstance(type) as AppStartup;
|
|
||||||
var initDataMethod = type.GetMethod("BeforeInitTable");
|
|
||||||
initDataMethod?.Invoke(startup, [dbProvider, oldVerion, currentVersion]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Log.Information($"数据库初始化表结构之前有错 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId} : {ex.Message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
return (startups, oldVerion, currentVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 初始化种子数据之后(版本号)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="dbProvider"></param>
|
|
||||||
/// <param name="startups"></param>
|
|
||||||
/// <param name="oldVerion"></param>
|
|
||||||
/// <param name="currentVersion"></param>
|
|
||||||
private static void AfterInitSeed(SqlSugarScopeProvider dbProvider, IOrderedEnumerable<Type> startups, long oldVerion, long currentVersion)
|
|
||||||
{
|
|
||||||
if (startups == null || !startups.Any()) return;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
foreach (var type in startups)
|
|
||||||
{
|
|
||||||
if (Activator.CreateInstance(type) is not AppStartup startup) continue;
|
|
||||||
var initDataMethod = type.GetMethod("AfterInitSeed");
|
|
||||||
if (initDataMethod == null) continue;
|
|
||||||
initDataMethod?.Invoke(startup, [dbProvider, oldVerion, currentVersion]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
string errr = $"数据库初始化种子数据之后有错 {dbProvider.CurrentConnectionConfig.DbType} - {dbProvider.CurrentConnectionConfig.ConfigId} : {ex.Message}";
|
|
||||||
Log.Information(errr);
|
|
||||||
Console.WriteLine(errr);
|
|
||||||
Console.WriteLine(ex.StackTrace);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
// 更新系统版本号
|
|
||||||
if (dbProvider.CurrentConnectionConfig.ConfigId.ToString() == SqlSugarConst.MainConfigId)
|
|
||||||
{
|
|
||||||
IEnumerable<SysConfig> cfgs =
|
|
||||||
[
|
|
||||||
new SysConfig{ Id=1300000000999, Name="系统版本号", Code=ConfigConst.SysVersion, Value=ConfigConst.SysCurrentVersion, SysFlag=YesNoEnum.Y, Remark= "系统版本号,用于自动升级,请勿手动填写", OrderNo=1000, GroupCode=ConfigConst.SysDefaultGroup, CreateTime=DateTime.Parse("2025-04-10 00:00:00") }
|
|
||||||
];
|
|
||||||
var storage = dbProvider.StorageableByObject(cfgs.ToList()).ToStorage();
|
|
||||||
storage.AsInsertable.ExecuteCommand();
|
|
||||||
storage.AsUpdateable.ExecuteCommand();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化种子数据
|
/// 初始化种子数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user