Merge pull request #1086 from umbraco/temp-U4-7598

U4-7598 - fix migrations during 4.7 to 7.4 upgrade
This commit is contained in:
Shannon Deminick
2016-02-04 11:10:45 +01:00
3 changed files with 83 additions and 54 deletions

View File

@@ -37,9 +37,9 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSeven
if (!dataTypeIds.Any()) return string.Empty;
var propertyData =
database.Fetch<PropertyDataDto>(
"WHERE propertyTypeId in (SELECT id from cmsPropertyType where dataTypeID IN (@dataTypeIds))", new { dataTypeIds = dataTypeIds });
// need to use dynamic, as PropertyDataDto has new properties
var propertyData = database.Fetch<dynamic>("SELECT * FROM cmsPropertyData"
+ " WHERE propertyTypeId in (SELECT id from cmsPropertyType where dataTypeID IN (@dataTypeIds))", new { dataTypeIds = dataTypeIds });
if (!propertyData.Any()) return string.Empty;
var nodesIdsWithProperty = propertyData.Select(x => x.NodeId).Distinct().ToArray();
@@ -71,13 +71,16 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSeven
xml = new XmlDocument();
xml.LoadXml(data.Text);
}
catch (Exception ex)
catch (Exception ex)
{
Logger.Error<UpdateRelatedLinksData>("The data stored for property id " + data.Id + " on document " + data.NodeId +
" is not valid XML, the data will be removed because it cannot be converted to the new format. The value was: " + data.Text, ex);
int dataId = data.id;
int dataNodeId = data.nodeId;
string dataText = data.dataNText;
Logger.Error<UpdateRelatedLinksData>("The data stored for property id " + dataId + " on document " + dataNodeId +
" is not valid XML, the data will be removed because it cannot be converted to the new format. The value was: " + dataText, ex);
data.Text = "";
database.Update(data);
data.dataNText = "";
database.Update("cmsPropertyData", "id", data, new[] { "dataNText" });
UpdateXmlTable(propertyTypes, data, cmsContentXmlEntries, database);
@@ -91,11 +94,11 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSeven
{
var title = node.Attributes["title"].Value;
var type = node.Attributes["type"].Value;
var newwindow = node.Attributes["newwindow"].Value.Equals("1") ? true : false;
var newwindow = node.Attributes["newwindow"].Value.Equals("1");
var lnk = node.Attributes["link"].Value;
//create the links in the format the new prop editor expects it to be
var link = new ExpandoObject() as IDictionary<string, Object>;
var link = new ExpandoObject() as IDictionary<string, object>;
link.Add("title", title);
link.Add("caption", title);
link.Add("link", lnk);
@@ -110,9 +113,9 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSeven
}
//store the serialized data
data.Text = JsonConvert.SerializeObject(links);
data.dataNText = JsonConvert.SerializeObject(links);
database.Update(data);
database.Update("cmsPropertyData", "id", data, new[] { "dataNText" });
UpdateXmlTable(propertyTypes, data, cmsContentXmlEntries, database);

View File

@@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
@@ -33,45 +35,57 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFourZer
// fill in the data in a way that is consistent over all environments
// (ie cannot use random guids, http://issues.umbraco.org/issue/U4-6942)
Execute.Code(UpdateGuids);
}
}
foreach (var data in Context.Database.Query<dynamic>(@"
private static string UpdateGuids(Database database)
{
var updates = new List<Tuple<Guid, int>>();
foreach (var data in database.Query<dynamic>(@"
SELECT cmsPropertyTypeGroup.id grId, cmsPropertyTypeGroup.text grName, cmsContentType.alias ctAlias, umbracoNode.nodeObjectType nObjType
FROM cmsPropertyTypeGroup
INNER JOIN cmsContentType
ON cmsPropertyTypeGroup.contentTypeNodeId = cmsContentType.nodeId
INNER JOIN umbracoNode
ON cmsContentType.nodeId = umbracoNode.id"))
{
Guid guid;
// see BaseDataCreation... built-in groups have their own guids
if (data.grId == 3)
{
Guid guid;
// see BaseDataCreation... built-in groups have their own guids
if (data.grId == 3)
{
guid = new Guid(Constants.PropertyTypeGroups.Image);
}
else if (data.grId == 4)
{
guid = new Guid(Constants.PropertyTypeGroups.File);
}
else if (data.grId == 5)
{
guid = new Guid(Constants.PropertyTypeGroups.Contents);
}
else if (data.grId == 11)
{
guid = new Guid(Constants.PropertyTypeGroups.Membership);
}
else
{
// create a consistent guid from
// group name + content type alias + object type
string guidSource = data.grName + data.ctAlias + data.nObjType;
guid = guidSource.ToGuid();
}
// set the Unique Id to the one we've generated
Update.Table("cmsPropertyTypeGroup").Set(new { uniqueID = guid }).Where(new { id = data.grId });
guid = new Guid(Constants.PropertyTypeGroups.Image);
}
else if (data.grId == 4)
{
guid = new Guid(Constants.PropertyTypeGroups.File);
}
else if (data.grId == 5)
{
guid = new Guid(Constants.PropertyTypeGroups.Contents);
}
else if (data.grId == 11)
{
guid = new Guid(Constants.PropertyTypeGroups.Membership);
}
else
{
// create a consistent guid from
// group name + content type alias + object type
string guidSource = data.grName + data.ctAlias + data.nObjType;
guid = guidSource.ToGuid();
}
// set the Unique Id to the one we've generated
// but not within the foreach loop (as we already have a data reader open)
updates.Add(Tuple.Create(guid, data.grId));
}
foreach (var update in updates)
database.Execute("UPDATE cmsPropertyTypeGroup SET uniqueID=@uid WHERE id=@id", new { uid = update.Item1, id = update.Item2 });
return string.Empty;
}
public override void Down()

View File

@@ -33,36 +33,40 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSixZeroOne
// won't exist yet
var propertyTypes = database.Fetch<dynamic>("SELECT * FROM cmsPropertyType WHERE propertyTypeGroupId > 0");
var propertyGroups = database.Fetch<PropertyTypeGroupDto>("WHERE id > 0");
// need to use dynamic, as PropertyTypeGroupDto has new properties
var propertyGroups = database.Fetch<dynamic>("SELECT * FROM cmsPropertyTypeGroup WHERE id > 0");
foreach (var propertyType in propertyTypes)
{
// get the PropertyTypeGroup of the current PropertyType, skip if not found
var propertyTypeGroup = propertyGroups.FirstOrDefault(x => x.Id == propertyType.propertyTypeGroupId);
var propertyTypeGroup = propertyGroups.FirstOrDefault(x => x.id == propertyType.propertyTypeGroupId);
if (propertyTypeGroup == null) continue;
// if the PropretyTypeGroup belongs to the same content type as the PropertyType, then fine
if (propertyTypeGroup.ContentTypeNodeId == propertyType.contentTypeId) continue;
if (propertyTypeGroup.contenttypeNodeId == propertyType.contentTypeId) continue;
// else we want to assign the PropertyType to a proper PropertyTypeGroup
// ie one that does belong to the same content - look for it
var okPropertyTypeGroup = propertyGroups.FirstOrDefault(x =>
x.Text == propertyTypeGroup.Text && // same name
x.ContentTypeNodeId == propertyType.contentTypeId); // but for proper content type
x.text == propertyTypeGroup.text && // same name
x.contenttypeNodeId == propertyType.contentTypeId); // but for proper content type
if (okPropertyTypeGroup == null)
{
// does not exist, create a new PropertyTypeGroup,
var propertyGroup = new PropertyTypeGroupDto
// does not exist, create a new PropertyTypeGroup
// cannot use a PropertyTypeGroupDto because of the new (not-yet-existing) uniqueID property
// cannot use a dynamic because database.Insert fails to set the value of property
var propertyGroup = new PropertyTypeGroupDtoTemp
{
ContentTypeNodeId = propertyType.contentTypeId,
Text = propertyTypeGroup.Text,
SortOrder = propertyTypeGroup.SortOrder
id = 0,
contenttypeNodeId = propertyType.contentTypeId,
text = propertyTypeGroup.text,
sortorder = propertyTypeGroup.sortorder
};
// save + add to list of groups
int id = Convert.ToInt16(database.Insert(propertyGroup));
propertyGroup.Id = id;
int id = Convert.ToInt16(database.Insert("cmsPropertyTypeGroup", "id", propertyGroup));
propertyGroup.id = id;
propertyGroups.Add(propertyGroup);
// update the PropertyType to use the new PropertyTypeGroup
@@ -71,7 +75,7 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSixZeroOne
else
{
// exists, update PropertyType to use the PropertyTypeGroup
propertyType.propertyTypeGroupId = okPropertyTypeGroup.Id;
propertyType.propertyTypeGroupId = okPropertyTypeGroup.id;
}
database.Update("cmsPropertyType", "id", propertyType);
}
@@ -79,5 +83,13 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSixZeroOne
return string.Empty;
}
private class PropertyTypeGroupDtoTemp
{
public int id { get; set; }
public int contenttypeNodeId { get; set; }
public string text { get; set; }
public int sortorder { get; set; }
}
}
}