Skip to content

Commit

Permalink
Save db version to file so no need to check db every time on startup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wayne-KTCSZ committed Sep 3, 2024
1 parent 913b7ab commit b3907b3
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Easy.StartTask
{
public interface IStartTask
public interface IAppStartTask
{
void Excute();
void OnStartup();
}
}
7 changes: 7 additions & 0 deletions src/EasyFrameWork/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ public static Version Parse(string version)
return false;
}

public static implicit operator Version(string version)
{
if (string.IsNullOrEmpty(version)) return null;

return Parse(version);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
Expand Down
4 changes: 2 additions & 2 deletions src/ZKEACMS.Updater/ApplicationStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

namespace ZKEACMS.Updater
{
public class ApplicationStartup : IStartTask
public class ApplicationStartup : IAppStartTask
{
private readonly IDbUpdaterService _dbUpdaterService;
public ApplicationStartup(IDbUpdaterService dbUpdaterService)
{
_dbUpdaterService = dbUpdaterService;
}
public void Excute()
public void OnStartup()
{
_dbUpdaterService.UpdateDatabase();
}
Expand Down
1 change: 1 addition & 0 deletions src/ZKEACMS.Updater/Models/DBVersionOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace ZKEACMS.Updater.Models
{
public class DBVersionOption
{
public string DBVersion { get; set; }
public string BaseVersion { get; set; }
public string[] Source { get; set; }
}
Expand Down
31 changes: 27 additions & 4 deletions src/ZKEACMS.Updater/Service/DbUpdaterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Easy.Extend;
using Easy.Mvc.Plugin;
using Easy.Net;
using Easy.Serializer;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
Expand All @@ -26,7 +27,7 @@ namespace ZKEACMS.Updater.Service
{
public class DbUpdaterService : IDbUpdaterService
{
private readonly IOptions<DBVersionOption> _dbVersionOption;
private readonly DBVersionOption _dbVersionOption;
private readonly IWebClient _webClient;
private readonly ILogger<DbUpdaterService> _logger;
private readonly IDBContextProvider _dbContextProvider;
Expand All @@ -39,7 +40,7 @@ public DbUpdaterService(DatabaseOption databaseOption,
IDBContextProvider dbContextProvider,
ILogger<DbUpdaterService> logger)
{
_dbVersionOption = dbVersionOption;
_dbVersionOption = dbVersionOption.Value;
_webClient = webClient;
_scriptFileName = $"{databaseOption.DbType}.sql";//MsSql.sql, MySql.sql, Sqlite.sql
_logger = logger;
Expand All @@ -56,6 +57,8 @@ public void UpdateDatabase()
private void UpgradeDbToLatest()
{
var appVersion = Easy.Version.Parse(Version.VersionInfo);
if (GetVersionFromFile() == appVersion) return;

bool allSuccess = true;
foreach (var dbContext in _dbContextProvider.GetAvailableDbContexts())
{
Expand All @@ -67,6 +70,7 @@ private void UpgradeDbToLatest()
}
if (allSuccess)
{
SaveVersionToFile(appVersion);
DeleteAllCachedScripts();
}
}
Expand Down Expand Up @@ -124,11 +128,30 @@ private Easy.Version GetDbVersion(DbContext dbContext)
}
if (version == null)
{
version = Easy.Version.Parse(_dbVersionOption.Value.BaseVersion);
version = Easy.Version.Parse(_dbVersionOption.BaseVersion);
}
return version;
}
private Easy.Version GetVersionFromFile()
{
return _dbVersionOption.DBVersion;
}

private void SaveVersionToFile(Easy.Version version)
{
try
{
var plugPath = PluginBase.GetPath<UpdaterPlug>();
var versionFile = Path.Combine(plugPath, "appsettings.json");
_dbVersionOption.DBVersion = version.ToString();
string versionJson = JsonConverter.Serialize(new { DBVersionOption = _dbVersionOption });
File.WriteAllText(versionFile, versionJson);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
}
}
private void ExcuteLocalScripts()
{
List<string> localScripts = ReadLocalScripts();
Expand Down Expand Up @@ -223,7 +246,7 @@ private IEnumerable<string> ReadRemoteScripts(Easy.Version versionFrom, Easy.Ver
private ReleaseVersion GetReleaseVersions()
{
ReleaseVersion releaseVersion = null;
foreach (var item in _dbVersionOption.Value.Source)
foreach (var item in _dbVersionOption.Source)
{
availableSource = item;
string source = $"{availableSource}/index.json";
Expand Down
2 changes: 1 addition & 1 deletion src/ZKEACMS.Updater/UpdaterPlug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override IEnumerable<WidgetTemplateEntity> WidgetServiceTypes()
public override void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddTransient<IOnModelCreating, EntityFrameWorkModelCreating>();
serviceCollection.AddTransient<IStartTask, ApplicationStartup>();
serviceCollection.AddTransient<IAppStartTask, ApplicationStartup>();
serviceCollection.AddTransient<IDbUpdaterService, DbUpdaterService>();
serviceCollection.AddTransient<IDashboardPartDriveService, UpdateDbFailedDashboardService>();
serviceCollection.RegistEvent<UpdateDbOnDbCreatedEventHandler>(Events.OnDatabaseCreated);
Expand Down
19 changes: 10 additions & 9 deletions src/ZKEACMS.Updater/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"DBVersionOption": {
"BaseVersion": "3.3.5",
"Source": [
"https://gitee.com/seriawei/ZKEACMS.Core/raw/develop/Database",
"https://raw.githubusercontent.com/SeriaWei/ZKEACMS/develop/Database"
]
}
}
{
"DBVersionOption": {
"DBVersion": "4.0.0.0",
"BaseVersion": "3.3.5",
"Source": [
"https://gitee.com/seriawei/ZKEACMS.Core/raw/develop/Database",
"https://raw.githubusercontent.com/SeriaWei/ZKEACMS/develop/Database"
]
}
}
4 changes: 2 additions & 2 deletions src/ZKEACMS/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public static void UseZKEACMS(this IApplicationBuilder applicationBuilder, IWebH
});
using (var scope = applicationBuilder.ApplicationServices.CreateScope())
{
foreach (IStartTask task in scope.ServiceProvider.GetServices<IStartTask>())
foreach (IAppStartTask task in scope.ServiceProvider.GetServices<IAppStartTask>())
{
task.Excute();
task.OnStartup();
}
}

Expand Down

0 comments on commit b3907b3

Please sign in to comment.