Added ability to execute version specific code during framework upgrade (removed ApplicationVersion table and replaced with Version field on Tenant table), updated version number to 0.9.0 and renamed install scripts to match - this will be a baseline release which will be upgradeable
This commit is contained in:
@ -26,7 +26,6 @@ namespace Oqtane.Infrastructure
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
private readonly IMemoryCache _cache;
|
||||
|
||||
|
||||
public DatabaseManager(IConfigurationRoot config, IServiceScopeFactory serviceScopeFactory, IMemoryCache cache)
|
||||
{
|
||||
_config = config;
|
||||
@ -217,7 +216,6 @@ namespace Oqtane.Infrastructure
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
CreateApplicationVersion(install.ConnectionString);
|
||||
UpdateConnectionString(install.ConnectionString);
|
||||
}
|
||||
}
|
||||
@ -277,21 +275,43 @@ namespace Oqtane.Infrastructure
|
||||
{
|
||||
var result = new Installation { Success = false, Message = string.Empty };
|
||||
|
||||
using (var db = new InstallationContext(NormalizeConnectionString(_config.GetConnectionString(SettingKeys.ConnectionStringKey))))
|
||||
{
|
||||
foreach (var tenant in db.Tenant.ToList())
|
||||
{
|
||||
var upgradeConfig = DeployChanges.To.SqlDatabase(NormalizeConnectionString(tenant.DBConnectionString))
|
||||
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly(), s => s.Contains("Tenant") && s.EndsWith(".sql",StringComparison.OrdinalIgnoreCase));
|
||||
string[] versions = Constants.ReleaseVersions.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
var upgrade = upgradeConfig.Build();
|
||||
if (upgrade.IsUpgradeRequired())
|
||||
using (var scope = _serviceScopeFactory.CreateScope())
|
||||
{
|
||||
var upgrades = scope.ServiceProvider.GetRequiredService<IUpgradeManager>();
|
||||
|
||||
using (var db = new InstallationContext(NormalizeConnectionString(_config.GetConnectionString(SettingKeys.ConnectionStringKey))))
|
||||
{
|
||||
foreach (var tenant in db.Tenant.ToList())
|
||||
{
|
||||
var upgradeResult = upgrade.PerformUpgrade();
|
||||
result.Success = upgradeResult.Successful;
|
||||
if (!result.Success)
|
||||
var upgradeConfig = DeployChanges.To.SqlDatabase(NormalizeConnectionString(tenant.DBConnectionString))
|
||||
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly(), s => s.Contains("Tenant.") && s.EndsWith(".sql", StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
var upgrade = upgradeConfig.Build();
|
||||
if (upgrade.IsUpgradeRequired())
|
||||
{
|
||||
result.Message = upgradeResult.Error.Message;
|
||||
var upgradeResult = upgrade.PerformUpgrade();
|
||||
result.Success = upgradeResult.Successful;
|
||||
if (!result.Success)
|
||||
{
|
||||
result.Message = upgradeResult.Error.Message;
|
||||
}
|
||||
}
|
||||
|
||||
// execute any version specific upgrade logic
|
||||
string version = tenant.Version;
|
||||
int index = Array.FindIndex(versions, item => item == version);
|
||||
if (index != (versions.Length - 1))
|
||||
{
|
||||
if (index == -1) index = 0;
|
||||
for (int i = index; i < versions.Length; i++)
|
||||
{
|
||||
upgrades.Upgrade(tenant, versions[i]);
|
||||
}
|
||||
tenant.Version = versions[versions.Length - 1];
|
||||
db.Entry(tenant).State = EntityState.Modified;
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -460,6 +480,9 @@ namespace Oqtane.Infrastructure
|
||||
aliases.UpdateAlias(alias);
|
||||
}
|
||||
|
||||
tenant.Version = Constants.Version;
|
||||
tenants.UpdateTenant(tenant);
|
||||
|
||||
log.Log(site.SiteId, LogLevel.Trace, this, LogFunction.Create, "Site Created {Site}", site);
|
||||
}
|
||||
}
|
||||
@ -469,20 +492,6 @@ namespace Oqtane.Infrastructure
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void CreateApplicationVersion(string connectionString)
|
||||
{
|
||||
using (var db = new InstallationContext(NormalizeConnectionString(connectionString)))
|
||||
{
|
||||
var version = db.ApplicationVersion.FirstOrDefault(item => item.Version == Constants.Version);
|
||||
if (version == null)
|
||||
{
|
||||
version = new ApplicationVersion { Version = Constants.Version, CreatedOn = DateTime.UtcNow };
|
||||
db.ApplicationVersion.Add(version);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string NormalizeConnectionString(string connectionString)
|
||||
{
|
||||
|
@ -0,0 +1,9 @@
|
||||
using Oqtane.Models;
|
||||
|
||||
namespace Oqtane.Infrastructure
|
||||
{
|
||||
public interface IUpgradeManager
|
||||
{
|
||||
void Upgrade(Tenant tenant, string version);
|
||||
}
|
||||
}
|
93
Oqtane.Server/Infrastructure/UpgradeManager.cs
Normal file
93
Oqtane.Server/Infrastructure/UpgradeManager.cs
Normal file
@ -0,0 +1,93 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Oqtane.Extensions;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Repository;
|
||||
using Oqtane.Shared;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Oqtane.Infrastructure
|
||||
{
|
||||
public class UpgradeManager : IUpgradeManager
|
||||
{
|
||||
private readonly IAliasRepository _aliases;
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
|
||||
public UpgradeManager(IAliasRepository aliases, IServiceScopeFactory serviceScopeFactory)
|
||||
{
|
||||
_aliases = aliases;
|
||||
_serviceScopeFactory = serviceScopeFactory;
|
||||
}
|
||||
|
||||
public void Upgrade(Tenant tenant, string version)
|
||||
{
|
||||
// core framework upgrade logic - note that you can check if current tenant is Master if you only want to execute logic once
|
||||
var pageTemplates = new List<PageTemplate>();
|
||||
|
||||
switch (version)
|
||||
{
|
||||
case "0.9.0":
|
||||
// add a page to all existing sites on upgrade
|
||||
|
||||
//pageTemplates.Add(new PageTemplate
|
||||
//{
|
||||
// Name = "Test",
|
||||
// Parent = "",
|
||||
// Path = "test",
|
||||
// Icon = Icons.Badge,
|
||||
// IsNavigation = true,
|
||||
// IsPersonalizable = false,
|
||||
// EditMode = false,
|
||||
// PagePermissions = new List<Permission>
|
||||
// {
|
||||
// new Permission(PermissionNames.View, Constants.AdminRole, true),
|
||||
// new Permission(PermissionNames.View, Constants.AllUsersRole, true),
|
||||
// new Permission(PermissionNames.Edit, Constants.AdminRole, true)
|
||||
// }.EncodePermissions(),
|
||||
// PageTemplateModules = new List<PageTemplateModule>
|
||||
// {
|
||||
// new PageTemplateModule
|
||||
// {
|
||||
// ModuleDefinitionName = typeof(Oqtane.Modules.Admin.Login.Index).ToModuleDefinitionName(), Title = "Test", Pane = "Content",
|
||||
// ModulePermissions = new List<Permission>
|
||||
// {
|
||||
// new Permission(PermissionNames.View, Constants.AdminRole, true),
|
||||
// new Permission(PermissionNames.View, Constants.AllUsersRole, true),
|
||||
// new Permission(PermissionNames.Edit, Constants.AdminRole, true)
|
||||
// }.EncodePermissions(),
|
||||
// Content = ""
|
||||
// }
|
||||
// }
|
||||
//});
|
||||
CreateSitePages(tenant, pageTemplates);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateSitePages(Tenant tenant, List<PageTemplate> pageTemplates)
|
||||
{
|
||||
if (pageTemplates.Count != 0)
|
||||
{
|
||||
var processed = new List<Site>();
|
||||
foreach (Alias alias in _aliases.GetAliases().Where(item => item.TenantId == tenant.TenantId))
|
||||
{
|
||||
if (!processed.Exists(item => item.SiteId == alias.SiteId))
|
||||
{
|
||||
using (var scope = _serviceScopeFactory.CreateScope())
|
||||
{
|
||||
var siteState = scope.ServiceProvider.GetRequiredService<SiteState>();
|
||||
siteState.Alias = alias;
|
||||
var sites = scope.ServiceProvider.GetRequiredService<ISiteRepository>();
|
||||
var site = sites.GetSite(alias.SiteId);
|
||||
if (site != null)
|
||||
{
|
||||
sites.CreatePages(site, pageTemplates);
|
||||
}
|
||||
processed.Add(site);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user