|
|
|
@ -1,5 +1,4 @@
|
|
|
|
|
using Application.Domain.Entities;
|
|
|
|
|
using Hangfire;
|
|
|
|
|
using Hangfire;
|
|
|
|
|
using Hangfire.LiteDB;
|
|
|
|
|
using Infrastructure.Data;
|
|
|
|
|
using Infrastructure.Email;
|
|
|
|
@ -63,47 +62,64 @@ namespace IoTNode
|
|
|
|
|
app.UseHangfireDashboard("/job", options);
|
|
|
|
|
|
|
|
|
|
app.UseHangfireServer();
|
|
|
|
|
var version = Convert.ToInt64(Helper.Instance.GetVersion().Replace(".", ""));
|
|
|
|
|
UpdateDataBase(app, "10020082702.sql", () => version < 10020082702);
|
|
|
|
|
UpdateVersion(app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateVersion(IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
var scope = app.ApplicationServices.CreateScope();
|
|
|
|
|
var services = scope.ServiceProvider;
|
|
|
|
|
var context = services.GetService<DbContext>();
|
|
|
|
|
var node = context.Set<Node>().FirstOrDefault();
|
|
|
|
|
node.Version = Helper.Instance.GetVersion();
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
UpdateDB(app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateDataBase(IApplicationBuilder app, string sqlFile, Func<bool> func)
|
|
|
|
|
private void UpdateDB(IApplicationBuilder app)
|
|
|
|
|
{
|
|
|
|
|
using var scope = app.ApplicationServices.CreateScope();
|
|
|
|
|
var services = scope.ServiceProvider;
|
|
|
|
|
var context = services.GetService<DbContext>();
|
|
|
|
|
if (func.Invoke())
|
|
|
|
|
using var cmd = context.Database.GetDbConnection().CreateCommand();
|
|
|
|
|
cmd.CommandText = "select Version from iot_Node";
|
|
|
|
|
context.Database.OpenConnection();
|
|
|
|
|
var versionValue = cmd.ExecuteScalar().ToString();
|
|
|
|
|
var version = this.GetVersion(versionValue);
|
|
|
|
|
var files = Directory.GetFiles(Path.Combine(Env.ContentRootPath, "upgradescripts"))
|
|
|
|
|
.ToDictionary(o => this.GetVersion(Path.GetFileNameWithoutExtension(o))).OrderBy(o => o.Key);
|
|
|
|
|
foreach (var item in files)
|
|
|
|
|
{
|
|
|
|
|
var updateSqlFile = Path.Combine(this.Env.ContentRootPath, sqlFile);
|
|
|
|
|
if (File.Exists(updateSqlFile))
|
|
|
|
|
if (version < item.Key)
|
|
|
|
|
{
|
|
|
|
|
var sql = File.ReadAllText(updateSqlFile);
|
|
|
|
|
var sql = File.ReadAllText(item.Value);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
context.Database.BeginTransaction();
|
|
|
|
|
context.Database.ExecuteSqlRaw(sql);
|
|
|
|
|
context.Database.CommitTransaction();
|
|
|
|
|
File.Delete(updateSqlFile);
|
|
|
|
|
using var cmd2 = context.Database.GetDbConnection().CreateCommand();
|
|
|
|
|
context.Database.OpenConnection();
|
|
|
|
|
cmd2.CommandText = $"update iot_Node set Version='{Path.GetFileNameWithoutExtension(item.Value)}'";
|
|
|
|
|
cmd2.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"update database by {sqlFile} error ");
|
|
|
|
|
Console.WriteLine($"update database by {item.Value} error ");
|
|
|
|
|
ex.PrintStack();
|
|
|
|
|
context.Database.RollbackTransaction();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long GetVersion(string versionValue)
|
|
|
|
|
{
|
|
|
|
|
var values = versionValue.Split('.').Select(o => Convert.ToInt64(o)).Reverse().ToArray();
|
|
|
|
|
var result = 0L;
|
|
|
|
|
for (int i = 0; i < values.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (i == 0)
|
|
|
|
|
{
|
|
|
|
|
result += values[i];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result += values[i] * (long)Math.Pow(10, i + 7);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|