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:
Shaun Walker
2020-05-01 10:27:14 -04:00
parent dda7121883
commit 7c6dc6d774
23 changed files with 230 additions and 145 deletions

View File

@ -123,7 +123,7 @@ namespace Oqtane.Controllers
}
switch (entityName)
{
case EntityNames.Host:
case EntityNames.Tenant:
authorized = User.IsInRole(Constants.HostRole);
break;
case EntityNames.Site:

View File

@ -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)
{

View File

@ -0,0 +1,9 @@
using Oqtane.Models;
namespace Oqtane.Infrastructure
{
public interface IUpgradeManager
{
void Upgrade(Tenant tenant, string version);
}
}

View 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);
}
}
}
}
}
}
}

View File

@ -4,7 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>7.3</LangVersion>
<Configurations>Debug;Release</Configurations>
<Version>0.0.9</Version>
<Version>0.9.0</Version>
<Product>Oqtane</Product>
<Authors>Shaun Walker</Authors>
<Company>.NET Foundation</Company>
@ -20,10 +20,8 @@
<ItemGroup>
<EmbeddedResource Include="Modules\HtmlText\Scripts\HtmlText.1.0.0.sql" />
<EmbeddedResource Include="Modules\HtmlText\Scripts\HtmlText.Uninstall.sql" />
<EmbeddedResource Include="Scripts\Master.00.00.00.sql" />
<EmbeddedResource Include="Scripts\Master.00.00.01.sql" />
<EmbeddedResource Include="Scripts\Tenant.00.00.00.sql" />
<EmbeddedResource Include="Scripts\Tenant.00.00.01.sql" />
<EmbeddedResource Include="Scripts\Master.0.9.0.sql" />
<EmbeddedResource Include="Scripts\Tenant.0.9.0.sql" />
</ItemGroup>
<ItemGroup>

View File

@ -21,7 +21,5 @@ namespace Oqtane.Repository
public virtual DbSet<Tenant> Tenant { get; set; }
public virtual DbSet<ModuleDefinition> ModuleDefinition { get; set; }
public virtual DbSet<Job> Job { get; set; }
public virtual DbSet<ApplicationVersion> ApplicationVersion { get; set; }
}
}

View File

@ -10,5 +10,6 @@ namespace Oqtane.Repository
Site UpdateSite(Site site);
Site GetSite(int siteId);
void DeleteSite(int siteId);
void CreatePages(Site site, List<PageTemplate> pageTemplates);
}
}

View File

