cleaning up migrations. we're deleting the table so no need to do a bunch of migration stuff instead of just compiling it into one migration deleting and creating the table again.

This commit is contained in:
Claus
2016-08-04 13:46:39 +02:00
parent 16afab86f3
commit 59c3d6a206
5 changed files with 26 additions and 194 deletions

View File

@@ -1,7 +1,6 @@
using System.Linq;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence.Migrations.Syntax.Create;
using Umbraco.Core.Persistence.SqlSyntax;
namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFiveZero
@@ -21,24 +20,38 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFiveZer
private string MigrationCode(Database database)
{
// don't execute if the table is already there
var tables = SqlSyntax.GetTablesInSchema(database).ToArray();
if (tables.InvariantContains("umbracoRedirectUrl")) return null;
var umbracoRedirectUrlTableName = "umbracoRedirectUrl";
var localContext = new LocalMigrationContext(Context.CurrentDatabaseProvider, database, SqlSyntax, Logger);
localContext.Create.Table("umbracoRedirectUrl")
.WithColumn("id").AsInt32().Identity().PrimaryKey("PK_umbracoRedirectUrl")
.WithColumn("contentId").AsInt32().NotNullable()
var tables = SqlSyntax.GetTablesInSchema(database).ToArray();
if (tables.InvariantContains(umbracoRedirectUrlTableName))
{
localContext.Delete.Table(umbracoRedirectUrlTableName);
}
localContext.Create.Table(umbracoRedirectUrlTableName)
.WithColumn("id").AsGuid().NotNullable().PrimaryKey("PK_" + umbracoRedirectUrlTableName)
.WithColumn("createDateUtc").AsDateTime().NotNullable()
.WithColumn("url").AsString(2048).NotNullable();
.WithColumn("url").AsString(2048).NotNullable()
.WithColumn("contentKey").AsGuid().NotNullable()
.WithColumn("urlHash").AsString(16).NotNullable();
localContext.Create.Index("IX_umbracoRedirectUrl")
.OnTable("umbracoRedirectUrl")
.OnColumn("url").Ascending()
.OnColumn("createDateUtc").Ascending()
localContext.Create.Index("IX_" + umbracoRedirectUrlTableName).OnTable(umbracoRedirectUrlTableName)
.OnColumn("urlHash")
.Ascending()
.OnColumn("contentKey")
.Ascending()
.OnColumn("createDateUtc")
.Descending()
.WithOptions().NonClustered();
localContext.Create.ForeignKey("FK_" + umbracoRedirectUrlTableName)
.FromTable(umbracoRedirectUrlTableName).ForeignColumn("contentKey")
.ToTable("umbracoNode").PrimaryColumn("uniqueID");
return localContext.GetSql();
}

View File

@@ -1,62 +0,0 @@
using System.Linq;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence.Migrations.Syntax.Alter;
using Umbraco.Core.Persistence.Migrations.Syntax.Create;
using Umbraco.Core.Persistence.Migrations.Syntax.Delete;
using Umbraco.Core.Persistence.Migrations.Syntax.Execute;
using Umbraco.Core.Persistence.SqlSyntax;
namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFiveZero
{
[Migration("7.5.0", 101, GlobalSettings.UmbracoMigrationName)]
public class AddRedirectUrlTable2 : MigrationBase
{
public AddRedirectUrlTable2(ISqlSyntaxProvider sqlSyntax, ILogger logger)
: base(sqlSyntax, logger)
{ }
public override void Up()
{
// defer, because we are making decisions based upon what's in the database
Execute.Code(MigrationCode);
}
private string MigrationCode(Database database)
{
var columns = SqlSyntax.GetColumnsInSchema(database).ToArray();
if (columns.Any(x => x.TableName.InvariantEquals("umbracoRedirectUrl") && x.ColumnName.InvariantEquals("contentKey")))
return null;
var localContext = new LocalMigrationContext(Context.CurrentDatabaseProvider, database, SqlSyntax, Logger);
localContext.Execute.Sql("DELETE FROM umbracoRedirectUrl"); // else cannot add non-nullable field
var keyConstraints = SqlSyntax.GetConstraintsPerColumn(database).Distinct();
var fk= keyConstraints
.SingleOrDefault(x => x.Item1 == "umbracoRedirectUrl" && x.Item2 == "contentId" && x.Item3.InvariantStartsWith("PK_") == false);
if (fk != null)
localContext.Delete.ForeignKey(fk.Item3).OnTable("umbracoRedirectUrl");
localContext.Delete.Column("contentId").FromTable("umbracoRedirectUrl");
// SQL CE does not want to alter-add non-nullable columns ;-(
// but it's OK to create as nullable then alter, go figure
//localContext.Alter.Table("umbracoRedirectUrl")
// .AddColumn("contentKey").AsGuid().NotNullable();
localContext.Alter.Table("umbracoRedirectUrl")
.AddColumn("contentKey").AsGuid().Nullable();
localContext.Alter.Table("umbracoRedirectUrl")
.AlterColumn("contentKey").AsGuid().NotNullable();
localContext.Create.ForeignKey("FK_umbracoRedirectUrl")
.FromTable("umbracoRedirectUrl").ForeignColumn("contentKey")
.ToTable("umbracoNode").PrimaryColumn("uniqueID");
return localContext.GetSql();
}
public override void Down()
{ }
}
}

