handle HtmlText module transition from SQL scripts to Migrations in module rather than in core framework

This commit is contained in:
Shaun Walker
2021-05-30 13:16:26 -04:00
parent 967f0fe626
commit afcc5e2170
7 changed files with 28 additions and 23 deletions

View File

@ -8,9 +8,9 @@ namespace Oqtane.Modules.HtmlText
{ {
Name = "HtmlText", Name = "HtmlText",
Description = "Renders HTML or Text Content", Description = "Renders HTML or Text Content",
Version = "1.0.0", Version = "1.0.1",
ServerManagerType = "Oqtane.Modules.HtmlText.Manager.HtmlTextManager, Oqtane.Server", 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" SettingsType = "Oqtane.Modules.HtmlText.Settings, Oqtane.Client"
}; };
} }

View File

@ -27,9 +27,10 @@ namespace Oqtane.Controllers
private readonly IInstallationManager _installationManager; private readonly IInstallationManager _installationManager;
private readonly IWebHostEnvironment _environment; private readonly IWebHostEnvironment _environment;
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
private readonly ITenantManager _tenantManager;
private readonly ILogManager _logger; 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; _moduleDefinitions = moduleDefinitions;
_tenants = tenants; _tenants = tenants;
@ -38,6 +39,7 @@ namespace Oqtane.Controllers
_installationManager = installationManager; _installationManager = installationManager;
_environment = environment; _environment = environment;
_serviceProvider = serviceProvider; _serviceProvider = serviceProvider;
_tenantManager = tenantManager;
_logger = logger; _logger = logger;
} }
@ -111,6 +113,7 @@ namespace Oqtane.Controllers
{ {
if (moduletype.GetInterface("IInstallable") != null) if (moduletype.GetInterface("IInstallable") != null)
{ {
_tenantManager.SetTenant(tenant.TenantId);
var moduleobject = ActivatorUtilities.CreateInstance(_serviceProvider, moduletype); var moduleobject = ActivatorUtilities.CreateInstance(_serviceProvider, moduletype);
((IInstallable)moduleobject).Uninstall(tenant); ((IInstallable)moduleobject).Uninstall(tenant);
} }

View File

@ -454,6 +454,7 @@ namespace Oqtane.Infrastructure
{ {
var moduleDefinitions = scope.ServiceProvider.GetRequiredService<IModuleDefinitionRepository>(); var moduleDefinitions = scope.ServiceProvider.GetRequiredService<IModuleDefinitionRepository>();
var sql = scope.ServiceProvider.GetRequiredService<ISqlRepository>(); var sql = scope.ServiceProvider.GetRequiredService<ISqlRepository>();
var tenantManager = scope.ServiceProvider.GetRequiredService<ITenantManager>();
foreach (var moduleDefinition in moduleDefinitions.GetModuleDefinitions()) foreach (var moduleDefinition in moduleDefinitions.GetModuleDefinitions())
{ {
@ -474,13 +475,13 @@ namespace Oqtane.Infrastructure
} }
if (index != (versions.Length - 1)) if (index != (versions.Length - 1))
{ {
if (index == -1) index = 0; for (var i = (index + 1); i < versions.Length; i++)
for (var i = index; i < versions.Length; i++)
{ {
try try
{ {
if (moduleType.GetInterface("IInstallable") != null) if (moduleType.GetInterface("IInstallable") != null)
{ {
tenantManager.SetTenant(tenant.TenantId);
var moduleObject = ActivatorUtilities.CreateInstance(scope.ServiceProvider, moduleType) as IInstallable; var moduleObject = ActivatorUtilities.CreateInstance(scope.ServiceProvider, moduleType) as IInstallable;
moduleObject?.Install(tenant, versions[i]); moduleObject?.Install(tenant, versions[i]);
} }

View File

@ -1,16 +1,11 @@
using System;
using System.Collections.Generic;
using Oqtane.Infrastructure; using Oqtane.Infrastructure;
using Oqtane.Models; using Oqtane.Models;
using Oqtane.Repository;
using Oqtane.Modules.HtmlText.Models;
using Oqtane.Modules.HtmlText.Repository; using Oqtane.Modules.HtmlText.Repository;
using System.Net; using System.Net;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Oqtane.Enums; using Oqtane.Enums;
using Oqtane.Interfaces; using Oqtane.Repository;
using Oqtane.Shared;
// ReSharper disable ConvertToUsingDeclaration // ReSharper disable ConvertToUsingDeclaration
@ -21,13 +16,14 @@ namespace Oqtane.Modules.HtmlText.Manager
private readonly IHtmlTextRepository _htmlText; private readonly IHtmlTextRepository _htmlText;
private readonly ITenantManager _tenantManager; private readonly ITenantManager _tenantManager;
private readonly IHttpContextAccessor _accessor; private readonly IHttpContextAccessor _accessor;
private readonly ISqlRepository _sqlRepository;
public HtmlTextManager(IHtmlTextRepository htmlText, ITenantManager tenantManager, IHttpContextAccessor httpContextAccessor, ISqlRepository sqlRepository)
public HtmlTextManager(IHtmlTextRepository htmlText, ITenantManager tenantManager, IHttpContextAccessor httpContextAccessor)
{ {
_htmlText = htmlText; _htmlText = htmlText;
_tenantManager = tenantManager; _tenantManager = tenantManager;
_accessor = httpContextAccessor; _accessor = httpContextAccessor;
_sqlRepository = sqlRepository;
} }
public string ExportModule(Module module) public string ExportModule(Module module)
@ -61,15 +57,16 @@ namespace Oqtane.Modules.HtmlText.Manager
public bool Install(Tenant tenant, string version) 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); return Migrate(new HtmlTextContext(_tenantManager, _accessor), tenant, MigrationType.Up);
} }
public bool Uninstall(Tenant tenant) public bool Uninstall(Tenant tenant)
{ {
_tenantManager.SetTenant(tenant.TenantId);
return Migrate(new HtmlTextContext(_tenantManager, _accessor), tenant, MigrationType.Down); return Migrate(new HtmlTextContext(_tenantManager, _accessor), tenant, MigrationType.Down);
} }
} }

View File

@ -3,8 +3,8 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
using Oqtane.Enums; using Oqtane.Enums;
using Oqtane.Models; using Oqtane.Models;
using Oqtane.Modules.HtmlText.Repository;
using Oqtane.Repository; using Oqtane.Repository;
using Oqtane.Shared;
namespace Oqtane.Modules namespace Oqtane.Modules
{ {
@ -38,5 +38,13 @@ namespace Oqtane.Modules
return result; 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);
}
} }
} }

View File

@ -20,6 +20,4 @@ IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'dbo.SchemaVersion
FROM SchemaVersions FROM SchemaVersions
WHERE ScriptName LIKE 'Oqtane.Scripts.Tenant.01%' WHERE ScriptName LIKE 'Oqtane.Scripts.Tenant.01%'
OR ScriptName LIKE 'Oqtane.Scripts.Tenant.02%' 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 END

View File

@ -25,13 +25,11 @@ namespace [Owner].[Module].Manager
public bool Install(Tenant tenant, string version) public bool Install(Tenant tenant, string version)
{ {
_tenantManager.SetTenant(tenant.TenantId);
return Migrate(new [Module]Context(_tenantManager, _accessor), tenant, MigrationType.Up); return Migrate(new [Module]Context(_tenantManager, _accessor), tenant, MigrationType.Up);
} }
public bool Uninstall(Tenant tenant) public bool Uninstall(Tenant tenant)
{ {
_tenantManager.SetTenant(tenant.TenantId);
return Migrate(new [Module]Context(_tenantManager, _accessor), tenant, MigrationType.Down); return Migrate(new [Module]Context(_tenantManager, _accessor), tenant, MigrationType.Down);
} }