From 79ad33dd6e8b68ceb978a06bb6d7ef883fe50c3b Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 25 Jun 2015 14:35:05 +0200 Subject: [PATCH] Fixes: U4-5673 SQL Azure cannot drop clustered index on upgrade --- .../AdditionalIndexesAndKeys.cs | 55 +++++++++++++++++-- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSixTwoZero/AdditionalIndexesAndKeys.cs b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSixTwoZero/AdditionalIndexesAndKeys.cs index 9ebc3de781..0938d9be91 100644 --- a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSixTwoZero/AdditionalIndexesAndKeys.cs +++ b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSixTwoZero/AdditionalIndexesAndKeys.cs @@ -47,11 +47,33 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSixTwoZero Create.Index("IX_cmsDocument_newest").OnTable("cmsDocument").OnColumn("newest").Ascending().WithOptions().NonClustered(); } - //TODO: We need to fix this for SQL Azure since it does not let you drop any clustered indexes - // Issue: http://issues.umbraco.org/issue/U4-5673 - // Some work around notes: - // http://stackoverflow.com/questions/15872347/alter-clustered-index-column - // https://social.msdn.microsoft.com/Forums/azure/en-US/5cc4b302-fa42-4c62-956a-bbf79dbbd040/changing-clustered-index-in-azure?forum=ssdsgetstarted + //We need to do this for SQL Azure V2 since it does not let you drop any clustered indexes + // Issue: http://issues.umbraco.org/issue/U4-5673 + if (Context.CurrentDatabaseProvider == DatabaseProviders.SqlServer || Context.CurrentDatabaseProvider == DatabaseProviders.SqlAzure) + { + var version = Context.Database.ExecuteScalar("SELECT @@@@VERSION"); + if (version.Contains("Microsoft SQL Azure")) + { + var parts = version.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray(); + if (parts.Length > 1) + { + if (parts[1].StartsWith("11.")) + { + + //we want to drop the umbracoUserLogins_Index index since it is named incorrectly and then re-create it so + // it follows the standard naming convention + if (dbIndexes.Any(x => x.IndexName.InvariantEquals("umbracoUserLogins_Index"))) + { + //It's the old version that doesn't support dropping a clustered index on a table, so we need to do some manual work. + ExecuteSqlAzureSqlForChangingIndex(); + } + + return; + } + } + } + } + //we want to drop the umbracoUserLogins_Index index since it is named incorrectly and then re-create it so // it follows the standard naming convention @@ -72,5 +94,28 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSixTwoZero Delete.Index("IX_cmsDocument_published").OnTable("cmsDocument"); Delete.Index("IX_cmsDocument_newest").OnTable("cmsDocument"); } + + private void ExecuteSqlAzureSqlForChangingIndex() + { + Context.Database.Execute(@"CREATE TABLE ""umbracoUserLogins_temp"" +( + contextID uniqueidentifier NOT NULL, + userID int NOT NULL, + [timeout] bigint NOT NULL +); +CREATE CLUSTERED INDEX ""IX_umbracoUserLogins_Index"" ON ""umbracoUserLogins_temp"" (""contextID""); +INSERT INTO ""umbracoUserLogins_temp"" SELECT * FROM ""umbracoUserLogins"" +DROP TABLE ""umbracoUserLogins"" +CREATE TABLE ""umbracoUserLogins"" +( + contextID uniqueidentifier NOT NULL, + userID int NOT NULL, + [timeout] bigint NOT NULL +); +CREATE CLUSTERED INDEX ""IX_umbracoUserLogins_Index"" ON ""umbracoUserLogins"" (""contextID""); +INSERT INTO ""umbracoUserLogins"" SELECT * FROM ""umbracoUserLogins_temp"" +DROP TABLE ""umbracoUserLogins_temp"""); + + } } } \ No newline at end of file