-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Migrations.cs
233 lines (190 loc) · 11.7 KB
/
Migrations.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System.Text.Json.Nodes;
using Dapper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.Data;
using OrchardCore.Data.Migration;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Descriptor.Models;
using OrchardCore.Environment.Shell.Scope;
using OrchardCore.Search.Lucene.Model;
using YesSql;
namespace OrchardCore.Search.Lucene;
public sealed class Migrations : DataMigration
{
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly ShellDescriptor _shellDescriptor;
public Migrations(
IContentDefinitionManager contentDefinitionManager,
ShellDescriptor shellDescriptor)
{
_contentDefinitionManager = contentDefinitionManager;
_shellDescriptor = shellDescriptor;
}
// New installations don't need to be upgraded, but because there is no initial migration record,
// 'UpgradeAsync()' is called in a new 'CreateAsync()' but only if the feature was already installed.
public async Task<int> CreateAsync()
{
if (_shellDescriptor.WasFeatureAlreadyInstalled("OrchardCore.Search.Lucene"))
{
await UpgradeAsync();
}
// Shortcut other migration steps on new content definition schemas.
return 1;
}
// Upgrade an existing installation.
private async Task UpgradeAsync()
{
var contentTypeDefinitions = await _contentDefinitionManager.LoadTypeDefinitionsAsync();
foreach (var contentTypeDefinition in contentTypeDefinitions)
{
foreach (var partDefinition in contentTypeDefinition.Parts)
{
await _contentDefinitionManager.AlterPartDefinitionAsync(partDefinition.Name, partBuilder =>
{
if (partDefinition.Settings.TryGetPropertyValue("ContentIndexSettings", out var existingPartSettings) &&
!partDefinition.Settings.ContainsKey(nameof(LuceneContentIndexSettings)))
{
var included = existingPartSettings["Included"];
var analyzed = existingPartSettings["Analyzed"];
if (included is not null)
{
if (analyzed is not null)
{
if ((bool)included && !(bool)analyzed)
{
existingPartSettings["Keyword"] = true;
}
}
else
{
if ((bool)included)
{
existingPartSettings["Keyword"] = true;
}
}
}
var jExistingPartSettings = existingPartSettings.AsObject();
// We remove unnecessary properties from old releases.
jExistingPartSettings.Remove("Analyzed");
jExistingPartSettings.Remove("Tokenized");
jExistingPartSettings.Remove("Template");
partDefinition.Settings[nameof(LuceneContentIndexSettings)] = jExistingPartSettings.Clone();
}
partDefinition.Settings.Remove("ContentIndexSettings");
});
}
}
var partDefinitions = await _contentDefinitionManager.LoadPartDefinitionsAsync();
foreach (var partDefinition in partDefinitions)
{
await _contentDefinitionManager.AlterPartDefinitionAsync(partDefinition.Name, partBuilder =>
{
if (partDefinition.Settings.TryGetPropertyValue("ContentIndexSettings", out var existingPartSettings) &&
!partDefinition.Settings.ContainsKey(nameof(LuceneContentIndexSettings)))
{
var included = existingPartSettings["Included"];
var analyzed = existingPartSettings["Analyzed"];
if (included != null)
{
if (analyzed != null)
{
if ((bool)included && !(bool)analyzed)
{
existingPartSettings["Keyword"] = true;
}
}
else
{
if ((bool)included)
{
existingPartSettings["Keyword"] = true;
}
}
}
var jExistingPartSettings = existingPartSettings.AsObject();
// We remove unnecessary properties from old releases.
jExistingPartSettings.Remove("Analyzed");
jExistingPartSettings.Remove("Tokenized");
jExistingPartSettings.Remove("Template");
partDefinition.Settings[nameof(LuceneContentIndexSettings)] = jExistingPartSettings.Clone();
}
partDefinition.Settings.Remove("ContentIndexSettings");
foreach (var fieldDefinition in partDefinition.Fields)
{
if (fieldDefinition.Settings.TryGetPropertyValue("ContentIndexSettings", out var existingFieldSettings) &&
!fieldDefinition.Settings.TryGetPropertyValue(nameof(LuceneContentIndexSettings), out _))
{
var included = existingFieldSettings["Included"];
var analyzed = existingFieldSettings["Analyzed"];
if (included != null)
{
if (analyzed != null)
{
if ((bool)included && !(bool)analyzed)
{
existingFieldSettings["Keyword"] = true;
}
}
else
{
if ((bool)included)
{
existingFieldSettings["Keyword"] = true;
}
}
}
var jExistingFieldSettings = existingFieldSettings.AsObject();
// We remove unnecessary properties from old releases.
jExistingFieldSettings.Remove("Analyzed");
jExistingFieldSettings.Remove("Tokenized");
jExistingFieldSettings.Remove("Template");
fieldDefinition.Settings.Add(nameof(LuceneContentIndexSettings), jExistingFieldSettings.Clone());
}
fieldDefinition.Settings.Remove("ContentIndexSettings");
}
});
}
// Defer this until after the subsequent migrations have succeeded as the schema has changed.
ShellScope.AddDeferredTask(async scope =>
{
var session = scope.ServiceProvider.GetRequiredService<ISession>();
var dbConnectionAccessor = scope.ServiceProvider.GetService<IDbConnectionAccessor>();
var logger = scope.ServiceProvider.GetService<ILogger<Migrations>>();
var tablePrefix = session.Store.Configuration.TablePrefix;
var documentTableName = session.Store.Configuration.TableNameConvention.GetDocumentTable();
var table = $"{session.Store.Configuration.TablePrefix}{documentTableName}";
await using var connection = dbConnectionAccessor.CreateConnection();
await connection.OpenAsync();
using var transaction = await connection.BeginTransactionAsync(session.Store.Configuration.IsolationLevel);
var dialect = session.Store.Configuration.SqlDialect;
try
{
logger.LogDebug("Updating Lucene indices settings and queries");
var quotedTableName = dialect.QuoteForTableName(table, session.Store.Configuration.Schema);
var quotedContentColumnName = dialect.QuoteForColumnName("Content");
var quotedTypeColumnName = dialect.QuoteForColumnName("Type");
var updateCmd = $"UPDATE {quotedTableName} SET {quotedContentColumnName} = REPLACE({quotedContentColumnName}, '\"$type\":\"OrchardCore.Lucene.LuceneQuery, OrchardCore.Lucene\"', '\"$type\":\"OrchardCore.Search.Lucene.LuceneQuery, OrchardCore.Search.Lucene\"') WHERE {quotedTypeColumnName} = 'OrchardCore.Queries.Services.QueriesDocument, OrchardCore.Queries'";
await transaction.Connection.ExecuteAsync(updateCmd, null, transaction);
updateCmd = $"UPDATE {quotedTableName} SET {quotedContentColumnName} = REPLACE({quotedContentColumnName}, '\"$type\":\"OrchardCore.Lucene.Deployment.LuceneIndexDeploymentStep, OrchardCore.Lucene\"', '\"$type\":\"OrchardCore.Search.Lucene.Deployment.LuceneIndexDeploymentStep, OrchardCore.Search.Lucene\"') WHERE {quotedTypeColumnName} = 'OrchardCore.Deployment.DeploymentPlan, OrchardCore.Deployment.Abstractions'";
await transaction.Connection.ExecuteAsync(updateCmd, null, transaction);
updateCmd = $"UPDATE {quotedTableName} SET {quotedContentColumnName} = REPLACE({quotedContentColumnName}, '\"$type\":\"OrchardCore.Lucene.Deployment.LuceneSettingsDeploymentStep, OrchardCore.Lucene\"', '\"$type\":\"OrchardCore.Search.Lucene.Deployment.LuceneSettingsDeploymentStep, OrchardCore.Search.Lucene\"') WHERE {quotedTypeColumnName} = 'OrchardCore.Deployment.DeploymentPlan, OrchardCore.Deployment.Abstractions'";
await transaction.Connection.ExecuteAsync(updateCmd, null, transaction);
updateCmd = $"UPDATE {quotedTableName} SET {quotedContentColumnName} = REPLACE({quotedContentColumnName}, '\"$type\":\"OrchardCore.Lucene.Deployment.LuceneIndexResetDeploymentStep, OrchardCore.Lucene\"', '\"$type\":\"OrchardCore.Search.Lucene.Deployment.LuceneIndexResetDeploymentStep, OrchardCore.Search.Lucene\"') WHERE {quotedTypeColumnName} = 'OrchardCore.Deployment.DeploymentPlan, OrchardCore.Deployment.Abstractions'";
await transaction.Connection.ExecuteAsync(updateCmd, null, transaction);
updateCmd = $"UPDATE {quotedTableName} SET {quotedContentColumnName} = REPLACE({quotedContentColumnName}, '\"$type\":\"OrchardCore.Lucene.Deployment.LuceneIndexRebuildDeploymentStep, OrchardCore.Lucene\"', '\"$type\":\"OrchardCore.Search.Lucene.Deployment.LuceneIndexRebuildDeploymentStep, OrchardCore.Search.Lucene\"') WHERE {quotedTypeColumnName} = 'OrchardCore.Deployment.DeploymentPlan, OrchardCore.Deployment.Abstractions'";
await transaction.Connection.ExecuteAsync(updateCmd, null, transaction);
updateCmd = $"UPDATE {quotedTableName} SET {quotedTypeColumnName} = 'OrchardCore.Search.Lucene.Model.LuceneIndexSettingsDocument, OrchardCore.Search.Lucene' WHERE {quotedTypeColumnName} = 'OrchardCore.Lucene.Model.LuceneIndexSettingsDocument, OrchardCore.Lucene'";
await transaction.Connection.ExecuteAsync(updateCmd, null, transaction);
await transaction.CommitAsync();
}
catch (Exception e)
{
await transaction.RollbackAsync();
logger.LogError(e, "An error occurred while updating Lucene indices settings and queries");
throw;
}
});
}
}