You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
6.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
{
/// <summary>
/// 初始化Main数据库结构
/// </summary>
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);
}
}
/// <summary>
/// 初始化日志数据库结构
/// </summary>
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<SplitTableAttribute>() != null)
{
logDb.CodeFirst.SplitTables().InitTables(t);
}
else
{
logDb.CodeFirst.InitTables(t);
}
}
});
sw.Stop();
$"Log Tables created successfully! {sw.ElapsedMilliseconds}ms".WriteSuccessLine();
Console.WriteLine();
}
}
}