Skip to content

Commit

Permalink
Merge pull request #702 from mixcore/features/fix-migrate-sytem-datab…
Browse files Browse the repository at this point in the history
…ases

fix sync system databases
  • Loading branch information
nhathoang989 authored Oct 11, 2023
2 parents 157e5db + 7d46035 commit b65a188
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/develop_dev-mixcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions src/applications/Mixcore/mixcore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,18 @@
<EmbeddedResource Include="Views\_ViewStart.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<Resource Include="wwwroot\default-mixcontent\templates\_ViewImports.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<Resource Include="wwwroot\default-mixcontent\templates\_ViewStart.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<ItemGroup>
<Compile Remove="logs\**" />
</ItemGroup>
<ItemGroup>
<Content Remove="wwwroot\default-mixcontent\templates\_ViewImports.cshtml" />
<Content Remove="wwwroot\default-mixcontent\templates\_ViewStart.cshtml" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = null;
}
2 changes: 1 addition & 1 deletion src/platform/mix.constant/Constants/MixFolders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
10 changes: 9 additions & 1 deletion src/platform/mix.library/Services/MixThemeImportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion src/platform/mix.repodb/Interfaces/IMixDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public interface IMixDbService
public Task<bool> BackupDatabase(string databaseName, CancellationToken cancellationToken = default);
Task<JObject?> GetByParentIdAsync(string tableName, MixContentType parentType, int parentId, bool loadNestedData);
Task<JObject> ParseDataAsync(string tableName, dynamic obj);
Task<bool> MigrateSystemDatabases();
Task<bool> MigrateSystemDatabases(CancellationToken cancellationToken = default);
}
}
5 changes: 0 additions & 5 deletions src/platform/mix.repodb/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ public static IServiceCollection AddMixRepoDb(this IServiceCollection services,
services.AddHostedService<MixRepoDbPublisher>();
services.AddHostedService<MixRepoDbSubscriber>();

if (!globalConfig.IsInit && globalConfig.IsUpdateSystemDatabases)
{
var mixDbService = services.GetService<IMixDbService>();
mixDbService.MigrateSystemDatabases().GetAwaiter().GetResult();
}
return services;
}
}
Expand Down
51 changes: 20 additions & 31 deletions src/platform/mix.repodb/Services/MixDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,12 @@ private async Task SwitchDbContext(int dbContextId)

}

public async Task<bool> MigrateSystemDatabases()
// Only run after init CMS success
// TODO: Add version to systemDatabases and update if have newer version
public async Task<bool> MigrateSystemDatabases(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

var strMixDbs = MixFileHelper.GetFile(
"system-databases", MixFileExtensions.Json, MixFolders.JsonDataFolder);
var obj = JObject.Parse(strMixDbs.Content);
Expand All @@ -536,29 +540,26 @@ public async Task<bool> 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);
Expand Down Expand Up @@ -701,18 +702,6 @@ private string GetInsertQuery(ExpandoObject obj, List<string> selectMembers)
return await _repository.GetAllAsync(cancellationToken);
}

private async Task<bool> 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<bool> Migrate(MixDatabaseViewModel database,
MixDatabaseProvider databaseProvider,
MixRepoDbRepository repo)
Expand Down

0 comments on commit b65a188

Please sign in to comment.