From 59c3d6a2069e315afea163ee0ebbba346f12ec3d Mon Sep 17 00:00:00 2001 From: Claus Date: Thu, 4 Aug 2016 13:46:39 +0200 Subject: [PATCH] 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. --- .../AddRedirectUrlTable.cs | 37 +++++++---- .../AddRedirectUrlTable2.cs | 62 ------------------- .../AddRedirectUrlTable3.cs | 57 ----------------- .../AddRedirectUrlTable4.cs | 59 ------------------ .../Migrations/MigrationIssuesTests.cs | 5 +- 5 files changed, 26 insertions(+), 194 deletions(-) delete mode 100644 src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable2.cs delete mode 100644 src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable3.cs delete mode 100644 src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable4.cs diff --git a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable.cs b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable.cs index d667d2d91f..983d6709bb 100644 --- a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable.cs +++ b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable.cs @@ -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(); } diff --git a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable2.cs b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable2.cs deleted file mode 100644 index 2ea854d824..0000000000 --- a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable2.cs +++ /dev/null @@ -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() - { } - } -} diff --git a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable3.cs b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable3.cs deleted file mode 100644 index a9550301b2..0000000000 --- a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable3.cs +++ /dev/null @@ -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() - { } - } -} diff --git a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable4.cs b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable4.cs deleted file mode 100644 index e4724fc48f..0000000000 --- a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/AddRedirectUrlTable4.cs +++ /dev/null @@ -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() - { } - } -} diff --git a/src/Umbraco.Tests/Migrations/MigrationIssuesTests.cs b/src/Umbraco.Tests/Migrations/MigrationIssuesTests.cs index d122a3cd25..26db7ef58c 100644 --- a/src/Umbraco.Tests/Migrations/MigrationIssuesTests.cs +++ b/src/Umbraco.Tests/Migrations/MigrationIssuesTests.cs @@ -119,10 +119,7 @@ namespace Umbraco.Tests.Migrations //pass in explicit migrations new DeleteRedirectUrlTable(SqlSyntax, logger), - new AddRedirectUrlTable(SqlSyntax, logger), - new AddRedirectUrlTable2(SqlSyntax, logger), - new AddRedirectUrlTable3(SqlSyntax, logger), - new AddRedirectUrlTable4(SqlSyntax, logger) + new AddRedirectUrlTable(SqlSyntax, logger) ); var db = new UmbracoDatabase("Datasource=|DataDirectory|UmbracoPetaPocoTests.sdf;Flush Interval=1;", "System.Data.SqlServerCe.4.0", Logger);