@ -731,7 +731,7 @@ namespace Oqtane.Repository
CreatePages(site, CreateAdminPages());
}
private void CreatePages(Site site, List<PageTemplate> pageTemplates)
public void CreatePages(Site site, List<PageTemplate> pageTemplates)
{
List<ModuleDefinition> moduledefinitions = _moduleDefinitionRepository.GetModuleDefinitions(site.SiteId).ToList();
foreach (PageTemplate pagetemplate in pageTemplates)

View File

@ -7,6 +7,7 @@ CREATE TABLE [dbo].[Tenant](
[TenantId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[DBConnectionString] [nvarchar](1024) NOT NULL,
[Version] [nvarchar](50) NULL,
[CreatedBy] [nvarchar](256) NOT NULL,
[CreatedOn] [datetime] NOT NULL,
[ModifiedBy] [nvarchar](256) NOT NULL,
@ -91,17 +92,6 @@ CREATE TABLE [dbo].[JobLog] (
)
GO
CREATE TABLE [dbo].[ApplicationVersion](
[ApplicationVersionId] [int] IDENTITY(1,1) NOT NULL,
[Version] [nvarchar](50) NOT NULL,
[CreatedOn] [datetime] NOT NULL
CONSTRAINT [PK_ApplicationVersion] PRIMARY KEY CLUSTERED
(
[ApplicationVersionId] ASC
)
)
GO
/*
Create foreign key relationships

View File

@ -1,5 +0,0 @@
/*
schema updates
*/

View File

@ -457,3 +457,72 @@ CREATE UNIQUE NONCLUSTERED INDEX IX_Folder ON [dbo].Folder
[Path]
) ON [PRIMARY]
GO
/*
ASP.NET Identity Minimal Schema
*/
CREATE TABLE [dbo].[AspNetUsers](
[Id] [nvarchar](450) NOT NULL,
[UserName] [nvarchar](256) NULL,
[NormalizedUserName] [nvarchar](256) NULL,
[Email] [nvarchar](256) NULL,
[NormalizedEmail] [nvarchar](256) NULL,
[EmailConfirmed] [bit] NOT NULL,
[PasswordHash] [nvarchar](max) NULL,
[SecurityStamp] [nvarchar](max) NULL,
[ConcurrencyStamp] [nvarchar](max) NULL,
[PhoneNumber] [nvarchar](max) NULL,
[PhoneNumberConfirmed] [bit] NOT NULL,
[TwoFactorEnabled] [bit] NOT NULL,
[LockoutEnd] [datetimeoffset](7) NULL,
[LockoutEnabled] [bit] NOT NULL,
[AccessFailedCount] [int] NOT NULL,
CONSTRAINT [PK_AspNetUsers] PRIMARY KEY CLUSTERED
(
[Id] ASC
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[AspNetUserClaims](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [nvarchar](450) NOT NULL,
[ClaimType] [nvarchar](max) NULL,
[ClaimValue] [nvarchar](max) NULL,
CONSTRAINT [PK_AspNetUserClaims] PRIMARY KEY CLUSTERED
(
[Id] ASC
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_AspNetUserClaims_UserId] ON [dbo].[AspNetUserClaims]
(
[UserId] ASC
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [EmailIndex] ON [dbo].[AspNetUsers]
(
[NormalizedEmail] ASC
) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX [UserNameIndex] ON [dbo].[AspNetUsers]
(
[NormalizedUserName] ASC
)
WHERE ([NormalizedUserName] IS NOT NULL)
ON [PRIMARY]
GO
ALTER TABLE [dbo].[AspNetUserClaims] WITH CHECK ADD CONSTRAINT [FK_AspNetUserClaims_AspNetUsers_UserId] FOREIGN KEY([UserId])
REFERENCES [dbo].[AspNetUsers] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[AspNetUserClaims] CHECK CONSTRAINT [FK_AspNetUserClaims_AspNetUsers_UserId]
GO

View File

@ -1,68 +0,0 @@
/*
ASP.NET Identity Minimal Schema
*/
CREATE TABLE [dbo].[AspNetUsers](
[Id] [nvarchar](450) NOT NULL,
[UserName] [nvarchar](256) NULL,
[NormalizedUserName] [nvarchar](256) NULL,
[Email] [nvarchar](256) NULL,
[NormalizedEmail] [nvarchar](256) NULL,
[EmailConfirmed] [bit] NOT NULL,
[PasswordHash] [nvarchar](max) NULL,
[SecurityStamp] [nvarchar](max) NULL,
[ConcurrencyStamp] [nvarchar](max) NULL,
[PhoneNumber] [nvarchar](max) NULL,
[PhoneNumberConfirmed] [bit] NOT NULL,
[TwoFactorEnabled] [bit] NOT NULL,
[LockoutEnd] [datetimeoffset](7) NULL,
[LockoutEnabled] [bit] NOT NULL,
[AccessFailedCount] [int] NOT NULL,
CONSTRAINT [PK_AspNetUsers] PRIMARY KEY CLUSTERED
(
[Id] ASC
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[AspNetUserClaims](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserId] [nvarchar](450) NOT NULL,
[ClaimType] [nvarchar](max) NULL,
[ClaimValue] [nvarchar](max) NULL,
CONSTRAINT [PK_AspNetUserClaims] PRIMARY KEY CLUSTERED
(
[Id] ASC
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_AspNetUserClaims_UserId] ON [dbo].[AspNetUserClaims]
(
[UserId] ASC
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [EmailIndex] ON [dbo].[AspNetUsers]
(
[NormalizedEmail] ASC
) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX [UserNameIndex] ON [dbo].[AspNetUsers]
(
[NormalizedUserName] ASC
)
WHERE ([NormalizedUserName] IS NOT NULL)
ON [PRIMARY]
GO
ALTER TABLE [dbo].[AspNetUserClaims] WITH CHECK ADD CONSTRAINT [FK_AspNetUserClaims_AspNetUsers_UserId] FOREIGN KEY([UserId])
REFERENCES [dbo].[AspNetUsers] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[AspNetUserClaims] CHECK CONSTRAINT [FK_AspNetUserClaims_AspNetUsers_UserId]
GO

View File

@ -9,7 +9,6 @@ using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -186,6 +185,7 @@ namespace Oqtane
services.AddTransient<IFileRepository, FileRepository>();
services.AddTransient<ISiteTemplateRepository, SiteTemplateRepository>();
services.AddTransient<ISqlRepository, SqlRepository>();
services.AddTransient<IUpgradeManager, UpgradeManager>();
// load the external assemblies into the app domain
services.AddOqtaneModules();