From 7d46035cc57e1c8c06adfd35dc65289eaf944ce0 Mon Sep 17 00:00:00 2001 From: Eric Nguyen Date: Wed, 11 Oct 2023 18:23:33 +0700 Subject: [PATCH] fix sync system databases --- .github/workflows/develop_dev-mixcore.yml | 2 +- src/applications/Mixcore/mixcore.csproj | 10 ++++ .../templates/_ViewImports.cshtml | 16 ++++++ .../templates/_ViewStart.cshtml | 3 ++ .../mix.constant/Constants/MixFolders.cs | 2 +- .../Services/MixThemeImportService.cs | 10 +++- .../mix.repodb/Interfaces/IMixDbService.cs | 2 +- .../mix.repodb/ServiceCollectionExtensions.cs | 5 -- .../mix.repodb/Services/MixDbService.cs | 51 ++++++++----------- 9 files changed, 61 insertions(+), 40 deletions(-) create mode 100644 src/applications/Mixcore/wwwroot/default-mixcontent/templates/_ViewImports.cshtml create mode 100644 src/applications/Mixcore/wwwroot/default-mixcontent/templates/_ViewStart.cshtml diff --git a/.github/workflows/develop_dev-mixcore.yml b/.github/workflows/develop_dev-mixcore.yml index dd29dd84b..8cf83f8cd 100644 --- a/.github/workflows/develop_dev-mixcore.yml +++ b/.github/workflows/develop_dev-mixcore.yml @@ -6,7 +6,7 @@ name: Build and deploy ASP.Net Core app to Azure Web App - dev-mixcore on: push: branches: - - features/cleanup-startup + - features/fix-migrate-sytem-databases workflow_dispatch: jobs: diff --git a/src/applications/Mixcore/mixcore.csproj b/src/applications/Mixcore/mixcore.csproj index fb2c97aa4..58b28d582 100644 --- a/src/applications/Mixcore/mixcore.csproj +++ b/src/applications/Mixcore/mixcore.csproj @@ -85,8 +85,18 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + + + + + diff --git a/src/applications/Mixcore/wwwroot/default-mixcontent/templates/_ViewImports.cshtml b/src/applications/Mixcore/wwwroot/default-mixcontent/templates/_ViewImports.cshtml new file mode 100644 index 000000000..afd1e2cc9 --- /dev/null +++ b/src/applications/Mixcore/wwwroot/default-mixcontent/templates/_ViewImports.cshtml @@ -0,0 +1,16 @@ +@using Mixcore +@using Mixcore.Models +@using Mix.Shared.Services +@using Mix.Lib.Services +@using Mix.Lib.Helpers +@using Mix.Constant.Constants +@using Mix.Heart.Services +@using Mix.Heart.Models +@using Mix.Heart.Enums +@using Mix.Service.Services +@using Mixcore.Domain.ViewModels +@using Mixcore.Domain.Services +@using Mix.Lib.Interfaces +@using Mix.Database.Entities.Cms +@using Mix.Services.Databases.Lib.Services +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/src/applications/Mixcore/wwwroot/default-mixcontent/templates/_ViewStart.cshtml b/src/applications/Mixcore/wwwroot/default-mixcontent/templates/_ViewStart.cshtml new file mode 100644 index 000000000..be2a4f37a --- /dev/null +++ b/src/applications/Mixcore/wwwroot/default-mixcontent/templates/_ViewStart.cshtml @@ -0,0 +1,3 @@ +@{ + Layout = null; +} diff --git a/src/platform/mix.constant/Constants/MixFolders.cs b/src/platform/mix.constant/Constants/MixFolders.cs index c1959986b..7d6a0ad45 100644 --- a/src/platform/mix.constant/Constants/MixFolders.cs +++ b/src/platform/mix.constant/Constants/MixFolders.cs @@ -4,7 +4,7 @@ public class MixFolders { public const string WebRootPath = "wwwroot"; public const string StaticFiles = "mixcontent/staticfiles"; - public const string TemplatesFolder = "mixcontent/templates";//"Views/Shared/Templates" + public const string TemplatesFolder = "mixcontent/templates"; public const string TempFolder = "mixcontent/temp"; public const string ThemePackage = "mixcontent/temp/themes"; public const string PortalApps = "portal-apps"; diff --git a/src/platform/mix.library/Services/MixThemeImportService.cs b/src/platform/mix.library/Services/MixThemeImportService.cs index 264e8c88c..b2f739ff1 100644 --- a/src/platform/mix.library/Services/MixThemeImportService.cs +++ b/src/platform/mix.library/Services/MixThemeImportService.cs @@ -257,12 +257,15 @@ private async Task ImportConfigurations() await ImportContentDataAsync(_siteData.ConfigurationContents, _dicConfigurationContentIds, _dicConfigurationIds); } - private async Task ImportMixDatabases(MixCmsContext dbContext, IMixDbService mixDbService) + private async Task ImportMixDatabases(MixCmsContext dbContext, IMixDbService mixDbService, CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + await ImportDatabaseContextsAsync(dbContext); await ImportDatabasesAsync(dbContext); await ImportDatabaseRelationshipsAsync(dbContext); await MigrateMixDatabaseAsync(mixDbService); + await MigrateSystemMixDatabaseAsync(mixDbService, cancellationToken); } private async Task MigrateMixDatabaseAsync(IMixDbService mixDbService) @@ -275,6 +278,11 @@ private async Task MigrateMixDatabaseAsync(IMixDbService mixDbService) } } } + + private async Task MigrateSystemMixDatabaseAsync(IMixDbService mixDbService, CancellationToken cancellationToken = default) + { + await mixDbService.MigrateSystemDatabases(cancellationToken); + } private async Task ImportPosts() { diff --git a/src/platform/mix.repodb/Interfaces/IMixDbService.cs b/src/platform/mix.repodb/Interfaces/IMixDbService.cs index e92c11136..63018352a 100644 --- a/src/platform/mix.repodb/Interfaces/IMixDbService.cs +++ b/src/platform/mix.repodb/Interfaces/IMixDbService.cs @@ -20,6 +20,6 @@ public interface IMixDbService public Task BackupDatabase(string databaseName, CancellationToken cancellationToken = default); Task GetByParentIdAsync(string tableName, MixContentType parentType, int parentId, bool loadNestedData); Task ParseDataAsync(string tableName, dynamic obj); - Task MigrateSystemDatabases(); + Task MigrateSystemDatabases(CancellationToken cancellationToken = default); } } diff --git a/src/platform/mix.repodb/ServiceCollectionExtensions.cs b/src/platform/mix.repodb/ServiceCollectionExtensions.cs index 48a582811..9ba1ed62b 100644 --- a/src/platform/mix.repodb/ServiceCollectionExtensions.cs +++ b/src/platform/mix.repodb/ServiceCollectionExtensions.cs @@ -22,11 +22,6 @@ public static IServiceCollection AddMixRepoDb(this IServiceCollection services, services.AddHostedService(); services.AddHostedService(); - if (!globalConfig.IsInit && globalConfig.IsUpdateSystemDatabases) - { - var mixDbService = services.GetService(); - mixDbService.MigrateSystemDatabases().GetAwaiter().GetResult(); - } return services; } } diff --git a/src/platform/mix.repodb/Services/MixDbService.cs b/src/platform/mix.repodb/Services/MixDbService.cs index 60fb1177d..14331a679 100644 --- a/src/platform/mix.repodb/Services/MixDbService.cs +++ b/src/platform/mix.repodb/Services/MixDbService.cs @@ -525,8 +525,12 @@ private async Task SwitchDbContext(int dbContextId) } - public async Task MigrateSystemDatabases() + // Only run after init CMS success + // TODO: Add version to systemDatabases and update if have newer version + public async Task MigrateSystemDatabases(CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + var strMixDbs = MixFileHelper.GetFile( "system-databases", MixFileExtensions.Json, MixFolders.JsonDataFolder); var obj = JObject.Parse(strMixDbs.Content); @@ -536,29 +540,26 @@ public async Task MigrateSystemDatabases() { foreach (var database in databases) { - if (!await CheckTableExist(database.SystemName, _repository)) + if (!_cmsUow.DbContext.MixDatabase.Any(m => m.SystemName == database.SystemName)) { - MixDatabaseViewModel currentDb = await MixDatabaseViewModel.GetRepository(_cmsUow, CacheService) - .GetSingleAsync(m => m.SystemName == database.SystemName); - if (currentDb == null) - { - currentDb = new(database, _cmsUow); - currentDb.Id = 0; - currentDb.MixTenantId = CurrentTenant?.Id ?? 1; - currentDb.CreatedDateTime = DateTime.UtcNow; - currentDb.Columns = new(); - if (columns is not null) + MixDatabaseViewModel currentDb = new(database, _cmsUow); + currentDb.Id = 0; + currentDb.MixTenantId = CurrentTenant?.Id ?? 1; + currentDb.CreatedDateTime = DateTime.UtcNow; + currentDb.Columns = new(); + + if (columns is not null) + { + var cols = columns.Where(c => c.MixDatabaseName == database.SystemName).ToList(); + foreach (var col in cols) { - var cols = columns.Where(c => c.MixDatabaseName == database.SystemName).ToList(); - foreach (var col in cols) - { - currentDb.Columns.Add(new(col, _cmsUow)); - } + col.Id = 0; + currentDb.Columns.Add(new(col, _cmsUow)); } - - await currentDb.SaveAsync(); } + + await currentDb.SaveAsync(cancellationToken); if (currentDb is { Columns.Count: > 0 }) { await Migrate(currentDb, _databaseService.DatabaseProvider, _repository); @@ -701,18 +702,6 @@ private string GetInsertQuery(ExpandoObject obj, List selectMembers) return await _repository.GetAllAsync(cancellationToken); } - private async Task CheckTableExist(string tableName, MixRepoDbRepository repo) - { - try - { - var count = await repo.ExecuteCommand($"SELECT Count(*) FROM {_databaseConstant.BacktickOpen}{tableName}{_databaseConstant.BacktickClose};"); - return true; - } - catch - { - return false; - } - } private async Task Migrate(MixDatabaseViewModel database, MixDatabaseProvider databaseProvider, MixRepoDbRepository repo)