From afcc5e217017a34f880fb1760e889239b9d344d6 Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Sun, 30 May 2021 13:16:26 -0400 Subject: [PATCH] handle HtmlText module transition from SQL scripts to Migrations in module rather than in core framework --- Oqtane.Client/Modules/HtmlText/ModuleInfo.cs | 4 ++-- .../Controllers/ModuleDefinitionController.cs | 5 +++- .../Infrastructure/DatabaseManager.cs | 5 ++-- .../HtmlText/Manager/HtmlTextManager.cs | 23 ++++++++----------- Oqtane.Server/Modules/MigratableModuleBase.cs | 10 +++++++- Oqtane.Server/Scripts/MigrateTenant.sql | 2 -- .../Server/Manager/[Module]Manager.cs | 2 -- 7 files changed, 28 insertions(+), 23 deletions(-) diff --git a/Oqtane.Client/Modules/HtmlText/ModuleInfo.cs b/Oqtane.Client/Modules/HtmlText/ModuleInfo.cs index 5e998f96..fbe6cf9e 100644 --- a/Oqtane.Client/Modules/HtmlText/ModuleInfo.cs +++ b/Oqtane.Client/Modules/HtmlText/ModuleInfo.cs @@ -8,9 +8,9 @@ namespace Oqtane.Modules.HtmlText { Name = "HtmlText", Description = "Renders HTML or Text Content", - Version = "1.0.0", + Version = "1.0.1", ServerManagerType = "Oqtane.Modules.HtmlText.Manager.HtmlTextManager, Oqtane.Server", - ReleaseVersions = "1.0.0", + ReleaseVersions = "1.0.0,1.0.1", SettingsType = "Oqtane.Modules.HtmlText.Settings, Oqtane.Client" }; } diff --git a/Oqtane.Server/Controllers/ModuleDefinitionController.cs b/Oqtane.Server/Controllers/ModuleDefinitionController.cs index 622366ec..140df3f3 100644 --- a/Oqtane.Server/Controllers/ModuleDefinitionController.cs +++ b/Oqtane.Server/Controllers/ModuleDefinitionController.cs @@ -27,9 +27,10 @@ namespace Oqtane.Controllers private readonly IInstallationManager _installationManager; private readonly IWebHostEnvironment _environment; private readonly IServiceProvider _serviceProvider; + private readonly ITenantManager _tenantManager; private readonly ILogManager _logger; - public ModuleDefinitionController(IModuleDefinitionRepository moduleDefinitions, ITenantRepository tenants, ISqlRepository sql, IUserPermissions userPermissions, IInstallationManager installationManager, IWebHostEnvironment environment, IServiceProvider serviceProvider, ILogManager logger) + public ModuleDefinitionController(IModuleDefinitionRepository moduleDefinitions, ITenantRepository tenants, ISqlRepository sql, IUserPermissions userPermissions, IInstallationManager installationManager, IWebHostEnvironment environment, IServiceProvider serviceProvider, ITenantManager tenantManager, ILogManager logger) { _moduleDefinitions = moduleDefinitions; _tenants = tenants; @@ -38,6 +39,7 @@ namespace Oqtane.Controllers _installationManager = installationManager; _environment = environment; _serviceProvider = serviceProvider; + _tenantManager = tenantManager; _logger = logger; } @@ -111,6 +113,7 @@ namespace Oqtane.Controllers { if (moduletype.GetInterface("IInstallable") != null) { + _tenantManager.SetTenant(tenant.TenantId); var moduleobject = ActivatorUtilities.CreateInstance(_serviceProvider, moduletype); ((IInstallable)moduleobject).Uninstall(tenant); } diff --git a/Oqtane.Server/Infrastructure/DatabaseManager.cs b/Oqtane.Server/Infrastructure/DatabaseManager.cs index 1eca23e9..2a11c98f 100644 --- a/Oqtane.Server/Infrastructure/DatabaseManager.cs +++ b/Oqtane.Server/Infrastructure/DatabaseManager.cs @@ -454,6 +454,7 @@ namespace Oqtane.Infrastructure { var moduleDefinitions = scope.ServiceProvider.GetRequiredService(); var sql = scope.ServiceProvider.GetRequiredService(); + var tenantManager = scope.ServiceProvider.GetRequiredService(); foreach (var moduleDefinition in moduleDefinitions.GetModuleDefinitions()) { @@ -474,13 +475,13 @@ namespace Oqtane.Infrastructure } if (index != (versions.Length - 1)) { - if (index == -1) index = 0; - for (var i = index; i < versions.Length; i++) + for (var i = (index + 1); i < versions.Length; i++) { try { if (moduleType.GetInterface("IInstallable") != null) { + tenantManager.SetTenant(tenant.TenantId); var moduleObject = ActivatorUtilities.CreateInstance(scope.ServiceProvider, moduleType) as IInstallable; moduleObject?.Install(tenant, versions[i]); } diff --git a/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs b/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs index f3612841..ba190654 100644 --- a/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs +++ b/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs @@ -1,16 +1,11 @@ -using System; -using System.Collections.Generic; using Oqtane.Infrastructure; using Oqtane.Models; -using Oqtane.Repository; -using Oqtane.Modules.HtmlText.Models; using Oqtane.Modules.HtmlText.Repository; using System.Net; using Microsoft.AspNetCore.Http; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; using Oqtane.Enums; -using Oqtane.Interfaces; +using Oqtane.Repository; +using Oqtane.Shared; // ReSharper disable ConvertToUsingDeclaration @@ -21,13 +16,14 @@ namespace Oqtane.Modules.HtmlText.Manager private readonly IHtmlTextRepository _htmlText; private readonly ITenantManager _tenantManager; private readonly IHttpContextAccessor _accessor; + private readonly ISqlRepository _sqlRepository; - - public HtmlTextManager(IHtmlTextRepository htmlText, ITenantManager tenantManager, IHttpContextAccessor httpContextAccessor) + public HtmlTextManager(IHtmlTextRepository htmlText, ITenantManager tenantManager, IHttpContextAccessor httpContextAccessor, ISqlRepository sqlRepository) { _htmlText = htmlText; _tenantManager = tenantManager; _accessor = httpContextAccessor; + _sqlRepository = sqlRepository; } public string ExportModule(Module module) @@ -61,15 +57,16 @@ namespace Oqtane.Modules.HtmlText.Manager public bool Install(Tenant tenant, string version) { - _tenantManager.SetTenant(tenant.TenantId); - + if (tenant.DBType == Constants.DefaultDBType && version == "1.0.1") + { + // version 1.0.0 used SQL scripts rather than migrations, so we need to seed the migration history table + AddMigrationHistory(_sqlRepository, tenant, "HtmlText.01.00.00.00"); + } return Migrate(new HtmlTextContext(_tenantManager, _accessor), tenant, MigrationType.Up); } public bool Uninstall(Tenant tenant) { - _tenantManager.SetTenant(tenant.TenantId); - return Migrate(new HtmlTextContext(_tenantManager, _accessor), tenant, MigrationType.Down); } } diff --git a/Oqtane.Server/Modules/MigratableModuleBase.cs b/Oqtane.Server/Modules/MigratableModuleBase.cs index c282d324..d97cc86d 100644 --- a/Oqtane.Server/Modules/MigratableModuleBase.cs +++ b/Oqtane.Server/Modules/MigratableModuleBase.cs @@ -3,8 +3,8 @@ using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; using Oqtane.Enums; using Oqtane.Models; -using Oqtane.Modules.HtmlText.Repository; using Oqtane.Repository; +using Oqtane.Shared; namespace Oqtane.Modules { @@ -38,5 +38,13 @@ namespace Oqtane.Modules return result; } + + public void AddMigrationHistory(ISqlRepository sqlRepository, Tenant tenant, string MigrationId) + { + var query = "IF NOT EXISTS(SELECT 1 FROM __EFMigrationsHistory WHERE MigrationId = '" + MigrationId + "') "; + query += "INSERT INTO __EFMigrationsHistory(MigrationId, ProductVersion, AppliedDate, AppliedVersion) "; + query += "VALUES('" + MigrationId + "', '5.0.0', SYSDATETIME(), '" + Constants.Version + "')"; + sqlRepository.ExecuteNonQuery(tenant, query); + } } } diff --git a/Oqtane.Server/Scripts/MigrateTenant.sql b/Oqtane.Server/Scripts/MigrateTenant.sql index 408172ca..28717566 100644 --- a/Oqtane.Server/Scripts/MigrateTenant.sql +++ b/Oqtane.Server/Scripts/MigrateTenant.sql @@ -20,6 +20,4 @@ IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'dbo.SchemaVersion FROM SchemaVersions WHERE ScriptName LIKE 'Oqtane.Scripts.Tenant.01%' OR ScriptName LIKE 'Oqtane.Scripts.Tenant.02%' - INSERT INTO __EFMigrationsHistory(MigrationId, ProductVersion, AppliedDate, AppliedVersion) - VALUES ('HtmlText.01.00.00.00', '5.0.0', SYSDATETIME(), '{{Version}}') END \ No newline at end of file diff --git a/Oqtane.Server/wwwroot/Modules/Templates/External/Server/Manager/[Module]Manager.cs b/Oqtane.Server/wwwroot/Modules/Templates/External/Server/Manager/[Module]Manager.cs index 24fa1a91..e9092589 100644 --- a/Oqtane.Server/wwwroot/Modules/Templates/External/Server/Manager/[Module]Manager.cs +++ b/Oqtane.Server/wwwroot/Modules/Templates/External/Server/Manager/[Module]Manager.cs @@ -25,13 +25,11 @@ namespace [Owner].[Module].Manager public bool Install(Tenant tenant, string version) { - _tenantManager.SetTenant(tenant.TenantId); return Migrate(new [Module]Context(_tenantManager, _accessor), tenant, MigrationType.Up); } public bool Uninstall(Tenant tenant) { - _tenantManager.SetTenant(tenant.TenantId); return Migrate(new [Module]Context(_tenantManager, _accessor), tenant, MigrationType.Down); }