Former-commit-id: 35758ac4e921e3d5f7e659bfbb4793be6e021678
Former-commit-id: 938ca04c3fefd6dd467b95e46e00d1bdf2b0b756
TSXN
wanggang 5 years ago
parent a1d7ecd470
commit 092dcbc592

@ -22,10 +22,13 @@
<Content Update="wwwroot/ffmpeg-windows-x64" CopyToPublishDirectory="Never" /> <Content Update="wwwroot/ffmpeg-windows-x64" CopyToPublishDirectory="Never" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="1.0.0.20082702.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="update.sh"> <None Update="update.sh">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="200827.sql"> <None Update="upgradescripts\1.0.0.20082704.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>

@ -1,5 +1,4 @@
using Application.Domain.Entities; using Hangfire;
using Hangfire;
using Hangfire.LiteDB; using Hangfire.LiteDB;
using Infrastructure.Data; using Infrastructure.Data;
using Infrastructure.Email; using Infrastructure.Email;
@ -63,47 +62,64 @@ namespace IoTNode
app.UseHangfireDashboard("/job", options); app.UseHangfireDashboard("/job", options);
app.UseHangfireServer(); app.UseHangfireServer();
var version = Convert.ToInt64(Helper.Instance.GetVersion().Replace(".", ""));
UpdateDataBase(app, "10020082702.sql", () => version < 10020082702);
UpdateVersion(app);
}
private void UpdateVersion(IApplicationBuilder app) UpdateDB(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();
} }
private void UpdateDataBase(IApplicationBuilder app, string sqlFile, Func<bool> func) private void UpdateDB(IApplicationBuilder app)
{ {
using var scope = app.ApplicationServices.CreateScope(); using var scope = app.ApplicationServices.CreateScope();
var services = scope.ServiceProvider; var services = scope.ServiceProvider;
var context = services.GetService<DbContext>(); 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 (version < item.Key)
if (File.Exists(updateSqlFile))
{ {
var sql = File.ReadAllText(updateSqlFile); var sql = File.ReadAllText(item.Value);
try try
{ {
context.Database.BeginTransaction(); context.Database.BeginTransaction();
context.Database.ExecuteSqlRaw(sql); context.Database.ExecuteSqlRaw(sql);
context.Database.CommitTransaction(); 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) catch (Exception ex)
{ {
Console.WriteLine($"update database by {sqlFile} error "); Console.WriteLine($"update database by {item.Value} error ");
ex.PrintStack(); ex.PrintStack();
context.Database.RollbackTransaction(); 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;
} }
} }
} }
Loading…
Cancel
Save