-
Notifications
You must be signed in to change notification settings - Fork 0
/
NestedContentKeyMigrator.cs
79 lines (67 loc) · 3.68 KB
/
NestedContentKeyMigrator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Skybrud.Essentials.Json.Extensions;
using Umbraco.Core;
using Umbraco.Core.Logging;
public static class NestedContentKeyMigrator
{
private class Row
{
public int id { get; set; }
public int contentNodeId { get; set; }
public string alias { get; set; }
public string dataNvarchar { get; set; }
public string dataNtext { get; set; }
public int? dataInt { get; set; }
public string preValue { get; set; }
}
public static void MigrateKeys(ApplicationContext applicationContext)
{
var database = applicationContext.DatabaseContext.Database;
string sql = @"SELECT cmsPropertyData.id, cmsPropertyData.contentNodeId, cmsPropertyType.alias, dataNvarchar, dataNtext, dataInt, cmsDocument.*
FROM cmsPropertyData
JOIN cmsPropertyType ON cmsPropertyType.id = cmsPropertyData.propertytypeid
JOIN cmsDataType ON cmsDataType.nodeId = cmsPropertyType.dataTypeId
JOIN cmsContentVersion ON cmsContentVersion.VersionId = cmsPropertyData.versionId
JOIN umbracoNode ON umbracoNode.id = cmsContentVersion.ContentId
JOIN cmsDocument ON cmsDocument.nodeId = umbracoNode.id
WHERE cmsDataType.propertyEditorAlias IN ('Umbraco.NestedContent')
AND (dataNtext IS NOT NULL AND dataNtext NOT LIKE '%""key"":""%')
AND(cmsDocument.published = 1 OR cmsDocument.newest = 1 OR cmsDocument.updateDate > (SELECT updateDate FROM cmsDocument AS innerDoc WHERE innerDoc.nodeId = cmsDocument.nodeId AND innerDoc.published = 1 AND newest = 1))
ORDER BY contentNodeId, cmsDataType.propertyEditorAlias";
var ncDataToMigrate = database.Query<Row>(sql).ToList();
if (ncDataToMigrate.Any())
{
foreach (var propertyData in ncDataToMigrate)
{
if (string.IsNullOrEmpty(propertyData.dataNtext))
{
LogHelper.Info(typeof(NestedContentKeyMigrator), () => $"MigrateKeys (node id: {propertyData.contentNodeId}) skipping property {propertyData.alias} - null dataNtext");
continue;
}
var ncArray = JArray.Parse(propertyData.dataNtext);
var ncKeyArray = new JArray();
foreach (var ncToken in ncArray)
{
var nc = (JObject) ncToken;
if (!nc.HasValue("key"))
{
var key = new JProperty("key", Guid.NewGuid().ToString());
nc.AddFirst(key);
}
ncKeyArray.Add(nc);
}
var ncKeys = JsonConvert.SerializeObject(ncKeyArray);
LogHelper.Info(typeof(NestedContentKeyMigrator), () => $"MigrateKeys (node id: {propertyData.contentNodeId}) converting property {propertyData.alias} from {propertyData.dataNtext} to {ncKeys}");
database.Execute("UPDATE cmsPropertyData SET dataNtext=@0 WHERE id=@1", ncKeys, propertyData.id);
}
LogHelper.Info(typeof(NestedContentKeyMigrator), () => $"MigrateKeys: republishing all nodes to update xml cache (equivalent to /umbraco/dialogs/republish.aspx?xml=true)");
var contentService = ApplicationContext.Current.Services.ContentService;
contentService.RePublishAll();
umbraco.library.RefreshContent();
LogHelper.Info(typeof(NestedContentKeyMigrator), () => $"MigrateKeys: republishing complete");
}
}
}