Skip to content

Commit 8f6dd26

Browse files
committed
Move index upgrade logic into the migration plan
1 parent 99eb7ba commit 8f6dd26

File tree

3 files changed

+52
-29
lines changed

3 files changed

+52
-29
lines changed
Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text.Json;
2+
using System.Text.Json.Serialization;
23
using Umbraco.Cms.Core.Services;
34
using Umbraco.Cms.Infrastructure.Migrations;
45
using Umbraco.Cms.Infrastructure.Scoping;
@@ -11,7 +12,7 @@ public class UpdateAlgoliaIndicesPostUpgrade : MigrationBase
1112
private readonly IScopeProvider _scopeProvider;
1213
private readonly IContentTypeService _contentTypeService;
1314

14-
public UpdateAlgoliaIndicesPostUpgrade(IMigrationContext context, IScopeProvider scopeProvider, IContentTypeService contentTypeService)
15+
public UpdateAlgoliaIndicesPostUpgrade(IMigrationContext context, IScopeProvider scopeProvider, IContentTypeService contentTypeService)
1516
: base(context)
1617
{
1718
_scopeProvider = scopeProvider;
@@ -24,36 +25,71 @@ protected override void Migrate()
2425

2526
var indices = scope.Database.Fetch<AlgoliaIndex>();
2627

27-
foreach (var index in indices)
28+
foreach (var index in indices)
2829
{
29-
bool requiresUpdate = false;
30-
31-
var indexSerializedData = JsonSerializer.Deserialize<IEnumerable<ContentTypeDto>>(index.SerializedData);
30+
var indexSerializedData = JsonSerializer.Deserialize<IEnumerable<AlgoliaMigrationIndexData>>(index.SerializedData);
3231
if (indexSerializedData == null) continue;
3332

3433
foreach (var data in indexSerializedData.Where(x => x.ContentType != null))
3534
{
36-
requiresUpdate = true;
35+
var contentTypeDto = new ContentTypeDto();
3736

38-
var contentType = _contentTypeService.Get(data.ContentType!.Alias);
37+
var contentType = _contentTypeService.Get(data.ContentType.Alias);
3938
if (contentType != null)
4039
{
41-
data.Id = contentType.Id;
40+
contentTypeDto.Id = contentType.Id;
4241
}
4342

44-
data.Icon = data.ContentType!.Icon;
45-
data.Alias = data.ContentType!.Alias;
46-
data.Name = data.ContentType!.Name;
47-
}
43+
contentTypeDto.Icon = data.ContentType.Icon;
44+
contentTypeDto.Alias = data.ContentType.Alias;
45+
contentTypeDto.Name = data.ContentType.Name;
46+
contentTypeDto.Properties = data.Properties.Select(x => new ContentTypePropertyDto
47+
{
48+
Alias = x.Alias,
49+
Name = x.Name,
50+
Icon = x.Icon,
51+
Selected = true
52+
});
4853

49-
if (requiresUpdate)
50-
{
51-
index.SerializedData = JsonSerializer.Serialize(indexSerializedData);
54+
index.SerializedData = JsonSerializer.Serialize(contentTypeDto);
5255
scope.Database.Update(index);
5356
}
5457
}
5558

5659
scope.Complete();
5760
}
61+
62+
private class AlgoliaMigrationIndexData
63+
{
64+
[JsonPropertyName("contentType")]
65+
public AlgoliaMigrationIndexContentType ContentType { get; set; }
66+
67+
[JsonPropertyName("properties")]
68+
public IEnumerable<AlgoliaMigrationIndexProperty> Properties { get; set; }
69+
}
70+
71+
public class AlgoliaMigrationIndexContentType
72+
{
73+
[JsonPropertyName("alias")]
74+
public string Alias { get; set; }
75+
76+
[JsonPropertyName("name")]
77+
public string Name { get; set; }
78+
79+
[JsonPropertyName("icon")]
80+
public string Icon { get; set; }
81+
}
82+
83+
public class AlgoliaMigrationIndexProperty
84+
{
85+
[JsonPropertyName("alias")]
86+
public string Alias { get; set; }
87+
88+
[JsonPropertyName("name")]
89+
public string Name { get; set; }
90+
91+
[JsonPropertyName("icon")]
92+
public string Icon { get; set; }
93+
}
5894
}
5995
}

src/Umbraco.Cms.Integrations.Search.Algolia/Models/ContentTypeDtos/ContentTypeDto.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,16 @@ public class ContentTypeDto
66
{
77
public int Id { get; set; }
88

9-
[JsonPropertyName("contentType")]
10-
public ContentEntity? ContentType { get; set; }
11-
129
public string Icon { get; set; }
1310

1411
public string Alias { get; set; }
1512

16-
[JsonPropertyName("name")]
1713
public string Name { get; set; }
1814

1915
public bool Selected { get; set; }
2016

2117
public bool AllowRemove { get; set; }
2218

23-
[JsonPropertyName("properties")]
2419
public IEnumerable<ContentTypePropertyDto> Properties { get; set; }
2520
}
2621
}
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
1-
using System.Text.Json.Serialization;
2-
3-
namespace Umbraco.Cms.Integrations.Search.Algolia.Models.ContentTypeDtos
1+
namespace Umbraco.Cms.Integrations.Search.Algolia.Models.ContentTypeDtos
42
{
53
public class ContentTypePropertyDto
64
{
7-
[JsonPropertyName("id")]
85
public int Id { get; set; }
96

10-
[JsonPropertyName("icon")]
117
public string Icon { get; set; }
128

13-
[JsonPropertyName("alias")]
149
public string Alias { get; set; }
1510

16-
[JsonPropertyName("name")]
1711
public string Name { get; set; }
1812

19-
[JsonPropertyName("group")]
2013
public string Group { get; set; }
2114

22-
[JsonPropertyName("selected")]
2315
public bool Selected { get; set; }
2416
}
2517
}

0 commit comments

Comments
 (0)