Updated the Installation of Oqtane to use Migrations

This commit is contained in:
Charles Nurse
2021-03-21 14:52:45 -07:00
parent 83e5502111
commit 8f1c760e87
27 changed files with 390 additions and 232 deletions

View File

@ -1,40 +1,64 @@
using Oqtane.Infrastructure;
using System;
using Oqtane.Infrastructure;
using Oqtane.Models;
using Oqtane.Repository;
using Oqtane.Modules.HtmlText.Models;
using Oqtane.Modules.HtmlText.Repository;
using System.Net;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Oqtane.Enums;
// ReSharper disable ConvertToUsingDeclaration
namespace Oqtane.Modules.HtmlText.Manager
{
public class HtmlTextManager : IInstallable, IPortable
public class HtmlTextManager : IMigratable, IPortable
{
private IHtmlTextRepository _htmlTexts;
private ISqlRepository _sql;
private readonly IHtmlTextRepository _htmlText;
private readonly ISqlRepository _sql;
public HtmlTextManager(IHtmlTextRepository htmltexts, ISqlRepository sql)
public HtmlTextManager(IHtmlTextRepository htmlText, ISqlRepository sql)
{
_htmlTexts = htmltexts;
_htmlText = htmlText;
_sql = sql;
}
public bool Install(Tenant tenant, string version)
public bool Migrate(Tenant tenant, MigrationType migrationType)
{
return _sql.ExecuteScript(tenant, GetType().Assembly, "HtmlText." + version + ".sql");
}
var result = true;
public bool Uninstall(Tenant tenant)
{
return _sql.ExecuteScript(tenant, GetType().Assembly, "HtmlText.Uninstall.sql");
var dbConfig = new DbConfig(null, null) {ConnectionString = tenant.DBConnectionString};
using (var db = new HtmlTextContext(dbConfig, null))
{
try
{
var migrator = db.GetService<IMigrator>();
if (migrationType == MigrationType.Down)
{
migrator.Migrate(Migration.InitialDatabase);
}
else
{
migrator.Migrate();
}
}
catch (Exception e)
{
Console.WriteLine(e);
result = false;
}
}
return result;
}
public string ExportModule(Module module)
{
string content = "";
HtmlTextInfo htmltext = _htmlTexts.GetHtmlText(module.ModuleId);
if (htmltext != null)
var htmlText = _htmlText.GetHtmlText(module.ModuleId);
if (htmlText != null)
{
content = WebUtility.HtmlEncode(htmltext.Content);
content = WebUtility.HtmlEncode(htmlText.Content);
}
return content;
}
@ -42,18 +66,18 @@ namespace Oqtane.Modules.HtmlText.Manager
public void ImportModule(Module module, string content, string version)
{
content = WebUtility.HtmlDecode(content);
HtmlTextInfo htmltext = _htmlTexts.GetHtmlText(module.ModuleId);
if (htmltext != null)
var htmlText = _htmlText.GetHtmlText(module.ModuleId);
if (htmlText != null)
{
htmltext.Content = content;
_htmlTexts.UpdateHtmlText(htmltext);
htmlText.Content = content;
_htmlText.UpdateHtmlText(htmlText);
}
else
{
htmltext = new HtmlTextInfo();
htmltext.ModuleId = module.ModuleId;
htmltext.Content = content;
_htmlTexts.AddHtmlText(htmltext);
htmlText = new HtmlTextInfo();
htmlText.ModuleId = module.ModuleId;
htmlText.Content = content;
_htmlText.AddHtmlText(htmlText);
}
}
}

View File

@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Oqtane.Modules.HtmlText.Migrations.EntityBuilders;
using Oqtane.Modules.HtmlText.Repository;
namespace Oqtane.Modules.HtmlText.Migrations
{
[DbContext(typeof(HtmlTextContext))]
[Migration("HtmlText.01.00.00.00")]
public class InitializeModule : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
//Create HtmlText table
var entityBuilder = new HtmlTextEntityBuilder(migrationBuilder);
entityBuilder.Create();
}
protected override void Down(MigrationBuilder migrationBuilder)
{
//Drop HtmlText table
var entityBuilder = new HtmlTextEntityBuilder(migrationBuilder);
entityBuilder.Drop();
}
}
}

View File

@ -0,0 +1,43 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Migrations.Operations.Builders;
using Oqtane.Migrations;
using Oqtane.Migrations.EntityBuilders;
using Oqtane.Migrations.Extensions;
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
namespace Oqtane.Modules.HtmlText.Migrations.EntityBuilders
{
public class HtmlTextEntityBuilder : AuditableBaseEntityBuilder<HtmlTextEntityBuilder>
{
private const string _entityTableName = "HtmlText";
private readonly PrimaryKey<HtmlTextEntityBuilder> _primaryKey = new("PK_HtmlText", x => x.HtmlTextId);
private readonly ForeignKey<HtmlTextEntityBuilder> _moduleForeignKey = new("FK_HtmlText_Module", x => x.ModuleId, "Module", "ModuleId", ReferentialAction.Cascade);
public HtmlTextEntityBuilder(MigrationBuilder migrationBuilder) : base(migrationBuilder)
{
EntityTableName = _entityTableName;
PrimaryKey = _primaryKey;
ForeignKeys.Add(_moduleForeignKey);
}
protected override HtmlTextEntityBuilder BuildTable(ColumnsBuilder table)
{
HtmlTextId = table.AddAutoIncrementColumn("HtmlTextId");
ModuleId = table.AddIntegerColumn("ModuleId");
Content = table.AddMaxStringColumn("Content");
AddAuditableColumns(table);
return this;
}
public OperationBuilder<AddColumnOperation> HtmlTextId { get; set; }
public OperationBuilder<AddColumnOperation> ModuleId { get; set; }
public OperationBuilder<AddColumnOperation> Content { get; set; }
}
}

View File

@ -10,7 +10,7 @@ namespace Oqtane.Modules.HtmlText.Repository
{
public virtual DbSet<HtmlTextInfo> HtmlText { get; set; }
public HtmlTextContext(ITenantResolver tenantResolver, IHttpContextAccessor accessor, IConfiguration configuration) : base(tenantResolver, accessor, configuration)
public HtmlTextContext(IDbConfig dbConfig, ITenantResolver tenantResolver) : base(dbConfig, tenantResolver)
{
// ContextBase handles multi-tenant database connections
}

View File

@ -1,19 +0,0 @@
CREATE TABLE [dbo].[HtmlText](
[HtmlTextId] [int] IDENTITY(1,1) NOT NULL,
[ModuleId] [int] NOT NULL,
[Content] [nvarchar](max) NOT NULL,
[CreatedBy] [nvarchar](256) NOT NULL,
[CreatedOn] [datetime] NOT NULL,
[ModifiedBy] [nvarchar](256) NOT NULL,
[ModifiedOn] [datetime] NOT NULL,
CONSTRAINT [PK_HtmlText] PRIMARY KEY CLUSTERED
(
[HtmlTextId] ASC
)
)
GO
ALTER TABLE [dbo].[HtmlText] WITH CHECK ADD CONSTRAINT [FK_HtmlText_Module] FOREIGN KEY([ModuleId])
REFERENCES [dbo].[Module] ([ModuleId])
ON DELETE CASCADE
GO

View File

@ -1,2 +0,0 @@
DROP TABLE [dbo].[HtmlText]
GO