View File

@@ -1,57 +0,0 @@
using System.Linq;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence.SqlSyntax;
namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFiveZero
{
[Migration("7.5.0", 102, GlobalSettings.UmbracoMigrationName)]
public class AddRedirectUrlTable3 : MigrationBase
{
public AddRedirectUrlTable3(ISqlSyntaxProvider sqlSyntax, ILogger logger)
: base(sqlSyntax, logger)
{ }
public override void Up()
{
// defer, because we are making decisions based upon what's in the database
Execute.Code(MigrationCode);
}
private string MigrationCode(Database database)
{
var columns = SqlSyntax.GetColumnsInSchema(database).ToArray();
if (columns.Any(x => x.TableName.InvariantEquals("umbracoRedirectUrl") && x.ColumnName.InvariantEquals("hurl")))
return null;
var localContext = new LocalMigrationContext(Context.CurrentDatabaseProvider, database, SqlSyntax, Logger);
localContext.Execute.Sql("DELETE FROM umbracoRedirectUrl"); // else cannot add non-nullable field
localContext.Delete.Index("IX_umbracoRedirectUrl").OnTable("umbracoRedirectUrl");
// SQL CE does not want to alter-add non-nullable columns ;-(
// but it's OK to create as nullable then alter, go figure
//localContext.Alter.Table("umbracoRedirectUrl")
// .AddColumn("urlHash").AsString(16).NotNullable();
localContext.Alter.Table("umbracoRedirectUrl")
.AddColumn("hurl").AsString(16).Nullable();
localContext.Alter.Table("umbracoRedirectUrl")
.AlterColumn("hurl").AsString(16).NotNullable();
localContext.Create.Index("IX_umbracoRedirectUrl").OnTable("umbracoRedirectUrl")
.OnColumn("hurl")
.Ascending()
.OnColumn("contentKey")
.Ascending()
.OnColumn("createDateUtc")
.Descending()
.WithOptions().NonClustered();
return localContext.GetSql();
}
public override void Down()
{ }
}
}

View File

@@ -1,59 +0,0 @@
using System.Linq;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence.SqlSyntax;
namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFiveZero
{
[Migration("7.5.0", 103, GlobalSettings.UmbracoMigrationName)]
public class AddRedirectUrlTable4 : MigrationBase
{
public AddRedirectUrlTable4(ISqlSyntaxProvider sqlSyntax, ILogger logger)
: base(sqlSyntax, logger)
{ }
public override void Up()
{
// defer, because we are making decisions based upon what's in the database
Execute.Code(MigrationCode);
}
private string MigrationCode(Database database)
{
var columns = SqlSyntax.GetColumnsInSchema(database).ToArray();
if (columns.Any(x => x.TableName.InvariantEquals("umbracoRedirectUrl") && x.ColumnName.InvariantEquals("urlHash")))
return null;
var localContext = new LocalMigrationContext(Context.CurrentDatabaseProvider, database, SqlSyntax, Logger);
localContext.Execute.Sql("DELETE FROM umbracoRedirectUrl"); // else cannot add non-nullable field
localContext.Delete.Index("IX_umbracoRedirectUrl").OnTable("umbracoRedirectUrl");
localContext.Delete.Column("hurl").FromTable("umbracoRedirectUrl");
// SQL CE does not want to alter-add non-nullable columns ;-(
// but it's OK to create as nullable then alter, go figure
//localContext.Alter.Table("umbracoRedirectUrl")
// .AddColumn("urlHash").AsString(16).NotNullable();
localContext.Alter.Table("umbracoRedirectUrl")
.AddColumn("urlHash").AsString(16).Nullable();
localContext.Alter.Table("umbracoRedirectUrl")
.AlterColumn("urlHash").AsString(16).NotNullable();
localContext.Create.Index("IX_umbracoRedirectUrl").OnTable("umbracoRedirectUrl")
.OnColumn("urlHash")
.Ascending()
.OnColumn("contentKey")
.Ascending()
.OnColumn("createDateUtc")
.Descending()
.WithOptions().NonClustered();
return localContext.GetSql();
}
public override void Down()
{ }
}
}