handle HtmlText module transition from SQL scripts to Migrations in module rather than in core framework
This commit is contained in:
		@ -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"
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
                            }
 | 
			
		||||
 | 
			
		||||
@ -454,6 +454,7 @@ namespace Oqtane.Infrastructure
 | 
			
		||||
            {
 | 
			
		||||
                var moduleDefinitions = scope.ServiceProvider.GetRequiredService<IModuleDefinitionRepository>();
 | 
			
		||||
                var sql = scope.ServiceProvider.GetRequiredService<ISqlRepository>();
 | 
			
		||||
                var tenantManager = scope.ServiceProvider.GetRequiredService<ITenantManager>();
 | 
			
		||||
 | 
			
		||||
                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]);
 | 
			
		||||
                                                }
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
@ -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);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user