using EntrustSettle.Common.Const;
using EntrustSettle.Common.DB;
using EntrustSettle.Model;
using SqlSugar;
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
namespace EntrustSettle.Common.Seed
{
public class DBSeed
{
///
/// 初始化Main数据库结构
///
public static void Seed(SeedContext myContext)
{
try
{
Console.WriteLine("************ EntrustSettle DataBase Set *****************");
Console.WriteLine($"Master DB ConId: {myContext.Db.CurrentConnectionConfig.ConfigId}");
Console.WriteLine($"Master DB Type: {myContext.Db.CurrentConnectionConfig.DbType}");
Console.WriteLine($"Master DB ConnectString: {myContext.Db.CurrentConnectionConfig.ConnectionString}");
Console.WriteLine();
if (BaseDBConfig.MainConfig.SlaveConnectionConfigs.AnyNoException())
{
var index = 0;
BaseDBConfig.MainConfig.SlaveConnectionConfigs.ForEach(m =>
{
index++;
Console.WriteLine($"Slave{index} DB HitRate: {m.HitRate}");
Console.WriteLine($"Slave{index} DB ConnectString: {m.ConnectionString}");
Console.WriteLine($"--------------------------------------");
});
}
else if (BaseDBConfig.ReuseConfigs.AnyNoException())
{
var index = 0;
BaseDBConfig.ReuseConfigs.ForEach(m =>
{
index++;
Console.WriteLine($"Reuse{index} DB ID: {m.ConfigId}");
Console.WriteLine($"Reuse{index} DB Type: {m.DbType}");
Console.WriteLine($"Reuse{index} DB ConnectString: {m.ConnectionString}");
Console.WriteLine($"--------------------------------------");
});
}
Console.WriteLine();
// 创建数据库
Console.WriteLine($"Create Database(The Db Id:{SeedContext.ConnId})...");
if (SeedContext.DbType != SqlSugar.DbType.Oracle && SeedContext.DbType != SqlSugar.DbType.Dm)
{
myContext.Db.DbMaintenance.CreateDatabase();
ConsoleHelper.WriteSuccessLine($"Database created successfully!");
}
else
{
//Oracle 数据库不支持该操作
ConsoleHelper.WriteSuccessLine($"Oracle 数据库不支持该操作,可手动创建Oracle/Dm数据库!");
}
// 创建数据库表,遍历指定命名空间下的class,
// 注意不要把其他命名空间下的也添加进来。
Console.WriteLine("Create Tables...");
var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
var referencedAssemblies = System.IO.Directory.GetFiles(path, "EntrustSettle.Model.dll")
.Select(Assembly.LoadFrom).ToArray();
var modelTypes = referencedAssemblies
.SelectMany(a => a.DefinedTypes)
.Select(type => type.AsType())
.Where(x => x.IsClass && x.Namespace is "EntrustSettle.Model.Models")
.ToList();
modelTypes.ForEach(t =>
{
// 这里只支持添加表,不支持删除
// 如果想要删除,数据库直接右键删除,或者联系SqlSugar作者;
if (!myContext.Db.DbMaintenance.IsAnyTable(t.Name))
{
Console.WriteLine(t.Name);
myContext.Db.CodeFirst.SplitTables().InitTables(t);
}
});
ConsoleHelper.WriteSuccessLine($"Tables created successfully!");
Console.WriteLine();
}
catch (Exception ex)
{
throw new Exception(
$"1、若是Mysql,查看常见问题:https://github.com/anjoy8/EntrustSettle/issues/148#issue-776281770 \n" +
$"2、若是Oracle,查看常见问题:https://github.com/anjoy8/EntrustSettle/issues/148#issuecomment-752340231 \n" +
"3、其他错误:" + ex.Message);
}
}
///
/// 初始化日志数据库结构
///
public static void MigrationLogs(SeedContext myContext)
{
// 创建数据库表,遍历指定命名空间下的class,
// 注意不要把其他命名空间下的也添加进来。
Console.WriteLine("Create Log Tables...");
if (!myContext.Db.IsAnyConnection(DBConst.LogDbConfigId.ToLower()))
{
throw new ApplicationException("未配置日志数据库,请在appsettings.json中DBS节点中配置");
}
var logDb = myContext.Db.GetConnection(DBConst.LogDbConfigId.ToLower());
Console.WriteLine($"Create log Database(The Db Id:{DBConst.LogDbConfigId.ToLower()})...");
logDb.DbMaintenance.CreateDatabase();
ConsoleHelper.WriteSuccessLine($"Log Database created successfully!");
var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
var referencedAssemblies = System.IO.Directory.GetFiles(path, "EntrustSettle.Model.dll")
.Select(Assembly.LoadFrom).ToArray();
var modelTypes = referencedAssemblies
.SelectMany(a => a.DefinedTypes)
.Select(type => type.AsType())
.Where(x => x.IsClass && x.Namespace != null && x.Namespace.StartsWith("EntrustSettle.Model.Logs") && !x.IsAbstract)
.ToList();
Stopwatch sw = Stopwatch.StartNew();
var tables = logDb.DbMaintenance.GetTableInfoList();
modelTypes.ForEach(t =>
{
// 这里只支持添加修改表,不支持删除
// 如果想要删除,数据库直接右键删除,或者联系SqlSugar作者;
if (!tables.Any(s => s.Name.Contains(t.Name)))
{
Console.WriteLine(t.Name);
if (t.GetCustomAttribute() != null)
{
logDb.CodeFirst.SplitTables().InitTables(t);
}
else
{
logDb.CodeFirst.InitTables(t);
}
}
});
sw.Stop();
$"Log Tables created successfully! {sw.ElapsedMilliseconds}ms".WriteSuccessLine();
Console.WriteLine();
}
}
}