Added support for migrating existing Oqtane installations from DbUp to Migrations. Also added a Migration for version 2.0.2, and set current version to 2.1.0

This commit is contained in:
Charles Nurse
2021-04-08 12:20:21 -07:00
parent d12b18350f
commit 8c45b7e42f
11 changed files with 331 additions and 80 deletions

View File

@ -0,0 +1,34 @@
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Oqtane.Interfaces;
using Oqtane.Migrations.EntityBuilders;
using Oqtane.Repository;
namespace Oqtane.Migrations
{
[DbContext(typeof(TenantDBContext))]
[Migration("Tenant.02.00.02.01")]
public class AddSiteGuidToSite : MultiDatabaseMigration
{
public AddSiteGuidToSite(IEnumerable<IOqtaneDatabase> databases) : base(databases)
{
}
protected override void Up(MigrationBuilder migrationBuilder)
{
//Add Column to Site table
var siteEntityBuilder = new SiteEntityBuilder(migrationBuilder, ActiveDatabase);
siteEntityBuilder.AddStringColumn("SiteGuid", 36, true, false);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
//Drop Column from Site table
var siteEntityBuilder = new SiteEntityBuilder(migrationBuilder, ActiveDatabase);
siteEntityBuilder.DropColumn("SiteGuid");
}
}
}

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Oqtane.Interfaces;
using Oqtane.Migrations.EntityBuilders;
using Oqtane.Repository;
namespace Oqtane.Migrations
{
[DbContext(typeof(TenantDBContext))]
[Migration("Tenant.02.01.00.00")]
public class AddAppVersionsTable : MultiDatabaseMigration
{
public AddAppVersionsTable(IEnumerable<IOqtaneDatabase> databases) : base(databases)
{
}
protected override void Up(MigrationBuilder migrationBuilder)
{
//Create AppVersions table
var appVersionsEntityBuilder = new AppVersionsEntityBuilder(migrationBuilder, ActiveDatabase);
appVersionsEntityBuilder.Create();
//Finish SqlServer Migration from DbUp
if (ActiveDatabase.Name == "SqlServer" || ActiveDatabase.Name == "LocalDB")
{
//Version 1.0.0
InsertVersion(migrationBuilder, "01.00.00", "Oqtane.Scripts.Master.00.09.00.00.sql");
//Version 1.0.1
InsertVersion(migrationBuilder, "01.00.01", "Oqtane.Scripts.Master.01.00.01.00.sql");
//Version 1.0.2
InsertVersion(migrationBuilder, "01.00.02", "Oqtane.Scripts.Tenant.01.00.02.01.sql");
//Version 2.0.0
InsertVersion(migrationBuilder, "02.00.00", "Oqtane.Scripts.Tenant.02.00.00.01.sql");
//Version 2.0.1
InsertVersion(migrationBuilder, "02.00.01", "Oqtane.Scripts.Tenant.02.00.01.03.sql");
//Version 2.0.2
InsertVersion(migrationBuilder, "02.00.02", "Oqtane.Scripts.Tenant.02.00.02.01.sql");
//Drop SchemaVersions
migrationBuilder.Sql(@"
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'dbo.SchemaVersions') AND OBJECTPROPERTY(id, N'IsTable') = 1)
BEGIN
DROP TABLE SchemaVersions
END");
}
//Version 2.1.0
migrationBuilder.Sql($@"
INSERT INTO AppVersions(Version, AppliedDate)
VALUES('02.01.00', '{DateTime.UtcNow:u}')
");
}
private void InsertVersion(MigrationBuilder migrationBuilder, string version, string scriptName)
{
migrationBuilder.Sql($@"
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'dbo.SchemaVersions') AND OBJECTPROPERTY(id, N'IsTable') = 1)
BEGIN
INSERT INTO AppVersions(Version, AppliedDate)
SELECT Version = '{version}', Applied As AppliedDate
FROM SchemaVersions
WHERE ScriptName = '{scriptName}'
END
");
}
}
}

