include theme resources on server page load, add IUpgradeable interface to provide site-based versioning support

This commit is contained in:
Shaun Walker
2022-04-01 17:57:30 -04:00
parent bbb547efb6
commit fc12903cfd
8 changed files with 193 additions and 8 deletions

View File

@ -190,6 +190,10 @@ namespace Oqtane.Infrastructure
if (result.Success)
{
result = CreateSite(install);
if (result.Success)
{
result = MigrateSites();
}
}
}
}
@ -665,6 +669,78 @@ namespace Oqtane.Infrastructure
return result;
}
private Installation MigrateSites()
{
var result = new Installation { Success = false, Message = string.Empty };
// find upgrade type
Type upgradetype = null;
var assemblies = AppDomain.CurrentDomain.GetOqtaneAssemblies();
foreach (Assembly assembly in assemblies)
{
var types = assembly.GetTypes().Where(item => item.GetInterfaces().Contains(typeof(IUpgradeable)));
if (types.Any())
{
upgradetype = types.First();
break;
}
}
// execute upgrade
if (upgradetype != null)
{
var obj = Activator.CreateInstance(upgradetype) as IUpgradeable;
if (obj != null)
{
using (var scope = _serviceScopeFactory.CreateScope())
{
var aliases = scope.ServiceProvider.GetRequiredService<IAliasRepository>();
var tenantManager = scope.ServiceProvider.GetRequiredService<ITenantManager>();
var sites = scope.ServiceProvider.GetRequiredService<ISiteRepository>();
foreach (var alias in aliases.GetAliases().ToList().Where(item => item.IsDefault))
{
var versions = obj.GetVersions(alias);
if (!string.IsNullOrEmpty(versions))
{
tenantManager.SetTenant(alias.TenantId);
var site = sites.GetSites().FirstOrDefault(item => item.SiteId == alias.SiteId);
if (site != null)
{
foreach (var version in versions.Split(',', StringSplitOptions.RemoveEmptyEntries))
{
if (string.IsNullOrEmpty(site.Version) || Version.Parse(version) > Version.Parse(site.Version))
{
if (obj.Upgrade(alias, version))
{
site.Version = version;
sites.UpdateSite(site);
}
else
{
result.Message = "An Error Occurred Executing IUpgradeable Interface For " + alias.Name + " For Version " + version;
}
}
}
}
}
}
}
}
}
if (string.IsNullOrEmpty(result.Message))
{
result.Success = true;
}
else
{
_filelogger.LogError(Utilities.LogMessage(this, result.Message));
}
return result;
}
private string DenormalizeConnectionString(string connectionString)
{
var dataDirectory = AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString();

View File

@ -1,5 +1,3 @@
using Microsoft.EntityFrameworkCore;
using Oqtane.Enums;
using Oqtane.Models;
namespace Oqtane.Infrastructure

View File

@ -0,0 +1,11 @@
using Oqtane.Models;
namespace Oqtane.Infrastructure
{
public interface IUpgradeable
{
string GetVersions(Alias alias);
bool Upgrade(Alias alias, string version);
}
}

View File

@ -0,0 +1,40 @@
using Oqtane.Models;
using Oqtane.Documentation;
namespace Oqtane.Infrastructure
{
[PrivateApi("Mark Site-Template classes as private, since it's not very useful in the public docs")]
public class ExampleUpgrade : IUpgradeable
{
string IUpgradeable.GetVersions(Alias alias)
{
var versions = "";
switch (alias.Name)
{
case "localhost:44357":
// return the list of official release versions for the specific site
versions = "1.0.0";
break;
}
return versions;
}
bool IUpgradeable.Upgrade(Alias alias, string version)
{
bool success = true;
switch (alias.Name)
{
case "localhost:44357":
switch (version)
{
case "1.0.0":
// execute some version-specific upgrade logic for the site here such as adding pages, modules, content, etc...
success = true;
break;
}
break;
}
return success;
}
}
}