Skip to content

Commit 99eb7ba

Browse files
committed
Add migration for updating an index serialized data following Umbraco upgrade
1 parent 0a1636d commit 99eb7ba

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

src/Umbraco.Cms.Integrations.Search.Algolia/Migrations/RunAlgoliaIndicesMigration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public void Handle(UmbracoApplicationStartingNotification notification)
4343
var migrationPlan = new MigrationPlan("AlgoliaIndices");
4444

4545
migrationPlan.From(string.Empty)
46-
.To<AddAlgoliaIndicesTable>("algoliaindices-db");
46+
.To<AddAlgoliaIndicesTable>("algoliaindices-db")
47+
.To<UpdateAlgoliaIndicesPostUpgrade>("algoliaindices-update-db");
4748

4849
var upgrader = new Upgrader(migrationPlan);
4950
upgrader.Execute(_migrationPlanExecutor, _coreScopeProvider, _keyValueService);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Text.Json;
2+
using Umbraco.Cms.Core.Services;
3+
using Umbraco.Cms.Infrastructure.Migrations;
4+
using Umbraco.Cms.Infrastructure.Scoping;
5+
using Umbraco.Cms.Integrations.Search.Algolia.Models.ContentTypeDtos;
6+
7+
namespace Umbraco.Cms.Integrations.Search.Algolia.Migrations
8+
{
9+
public class UpdateAlgoliaIndicesPostUpgrade : MigrationBase
10+
{
11+
private readonly IScopeProvider _scopeProvider;
12+
private readonly IContentTypeService _contentTypeService;
13+
14+
public UpdateAlgoliaIndicesPostUpgrade(IMigrationContext context, IScopeProvider scopeProvider, IContentTypeService contentTypeService)
15+
: base(context)
16+
{
17+
_scopeProvider = scopeProvider;
18+
_contentTypeService = contentTypeService;
19+
}
20+
21+
protected override void Migrate()
22+
{
23+
using var scope = _scopeProvider.CreateScope();
24+
25+
var indices = scope.Database.Fetch<AlgoliaIndex>();
26+
27+
foreach (var index in indices)
28+
{
29+
bool requiresUpdate = false;
30+
31+
var indexSerializedData = JsonSerializer.Deserialize<IEnumerable<ContentTypeDto>>(index.SerializedData);
32+
if (indexSerializedData == null) continue;
33+
34+
foreach (var data in indexSerializedData.Where(x => x.ContentType != null))
35+
{
36+
requiresUpdate = true;
37+
38+
var contentType = _contentTypeService.Get(data.ContentType!.Alias);
39+
if (contentType != null)
40+
{
41+
data.Id = contentType.Id;
42+
}
43+
44+
data.Icon = data.ContentType!.Icon;
45+
data.Alias = data.ContentType!.Alias;
46+
data.Name = data.ContentType!.Name;
47+
}
48+
49+
if (requiresUpdate)
50+
{
51+
index.SerializedData = JsonSerializer.Serialize(indexSerializedData);
52+
scope.Database.Update(index);
53+
}
54+
}
55+
56+
scope.Complete();
57+
}
58+
}
59+
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
namespace Umbraco.Cms.Integrations.Search.Algolia.Models.ContentTypeDtos
1+
using System.Text.Json.Serialization;
2+
3+
namespace Umbraco.Cms.Integrations.Search.Algolia.Models.ContentTypeDtos
24
{
35
public class ContentTypeDto
46
{
57
public int Id { get; set; }
68

9+
[JsonPropertyName("contentType")]
10+
public ContentEntity? ContentType { get; set; }
11+
712
public string Icon { get; set; }
813

914
public string Alias { get; set; }
1015

16+
[JsonPropertyName("name")]
1117
public string Name { get; set; }
1218

1319
public bool Selected { get; set; }
1420

1521
public bool AllowRemove { get; set; }
1622

23+
[JsonPropertyName("properties")]
1724
public IEnumerable<ContentTypePropertyDto> Properties { get; set; }
1825
}
1926
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
namespace Umbraco.Cms.Integrations.Search.Algolia.Models.ContentTypeDtos
1+
using System.Text.Json.Serialization;
2+
3+
namespace Umbraco.Cms.Integrations.Search.Algolia.Models.ContentTypeDtos
24
{
35
public class ContentTypePropertyDto
46
{
7+
[JsonPropertyName("id")]
58
public int Id { get; set; }
69

10+
[JsonPropertyName("icon")]
711
public string Icon { get; set; }
812

13+
[JsonPropertyName("alias")]
914
public string Alias { get; set; }
1015

16+
[JsonPropertyName("name")]
1117
public string Name { get; set; }
1218

19+
[JsonPropertyName("group")]
1320
public string Group { get; set; }
1421

22+
[JsonPropertyName("selected")]
1523
public bool Selected { get; set; }
1624
}
1725
}

0 commit comments

Comments
 (0)