优化脚本执行

This commit is contained in:
xief 2022-01-24 15:12:21 +08:00
parent a606282499
commit cd27c7e03a

View File

@ -45,6 +45,9 @@ namespace Chenfeng.MES.Archiver.Commands
[CommandOption("script", Description = "是否生成执行脚本")]
public bool Script { get; set; }
[CommandOption("verbose", 'v', Description = "显示sql")]
public bool Verbose { get; set; }
public ValueTask ExecuteAsync(IConsole console)
{
Print = (text) => console.Output.WriteLine($"{DateTime.Now.ToLongTimeString()} {text}");
@ -52,22 +55,7 @@ namespace Chenfeng.MES.Archiver.Commands
var starMonth = DateTime.Now.AddMonths(-12).ToString("yyyyMM");
SqlMapper.Settings.CommandTimeout = 60 * Timeout;
var executeType = "copy";
if (RemoveBackup)
{
executeType = "rm_bak";
console.Output.WriteLine("删除备份表");
}
else if (Restore)
{
executeType = "restore";
console.Output.WriteLine("还原备份表");
}
else
{
console.Output.WriteLine($"拷贝"+( Legacy ? "旧" :"新")+"版表数据"+(Replace?"并替换":""));
}
console.Output.WriteLine((Legacy ? "旧" : "新") + "版数据库");
try
{
if (this.Connection.Length > 0)
@ -88,13 +76,23 @@ namespace Chenfeng.MES.Archiver.Commands
TableQueryHelper.MesTables(this, starMonth);
}
if (Db!=null && sqlScripts.Length>0)
{
var sql = "set foreign_key_checks = 0;" + sqlScripts.ToString() + "set foreign_key_checks = 1;";
if (Verbose)
{
Print(sql);
}
Db.Execute(sql);
}
if (Script)
{
if (Directory.Exists("./scripts") == false)
{
Directory.CreateDirectory("./scripts");
}
using (var file = File.OpenWrite($"./scripts/mes{(Legacy?"-legacy":"")}-table-{executeType}.sql"))
using (var file = File.OpenWrite($"./scripts/mes{(Legacy ? "-legacy" : "")}-table.sql"))
{
using (var writer = new StreamWriter(file))
{
@ -120,20 +118,62 @@ namespace Chenfeng.MES.Archiver.Commands
}
public void ExecuteData(string table, string where, object param)
{
if (ExecuteBefore(table))
{
ExecuteSql($"INSERT INTO `{TableDic[table]}` SELECT * FROM `{table}` WHERE {where}", param);
}
}
public void ExecuteChildData(string table, string parentTable, string condition)
{
if (ExecuteBefore(table))
{
ExecuteSql($"INSERT INTO `{TableDic[table]}` SELECT child.* FROM `{TableDic[parentTable]}` parent join `{table}` child on {condition} ");
}
}
private bool ExecuteBefore(string table)
{
if (RemoveBackup)
{
ExecuteSql($"DROP TABLE IF EXISTS `{table}_bak`", Execute: false);
}
else if (Restore)
{
ExecuteSql($"DROP TABLE IF EXISTS `{table}`", Execute: false);
ExecuteSql($"RENAME TABLE `{table}_bak` TO `{table}`", Execute: false);
}
else if (Replace)
{
ExecuteSql($"RENAME TABLE `{table}` TO `{table}_bak`", Execute: false);
ExecuteSql($"RENAME TABLE `{TableDic[table]}` TO `{table}`", Execute: false);
}
else
{
ExecuteSql($"DROP TABLE IF EXISTS `{TableDic[table]}`");
ExecuteSql($"CREATE TABLE `{TableDic[table]}` LIKE `{table}`"); // IF NOT EXISTS
Print(TableDic[table] + " 创建完成");
return true;
}
return false;
}
private StringBuilder sqlScripts = new StringBuilder();
private void ExecuteSql(string sql, object param = null)
private void ExecuteSql(string sql, object? param = null, bool Execute = true)
{
if (Execute)
{
Db?.Execute(sql, param);
if (Script)
}
if (Script || Execute == false)
{
var fullSql = sql + ";";
if (param != null)
@ -152,6 +192,7 @@ namespace Chenfeng.MES.Archiver.Commands
public Dictionary<string, string> TableDic { get; set; } = new Dictionary<string, string>();
public void Step(Action<string> action, params string[] tables)
{
var list = tables.Select(table => new
@ -161,34 +202,11 @@ namespace Chenfeng.MES.Archiver.Commands
});
foreach (var item in list)
{
if (RemoveBackup)
{
ExecuteSql($"DROP TABLE IF EXISTS `{item.Table}_bak`");
}
else if (Restore)
{
ExecuteSql($"DROP TABLE IF EXISTS `{item.Table}`");
ExecuteSql($"RENAME TABLE `{item.Table}_bak` TO `{item.Table}`");
}
else
{
TableDic[item.Table] = item.NewTable;
ExecuteSql($"DROP TABLE IF EXISTS `{item.NewTable}`");
ExecuteSql($"CREATE TABLE `{item.NewTable}` LIKE `{item.Table}`"); // IF NOT EXISTS
Print(item.NewTable + " 创建完成");
action(item.Table);
Print(item.NewTable + " 拷贝完成");
}
if (Replace)
{
ExecuteSql($"RENAME TABLE `{item.Table}` TO `{item.Table}_bak`");
ExecuteSql($"RENAME TABLE `{item.NewTable}` TO `{item.Table}`");
}
}
}
}
}
}