From 9e6a2d8a7c156fa98dff66ee63b64d35a28bf32c Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 11 Aug 2015 15:56:48 +0200 Subject: [PATCH] Fixing: U4-6942 During upgrade MUST make sure that the new Property Type GUIDs are consistent ... just needs some testing now; --- .../AddUniqueIdPropertyTypeColumn.cs | 19 ++++++++++++++++++- src/Umbraco.Core/StringExtensions.cs | 13 +++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenThreeZero/AddUniqueIdPropertyTypeColumn.cs b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenThreeZero/AddUniqueIdPropertyTypeColumn.cs index f08719a308..f2f9261f52 100644 --- a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenThreeZero/AddUniqueIdPropertyTypeColumn.cs +++ b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenThreeZero/AddUniqueIdPropertyTypeColumn.cs @@ -30,9 +30,26 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenThreeZe .NonClustered() .WithOptions() .Unique(); + + //now we need to fill in the data so that it is consistent, we can't have it generating random GUIDs for + // the already existing data, see: http://issues.umbraco.org/issue/U4-6942 + + foreach (var data in Context.Database.Query(@" +SELECT cmsPropertyType.id ptId, cmsPropertyType.Alias ptAlias, cmsContentType.alias ctAlias +FROM cmsPropertyType +INNER JOIN cmsContentType +ON cmsPropertyType.contentTypeId = cmsContentType.nodeId")) + { + //create a guid from the concatenation of the property type alias + the doc type alias + string concatAlias = data.ptAlias + data.ctAlias; + var ptGuid = concatAlias.ToGuid(); + + //set the Unique Id to the one we've generated + Update.Table("cmsPropertyType").Set(new {uniqueID = ptGuid}).Where(new {id = data.ptId }); + } } - + } public override void Down() diff --git a/src/Umbraco.Core/StringExtensions.cs b/src/Umbraco.Core/StringExtensions.cs index e54164bd39..9ee33a286b 100644 --- a/src/Umbraco.Core/StringExtensions.cs +++ b/src/Umbraco.Core/StringExtensions.cs @@ -1409,5 +1409,18 @@ namespace Umbraco.Core { return string.IsNullOrEmpty(text) ? text : InvalidXmlChars.Replace(text, ""); } + + /// + /// Converts a string to a Guid - WARNING, depending on the string, this may not be unique + /// + /// + /// + internal static Guid ToGuid(this string text) + { + var md5 = MD5.Create(); + byte[] myStringBytes = Encoding.ASCII.GetBytes(text); + byte[] hash = md5.ComputeHash(myStringBytes); + return new Guid(hash); + } } }