View File

@ -0,0 +1,35 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Migrations.Operations.Builders;
using Oqtane.Interfaces;
namespace Oqtane.Migrations.EntityBuilders
{
public class AppVersionsEntityBuilder : BaseEntityBuilder<AppVersionsEntityBuilder>
{
private const string _entityTableName = "AppVersions";
private readonly PrimaryKey<AppVersionsEntityBuilder> _primaryKey = new("PK_AppVersions", x => x.Id);
public AppVersionsEntityBuilder(MigrationBuilder migrationBuilder, IOqtaneDatabase database) : base(migrationBuilder, database)
{
EntityTableName = _entityTableName;
PrimaryKey = _primaryKey;
}
protected override AppVersionsEntityBuilder BuildTable(ColumnsBuilder table)
{
Id = AddAutoIncrementColumn(table,"Id");
Version = AddStringColumn(table,"Version", 10);
AppliedDate = AddDateTimeColumn(table,"AppliedDate");
return this;
}
public OperationBuilder<AddColumnOperation> Id { get; set; }
public OperationBuilder<AddColumnOperation> Version { get; set; }
public OperationBuilder<AddColumnOperation> AppliedDate { get; set; }
}
}

View File

@ -83,29 +83,29 @@ namespace Oqtane.Migrations.EntityBuilders
return table.Column<int>(name: RewriteName(name), nullable: nullable);
}
public void AddMaxStringColumn(string name, int length, bool nullable = false)
public void AddMaxStringColumn(string name, int length, bool nullable = false, bool unicode = true)
{
_migrationBuilder.AddColumn<string>(RewriteName(name), RewriteName(EntityTableName), nullable: nullable, unicode: true);
_migrationBuilder.AddColumn<string>(RewriteName(name), RewriteName(EntityTableName), nullable: nullable, unicode: unicode);
}
protected OperationBuilder<AddColumnOperation> AddMaxStringColumn(ColumnsBuilder table, string name, bool nullable = false)
protected OperationBuilder<AddColumnOperation> AddMaxStringColumn(ColumnsBuilder table, string name, bool nullable = false, bool unicode = true)
{
return table.Column<string>(name: RewriteName(name), nullable: nullable, unicode: true);
return table.Column<string>(name: RewriteName(name), nullable: nullable, unicode: unicode);
}
public void AddStringColumn(string name, int length, bool nullable = false)
public void AddStringColumn(string name, int length, bool nullable = false, bool unicode = true)
{
_migrationBuilder.AddColumn<string>(RewriteName(name), RewriteName(EntityTableName), maxLength: length, nullable: nullable, unicode: true);
_migrationBuilder.AddColumn<string>(RewriteName(name), RewriteName(EntityTableName), maxLength: length, nullable: nullable, unicode: unicode);
}
protected OperationBuilder<AddColumnOperation> AddStringColumn(ColumnsBuilder table, string name, int length, bool nullable = false)
protected OperationBuilder<AddColumnOperation> AddStringColumn(ColumnsBuilder table, string name, int length, bool nullable = false, bool unicode = true)
{
return table.Column<string>(name: RewriteName(name), maxLength: length, nullable: nullable, unicode: true);
return table.Column<string>(name: RewriteName(name), maxLength: length, nullable: nullable, unicode: unicode);
}
public void AlterStringColumn(string name, int length, bool nullable = false)
public void AlterStringColumn(string name, int length, bool nullable = false, bool unicode = true)
{
_migrationBuilder.AlterColumn<string>(RewriteName(name), RewriteName(EntityTableName), maxLength: length, nullable: nullable);
_migrationBuilder.AlterColumn<string>(RewriteName(name), RewriteName(EntityTableName), maxLength: length, nullable: nullable, unicode: unicode);
}
public void DropColumn(string name)