diff --git a/src/ServiceControl.Audit.Persistence.RavenDB/ServiceControl.Audit.Persistence.RavenDB.csproj b/src/ServiceControl.Audit.Persistence.RavenDB/ServiceControl.Audit.Persistence.RavenDB.csproj
index f7e9dc94c3..beed4ad09b 100644
--- a/src/ServiceControl.Audit.Persistence.RavenDB/ServiceControl.Audit.Persistence.RavenDB.csproj
+++ b/src/ServiceControl.Audit.Persistence.RavenDB/ServiceControl.Audit.Persistence.RavenDB.csproj
@@ -24,6 +24,7 @@
+
diff --git a/src/ServiceControl.Audit.Persistence.RavenDB/index-definitions.manifest b/src/ServiceControl.Audit.Persistence.RavenDB/index-definitions.manifest
new file mode 100644
index 0000000000..648b13f44e
--- /dev/null
+++ b/src/ServiceControl.Audit.Persistence.RavenDB/index-definitions.manifest
@@ -0,0 +1,62 @@
+{
+ "Indexes": [
+ {
+ "Name": "FailedAuditImportIndex",
+ "Maps": [
+ "docs.FailedAuditImports.Select(cc => new {\n Id = Id(cc)\n})"
+ ],
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "MessagesViewIndex",
+ "Maps": [
+ "docs.ProcessedMessages.Select(message => new {\n MessageId = ((string) message.MessageMetadata[\"MessageId\"]),\n MessageType = ((string) message.MessageMetadata[\"MessageType\"]),\n IsSystemMessage = ((bool) message.MessageMetadata[\"IsSystemMessage\"]),\n Status = ((bool) message.MessageMetadata[\"IsRetried\"]) ? 4 : 3,\n TimeSent = ((DateTime) message.MessageMetadata[\"TimeSent\"]),\n ProcessedAt = message.ProcessedAt,\n ReceivingEndpointName = ((message.MessageMetadata[\"ReceivingEndpoint\"])).Name,\n SendingEndpointName = ((message.MessageMetadata[\"SendingEndpoint\"])).Name,\n CriticalTime = ((TimeSpan ? ) message.MessageMetadata[\"CriticalTime\"]),\n ProcessingTime = ((TimeSpan ? ) message.MessageMetadata[\"ProcessingTime\"]),\n DeliveryTime = ((TimeSpan ? ) message.MessageMetadata[\"DeliveryTime\"]),\n Query = Enumerable.ToArray(DynamicEnumerable.Union(message.MessageMetadata.ToDictionary(e1 => e1.Key, e1 => e1.Value).Select(_ => _.Value.ToString()), new string[] {\n String.Join(\" \", message.Headers.ToDictionary(e1 => e1.Key, e1 => e1.Value).Select(x => x.Value))\n })),\n ConversationId = ((string) message.MessageMetadata[\"ConversationId\"])\n})"
+ ],
+ "Fields": {
+ "Query": {
+ "Indexing": "Search",
+ "Analyzer": "Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net, Version=3.0.3.0, Culture=neutral, PublicKeyToken=85089178b9ac3181"
+ }
+ },
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "MessagesViewIndexWithFullTextSearch",
+ "Maps": [
+ "docs.ProcessedMessages.Select(message => new {\n MessageId = ((string) message.MessageMetadata[\"MessageId\"]),\n MessageType = ((string) message.MessageMetadata[\"MessageType\"]),\n IsSystemMessage = ((bool) message.MessageMetadata[\"IsSystemMessage\"]),\n Status = ((bool) message.MessageMetadata[\"IsRetried\"]) ? 4 : 3,\n TimeSent = ((DateTime) message.MessageMetadata[\"TimeSent\"]),\n ProcessedAt = message.ProcessedAt,\n ReceivingEndpointName = ((message.MessageMetadata[\"ReceivingEndpoint\"])).Name,\n SendingEndpointName = ((message.MessageMetadata[\"SendingEndpoint\"])).Name,\n CriticalTime = ((TimeSpan ? ) message.MessageMetadata[\"CriticalTime\"]),\n ProcessingTime = ((TimeSpan ? ) message.MessageMetadata[\"ProcessingTime\"]),\n DeliveryTime = ((TimeSpan ? ) message.MessageMetadata[\"DeliveryTime\"]),\n Query = Enumerable.ToArray(DynamicEnumerable.Union(message.MessageMetadata.ToDictionary(e1 => e1.Key, e1 => e1.Value).Select(_ => _.Value.ToString()), new string[] {\n String.Join(\" \", message.Headers.ToDictionary(e1 => e1.Key, e1 => e1.Value).Select(x => x.Value)),\n this.LoadAttachment(message, \"body\").GetContentAsString()\n })),\n ConversationId = ((string) message.MessageMetadata[\"ConversationId\"])\n})"
+ ],
+ "Fields": {
+ "Query": {
+ "Indexing": "Search",
+ "Analyzer": "Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net, Version=3.0.3.0, Culture=neutral, PublicKeyToken=85089178b9ac3181"
+ }
+ },
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "SagaDetailsIndex",
+ "Maps": [
+ "docs.SagaHistories.Select(doc => new {\n SagaId = doc.SagaId,\n Id = doc.SagaId,\n SagaType = doc.SagaType,\n Changes = doc.Changes\n})",
+ "docs.SagaSnapshots.Select(doc => new {\n SagaId = doc.SagaId,\n Id = doc.SagaId,\n SagaType = doc.SagaType,\n Changes = new[] {\n new {\n Endpoint = doc.Endpoint,\n FinishTime = doc.FinishTime,\n InitiatingMessage = doc.InitiatingMessage,\n OutgoingMessages = doc.OutgoingMessages,\n StartTime = doc.StartTime,\n StateAfterChange = doc.StateAfterChange,\n Status = doc.Status\n }\n }\n})"
+ ],
+ "Reduce": "results.GroupBy(result => result.SagaId).Select(g => new {\n g = g,\n first = DynamicEnumerable.FirstOrDefault(g)\n}).Select(this0 => new {\n Id = this0.first.SagaId,\n SagaId = this0.first.SagaId,\n SagaType = this0.first.SagaType,\n Changes = Enumerable.ToList(DynamicEnumerable.OrderByDescending(this0.g.SelectMany(x => x.Changes), x0 => x0.FinishTime).Take(50000))\n})",
+ "Fields": {
+ "Changes": {
+ "Indexing": "No"
+ },
+ "SagaId": {
+ "Indexing": "Exact"
+ },
+ "SagaType": {
+ "Indexing": "No"
+ }
+ },
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ }
+ ]
+}
diff --git a/src/ServiceControl.Audit.Persistence.Tests.RavenDB/IndexDefinitionManifestTests.cs b/src/ServiceControl.Audit.Persistence.Tests.RavenDB/IndexDefinitionManifestTests.cs
new file mode 100644
index 0000000000..ed75870d7a
--- /dev/null
+++ b/src/ServiceControl.Audit.Persistence.Tests.RavenDB/IndexDefinitionManifestTests.cs
@@ -0,0 +1,62 @@
+namespace ServiceControl.Audit.Persistence.Tests;
+
+using System.IO;
+using System.Linq;
+using System.Net.Http;
+using System.Threading.Tasks;
+using NUnit.Framework;
+using Persistence.RavenDB;
+using ServiceControl.RavenDB;
+using ServiceControlInstaller.Engine.Validation;
+using TestHelper;
+
+// The installer compares index-definitions.manifest with the index definitions on the server before an upgrade.
+[TestFixture]
+class IndexDefinitionManifestTests : CleanDatabaseFixture
+{
+ [Test]
+ public void Manifest_is_current()
+ {
+ var rendered = IndexDefinitionManifest.Render(IndexDeployment.FindIndexes(typeof(DatabaseSetup).Assembly));
+
+ if (IndexDefinitionManifest.NormalizeLineEndings(File.ReadAllText(ManifestPath)) != rendered)
+ {
+ var receivedPath = Path.Combine(TestContext.CurrentContext.WorkDirectory, IndexDefinitionManifest.FileName + ".received");
+ File.WriteAllText(receivedPath, rendered);
+ Assert.Fail($"{IndexDefinitionManifest.FileName} in ServiceControl.Audit.Persistence.RavenDB does not match the index definitions. Replace it with {receivedPath}.");
+ }
+ }
+
+ [Test]
+ public async Task Deployed_indexes_have_no_changes()
+ {
+ var changes = await DeployAndDetect(File.ReadAllText(ManifestPath));
+
+ Assert.That(changes.Changes, Is.Empty);
+ }
+
+ [Test]
+ public async Task Changed_map_is_reported()
+ {
+ var changes = await DeployAndDetect(File.ReadAllText(ManifestPath).Replace("ProcessedAt = ", "ProcessedAtChanged = "));
+
+ Assert.That(changes.Changes, Is.Not.Empty);
+ Assert.That(changes.Changes.Select(change => change.Database).Distinct(), Is.EqualTo(new[] { Store.Database }));
+ Assert.That(changes.Changes.SelectMany(change => change.Differences).Distinct(), Is.EqualTo(new[] { "Maps" }));
+ Assert.That(changes.Changes.All(change => change.Entries == 0 && !change.IsLocked), Is.True);
+ }
+
+ async Task DeployAndDetect(string manifest)
+ {
+ var indexes = IndexDeployment.FindIndexes(typeof(DatabaseSetup).Assembly).ToList();
+ await IndexDeployment.CreateIndexesAsync(indexes, Store, cancellationToken: TestTimeoutCancellationToken);
+
+ using var http = new HttpClient();
+ var changes = await RavenDBIndexChanges.Detect("instance", RavenDBIndexDefinitions.Parse(manifest), (path, token) => http.GetStringAsync(Store.Urls[0] + path, token), TestTimeoutCancellationToken);
+
+ // The shared server also holds the databases of other tests.
+ return new RavenDBIndexChanges { InstanceName = changes.InstanceName, Changes = changes.Changes.Where(change => change.Database == Store.Database).ToList() };
+ }
+
+ static string ManifestPath => Path.Combine(TestContext.CurrentContext.TestDirectory, IndexDefinitionManifest.FileName);
+}
diff --git a/src/ServiceControl.Audit.Persistence.Tests.RavenDB/ServiceControl.Audit.Persistence.Tests.RavenDB.csproj b/src/ServiceControl.Audit.Persistence.Tests.RavenDB/ServiceControl.Audit.Persistence.Tests.RavenDB.csproj
index d6295c26dc..1f7ceacb95 100644
--- a/src/ServiceControl.Audit.Persistence.Tests.RavenDB/ServiceControl.Audit.Persistence.Tests.RavenDB.csproj
+++ b/src/ServiceControl.Audit.Persistence.Tests.RavenDB/ServiceControl.Audit.Persistence.Tests.RavenDB.csproj
@@ -31,4 +31,9 @@
+
+
+
+
+
diff --git a/src/ServiceControl.Config/Commands/ScmuCommandChecks.cs b/src/ServiceControl.Config/Commands/ScmuCommandChecks.cs
index 5b47955af4..cb40a6e4c0 100644
--- a/src/ServiceControl.Config/Commands/ScmuCommandChecks.cs
+++ b/src/ServiceControl.Config/Commands/ScmuCommandChecks.cs
@@ -9,6 +9,7 @@
using ServiceControlInstaller.Engine;
using ServiceControlInstaller.Engine.Configuration.ServiceControl;
using ServiceControlInstaller.Engine.Instances;
+ using ServiceControlInstaller.Engine.Validation;
public class ScmuCommandChecks : AbstractCommandChecks
{
@@ -94,6 +95,14 @@ protected override Task PromptToStopRunningInstance(BaseService instance,
"Yes, I want to proceed", "No", cancellationToken);
}
+ protected override Task PromptForRavenDBIndexRebuild(RavenDBIndexChanges indexChanges, CancellationToken cancellationToken = default)
+ {
+ return windowManager.ShowYesNoDialog("UPGRADE REBUILDS INDEXES",
+ indexChanges.Describe(),
+ "Do you want to proceed?",
+ "Yes, upgrade and rebuild the indexes", "No, cancel the upgrade", cancellationToken);
+ }
+
protected override Task PromptToContinueWithForcedUpgrade(CancellationToken cancellationToken = default)
{
return windowManager.ShowMessage("Forced migration",
diff --git a/src/ServiceControl.Management.PowerShell/Validation/AcknowledgementValues.cs b/src/ServiceControl.Management.PowerShell/Validation/AcknowledgementValues.cs
index 60de4a497d..8c3060501d 100644
--- a/src/ServiceControl.Management.PowerShell/Validation/AcknowledgementValues.cs
+++ b/src/ServiceControl.Management.PowerShell/Validation/AcknowledgementValues.cs
@@ -4,5 +4,6 @@ static class AcknowledgementValues
{
public const string RabbitMQBrokerVersion310 = "RabbitMQBrokerVersion310";
public const string RabbitMQManagementApi = "RabbitMQManagementApi";
+ public const string RavenDBIndexRebuild = "RavenDBIndexRebuild";
}
}
diff --git a/src/ServiceControl.Management.PowerShell/Validation/PowerShellCommandChecks.cs b/src/ServiceControl.Management.PowerShell/Validation/PowerShellCommandChecks.cs
index ca81261a14..4c94624d56 100644
--- a/src/ServiceControl.Management.PowerShell/Validation/PowerShellCommandChecks.cs
+++ b/src/ServiceControl.Management.PowerShell/Validation/PowerShellCommandChecks.cs
@@ -10,6 +10,7 @@
using ServiceControlInstaller.Engine;
using ServiceControlInstaller.Engine.Configuration.ServiceControl;
using ServiceControlInstaller.Engine.Instances;
+ using ServiceControlInstaller.Engine.Validation;
class PowerShellCommandChecks : AbstractCommandChecks
{
@@ -97,6 +98,18 @@ protected override Task PromptToStopRunningInstance(BaseService instance,
return Task.FromResult(true);
}
+ protected override Task PromptForRavenDBIndexRebuild(RavenDBIndexChanges indexChanges, CancellationToken cancellationToken = default)
+ {
+ if (acknowledgements.Any(ack => ack.Equals(AcknowledgementValues.RavenDBIndexRebuild, StringComparison.OrdinalIgnoreCase)))
+ {
+ cmdlet.WriteWarning(indexChanges.Describe());
+ return Task.FromResult(true);
+ }
+
+ Terminate($"{indexChanges.Describe()}{Environment.NewLine}{Environment.NewLine}Use -Acknowledgements {AcknowledgementValues.RavenDBIndexRebuild} to upgrade and rebuild the indexes.", "Install Error", ErrorCategory.InvalidArgument);
+ return Task.FromResult(false);
+ }
+
protected override Task PromptToContinueWithForcedUpgrade(CancellationToken cancellationToken = default)
{
// In PowerShell, you passed the -Force parameter to get here in the first place
diff --git a/src/ServiceControl.Persistence.RavenDB/ServiceControl.Persistence.RavenDB.csproj b/src/ServiceControl.Persistence.RavenDB/ServiceControl.Persistence.RavenDB.csproj
index 64ee49dea8..64f59bf3f9 100644
--- a/src/ServiceControl.Persistence.RavenDB/ServiceControl.Persistence.RavenDB.csproj
+++ b/src/ServiceControl.Persistence.RavenDB/ServiceControl.Persistence.RavenDB.csproj
@@ -29,6 +29,7 @@
+
diff --git a/src/ServiceControl.Persistence.RavenDB/index-definitions.manifest b/src/ServiceControl.Persistence.RavenDB/index-definitions.manifest
new file mode 100644
index 0000000000..9bce7cafcb
--- /dev/null
+++ b/src/ServiceControl.Persistence.RavenDB/index-definitions.manifest
@@ -0,0 +1,170 @@
+{
+ "Indexes": [
+ {
+ "Name": "ArchivedGroupsViewIndex",
+ "Maps": [
+ "docs.FailedMessages.Where(doc => doc.Status == 4).Select(doc => new {\n doc = doc,\n failureTimes = doc.ProcessingAttempts.Select(x => x.FailureDetails.TimeOfFailure)\n}).SelectMany(this0 => this0.doc.FailureGroups, (this0, failureGroup) => new {\n Id = failureGroup.Id,\n Title = failureGroup.Title,\n Count = 1,\n First = DynamicEnumerable.Min(this0.failureTimes),\n Last = DynamicEnumerable.Max(this0.failureTimes),\n Type = failureGroup.Type\n})"
+ ],
+ "Reduce": "results.GroupBy(result => new {\n Id = result.Id,\n Title = result.Title,\n Type = result.Type\n}).Select(g => new {\n Id = g.Key.Id,\n Title = g.Key.Title,\n Count = Enumerable.Sum(g, x => ((int) x.Count)),\n First = DynamicEnumerable.Min(g, x0 => ((DateTime) x0.First)),\n Last = DynamicEnumerable.Max(g, x1 => ((DateTime) x1.Last)),\n Type = g.Key.Type\n})",
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "CustomChecksIndex",
+ "Maps": [
+ "docs.CustomChecks.Select(cc => new {\n Status = cc.Status,\n ReportedAt = cc.ReportedAt,\n Category = cc.Category,\n CustomCheckId = cc.CustomCheckId\n})"
+ ],
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "EndpointIndex",
+ "Maps": [
+ "docs.EndpointDocuments.Select(endpoint => new {\n EndpointId_ThroughputSource = endpoint.EndpointId.ThroughputSource,\n EndpointId_Name = endpoint.EndpointId.Name,\n SanitizedName = endpoint.SanitizedName,\n EndpointIndicators = endpoint.EndpointIndicators\n})"
+ ],
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "EventLogItems/ByRaisedAt",
+ "Maps": [
+ "docs.EventLogItems.Select(doc => new {\n RaisedAt = doc.RaisedAt\n})"
+ ],
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "FailedErrorImportIndex",
+ "Maps": [
+ "docs.FailedErrorImports.Select(cc => new {\n Id = Id(cc)\n})"
+ ],
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "FailedMessageFacetsIndex",
+ "Maps": [
+ "docs.FailedMessages.Where(failure => failure.Status == 1).Select(failure => new {\n failure = failure,\n t = ((DynamicEnumerable.LastOrDefault(failure.ProcessingAttempts)).MessageMetadata[\"ReceivingEndpoint\"])\n}).Select(this0 => new {\n Name = this0.t.Name,\n Host = this0.t.Host,\n MessageType = (DynamicEnumerable.LastOrDefault(this0.failure.ProcessingAttempts)).MessageMetadata[\"MessageType\"]\n})"
+ ],
+ "Fields": {
+ "Host": {
+ "Indexing": "Exact"
+ },
+ "MessageType": {
+ "Indexing": "Exact"
+ },
+ "Name": {
+ "Indexing": "Exact"
+ }
+ },
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "FailedMessageRetries/ByBatch",
+ "Maps": [
+ "docs.FailedMessageRetries.Select(doc => new {\n RetryBatchId = doc.RetryBatchId\n})"
+ ],
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "FailedMessageViewIndex",
+ "Maps": [
+ "docs.FailedMessages.Select(message => new {\n message = message,\n processingAttemptsLast = DynamicEnumerable.LastOrDefault(message.ProcessingAttempts)\n}).Select(this0 => new {\n MessageId = this0.processingAttemptsLast.MessageMetadata[\"MessageId\"],\n MessageType = this0.processingAttemptsLast.MessageMetadata[\"MessageType\"],\n Status = this0.message.Status,\n TimeSent = ((DateTime) this0.processingAttemptsLast.MessageMetadata[\"TimeSent\"]),\n ReceivingEndpointName = ((this0.processingAttemptsLast.MessageMetadata[\"ReceivingEndpoint\"])).Name,\n QueueAddress = this0.processingAttemptsLast.FailureDetails.AddressOfFailingEndpoint,\n TimeOfFailure = this0.processingAttemptsLast.FailureDetails.TimeOfFailure,\n LastModified = (this.MetadataFor(this0.message).Value < DateTime > (\"@last-modified\")).Ticks\n})"
+ ],
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "FailedMessages/ByGroup",
+ "Maps": [
+ "docs.FailedMessages.Select(doc => new {\n doc = doc,\n processingAttemptsLast = DynamicEnumerable.LastOrDefault(doc.ProcessingAttempts)\n}).SelectMany(this0 => this0.doc.FailureGroups, (this0, failureGroup) => new {\n Id = Id(this0.doc),\n MessageId = this0.doc.UniqueMessageId,\n FailureGroupId = failureGroup.Id,\n FailureGroupName = failureGroup.Title,\n Status = this0.doc.Status,\n MessageType = ((string) this0.processingAttemptsLast.MessageMetadata[\"MessageType\"]),\n TimeSent = ((DateTime) this0.processingAttemptsLast.MessageMetadata[\"TimeSent\"]),\n TimeOfFailure = this0.processingAttemptsLast.FailureDetails.TimeOfFailure,\n LastModified = (this.MetadataFor(this0.doc).Value < DateTime > (\"@last-modified\")).Ticks\n})"
+ ],
+ "Fields": {
+ "__all_fields": {
+ "Storage": "Yes"
+ }
+ },
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "FailureGroupsViewIndex",
+ "Maps": [
+ "docs.FailedMessages.Where(doc => doc.Status == 1).Select(doc => new {\n doc = doc,\n failureTimes = doc.ProcessingAttempts.Select(x => x.FailureDetails.TimeOfFailure)\n}).SelectMany(this0 => this0.doc.FailureGroups, (this0, failureGroup) => new {\n Id = failureGroup.Id,\n Title = failureGroup.Title,\n Count = 1,\n First = DynamicEnumerable.Min(this0.failureTimes),\n Last = DynamicEnumerable.Max(this0.failureTimes),\n Type = failureGroup.Type\n})"
+ ],
+ "Reduce": "results.GroupBy(result => new {\n Id = result.Id,\n Title = result.Title,\n Type = result.Type\n}).Select(g => new {\n Id = g.Key.Id,\n Title = g.Key.Title,\n Count = Enumerable.Sum(g, x => ((int) x.Count)),\n First = DynamicEnumerable.Min(g, x0 => ((DateTime) x0.First)),\n Last = DynamicEnumerable.Max(g, x1 => ((DateTime) x1.Last)),\n Type = g.Key.Type\n})",
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "GroupCommentIndex",
+ "Maps": [
+ "docs.GroupComments.Select(gc => new {\n Id = Id(gc),\n Comment = gc.Comment\n})"
+ ],
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "KnownEndpointIndex",
+ "Maps": [
+ "docs.KnownEndpoints.Select(message => new {\n EndpointDetails_Name = message.EndpointDetails.Name,\n EndpointDetails_Host = message.EndpointDetails.Host,\n HostDisplayName = message.HostDisplayName,\n Monitored = message.Monitored\n})"
+ ],
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "MessagesViewIndex",
+ "Maps": [
+ "docs.FailedMessages.Select(message => new {\n message = message,\n last = DynamicEnumerable.LastOrDefault(message.ProcessingAttempts)\n}).Select(this0 => new {\n MessageId = this0.last.MessageId,\n MessageType = ((string) this0.last.MessageMetadata[\"MessageType\"]),\n IsSystemMessage = ((bool) this0.last.MessageMetadata[\"IsSystemMessage\"]),\n Status = this0.message.Status == 4 ? 5 : (this0.message.Status == 2 ? 4 : (this0.message.ProcessingAttempts.Count == 1 ? 1 : 2)),\n TimeSent = ((DateTime) this0.last.MessageMetadata[\"TimeSent\"]),\n ProcessedAt = this0.last.AttemptedAt,\n ReceivingEndpointName = ((this0.last.MessageMetadata[\"ReceivingEndpoint\"])).Name,\n CriticalTime = ((TimeSpan ? ) this0.last.MessageMetadata[\"CriticalTime\"]),\n ProcessingTime = ((TimeSpan ? ) this0.last.MessageMetadata[\"ProcessingTime\"]),\n DeliveryTime = ((TimeSpan ? ) this0.last.MessageMetadata[\"DeliveryTime\"]),\n Query = Enumerable.ToArray(DynamicEnumerable.Union(this0.last.MessageMetadata.ToDictionary(e1 => e1.Key, e1 => e1.Value).Select(_ => _.Value.ToString()), new string[] {\n String.Join(\" \", this0.last.Headers.ToDictionary(e1 => e1.Key, e1 => e1.Value).Select(x => x.Value))\n })),\n ConversationId = ((string) this0.last.MessageMetadata[\"ConversationId\"])\n})"
+ ],
+ "Fields": {
+ "Query": {
+ "Indexing": "Search"
+ }
+ },
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "QueueAddressIndex",
+ "Maps": [
+ "docs.FailedMessages.Select(message => new {\n message = message,\n processingAttemptsLast = DynamicEnumerable.LastOrDefault(message.ProcessingAttempts)\n}).Select(this0 => new {\n PhysicalAddress = this0.processingAttemptsLast.FailureDetails.AddressOfFailingEndpoint,\n FailedMessageCount = 1\n})"
+ ],
+ "Reduce": "results.GroupBy(result => result.PhysicalAddress).Select(g => new {\n PhysicalAddress = g.Key,\n FailedMessageCount = Enumerable.Sum(g, m => ((int) m.FailedMessageCount))\n})",
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "RetryBatches/ByStatus/ReduceInitialBatchSize",
+ "Maps": [
+ "docs.RetryBatches.Select(doc => new {\n RequestId = doc.RequestId,\n RetryType = doc.RetryType,\n HasStagingBatches = doc.Status == 2,\n HasForwardingBatches = doc.Status == 3,\n InitialBatchSize = doc.InitialBatchSize,\n Originator = doc.Originator,\n Classifier = doc.Classifier,\n StartTime = doc.StartTime,\n Last = doc.Last\n})"
+ ],
+ "Reduce": "results.GroupBy(result => new {\n RequestId = result.RequestId,\n RetryType = result.RetryType\n}).Select(g => new {\n RequestId = g.Key.RequestId,\n RetryType = g.Key.RetryType,\n Originator = (DynamicEnumerable.FirstOrDefault(g)).Originator,\n HasStagingBatches = Enumerable.Any(g, x => x.HasStagingBatches),\n HasForwardingBatches = Enumerable.Any(g, x0 => x0.HasForwardingBatches),\n InitialBatchSize = Enumerable.Sum(g, x1 => ((int) x1.InitialBatchSize)),\n StartTime = (DynamicEnumerable.FirstOrDefault(g)).StartTime,\n Last = DynamicEnumerable.Max(g, x2 => ((DateTime) x2.Last)),\n Classifier = (DynamicEnumerable.FirstOrDefault(g)).Classifier\n})",
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ },
+ {
+ "Name": "RetryBatches/ByStatusAndSession",
+ "Maps": [
+ "docs.RetryBatches.Select(doc => new {\n RetrySessionId = doc.RetrySessionId,\n Status = doc.Status\n})"
+ ],
+ "Fields": {},
+ "AdditionalSources": {},
+ "AdditionalAssemblies": []
+ }
+ ]
+}
diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/IndexDefinitionManifestTests.cs b/src/ServiceControl.Persistence.Tests.RavenDB/IndexDefinitionManifestTests.cs
new file mode 100644
index 0000000000..f98ba845a3
--- /dev/null
+++ b/src/ServiceControl.Persistence.Tests.RavenDB/IndexDefinitionManifestTests.cs
@@ -0,0 +1,65 @@
+namespace ServiceControl.Persistence.Tests.RavenDB;
+
+using System.IO;
+using System.Linq;
+using System.Net.Http;
+using System.Threading.Tasks;
+using NUnit.Framework;
+using ServiceControl.Persistence.RavenDB;
+using ServiceControl.RavenDB;
+using ServiceControlInstaller.Engine.Validation;
+using TestHelper;
+
+// The installer compares index-definitions.manifest with the index definitions on the server before an upgrade.
+[TestFixture]
+class IndexDefinitionManifestTests
+{
+ [Test]
+ public void Manifest_is_current()
+ {
+ var rendered = IndexDefinitionManifest.Render(IndexDeployment.FindIndexes(typeof(DatabaseSetup).Assembly));
+
+ if (IndexDefinitionManifest.NormalizeLineEndings(File.ReadAllText(ManifestPath)) != rendered)
+ {
+ var receivedPath = Path.Combine(TestContext.CurrentContext.WorkDirectory, IndexDefinitionManifest.FileName + ".received");
+ File.WriteAllText(receivedPath, rendered);
+ Assert.Fail($"{IndexDefinitionManifest.FileName} in ServiceControl.Persistence.RavenDB does not match the index definitions. Replace it with {receivedPath}.");
+ }
+ }
+
+ [Test]
+ public async Task Deployed_indexes_have_no_changes()
+ {
+ var (changes, _) = await DeployAndDetect(File.ReadAllText(ManifestPath));
+
+ Assert.That(changes.Changes, Is.Empty);
+ }
+
+ [Test]
+ public async Task Changed_map_is_reported()
+ {
+ var (changes, database) = await DeployAndDetect(File.ReadAllText(ManifestPath).Replace(" Id = ", " IdChanged = "));
+
+ Assert.That(changes.Changes, Is.Not.Empty);
+ Assert.That(changes.Changes.Select(change => change.Database).Distinct(), Is.EqualTo(new[] { database }));
+ Assert.That(changes.Changes.SelectMany(change => change.Differences), Does.Contain("Maps"));
+ Assert.That(changes.Changes.All(change => change.Entries == 0 && !change.IsLocked), Is.True);
+ }
+
+ static async Task<(RavenDBIndexChanges Changes, string Database)> DeployAndDetect(string manifest)
+ {
+ await using var database = await ScratchDatabase.Create();
+ var store = database.Store;
+
+ var indexes = IndexDeployment.FindIndexes(typeof(DatabaseSetup).Assembly).ToList();
+ await IndexDeployment.CreateIndexesAsync(indexes, store);
+
+ using var http = new HttpClient();
+ var changes = await RavenDBIndexChanges.Detect("instance", RavenDBIndexDefinitions.Parse(manifest), (path, token) => http.GetStringAsync(store.Urls[0] + path, token));
+
+ // The shared server also holds the databases of other tests.
+ return (new RavenDBIndexChanges { InstanceName = changes.InstanceName, Changes = changes.Changes.Where(change => change.Database == store.Database).ToList() }, store.Database);
+ }
+
+ static string ManifestPath => Path.Combine(TestContext.CurrentContext.TestDirectory, IndexDefinitionManifest.FileName);
+}
diff --git a/src/ServiceControl.Persistence.Tests.RavenDB/ServiceControl.Persistence.Tests.RavenDB.csproj b/src/ServiceControl.Persistence.Tests.RavenDB/ServiceControl.Persistence.Tests.RavenDB.csproj
index af6adc14d5..6d11d1219f 100644
--- a/src/ServiceControl.Persistence.Tests.RavenDB/ServiceControl.Persistence.Tests.RavenDB.csproj
+++ b/src/ServiceControl.Persistence.Tests.RavenDB/ServiceControl.Persistence.Tests.RavenDB.csproj
@@ -32,4 +32,9 @@
+
+
+
+
+
diff --git a/src/ServiceControlInstaller.Engine/ServiceControlInstaller.Engine.csproj b/src/ServiceControlInstaller.Engine/ServiceControlInstaller.Engine.csproj
index a7f08f436b..11dfaa0c7b 100644
--- a/src/ServiceControlInstaller.Engine/ServiceControlInstaller.Engine.csproj
+++ b/src/ServiceControlInstaller.Engine/ServiceControlInstaller.Engine.csproj
@@ -29,6 +29,7 @@
+
diff --git a/src/ServiceControlInstaller.Engine/Validation/AbstractCommandChecks.cs b/src/ServiceControlInstaller.Engine/Validation/AbstractCommandChecks.cs
index 90e59d6baf..998dff6d67 100644
--- a/src/ServiceControlInstaller.Engine/Validation/AbstractCommandChecks.cs
+++ b/src/ServiceControlInstaller.Engine/Validation/AbstractCommandChecks.cs
@@ -23,6 +23,7 @@ public abstract class AbstractCommandChecks
protected abstract Task NotifyForIncompatibleStorageEngine(IServiceControlBaseInstance baseInstance, CancellationToken cancellationToken = default);
protected abstract Task NotifyForIncompatibleUpgradeVersion(UpgradeInfo upgradeInfo, CancellationToken cancellationToken = default);
protected abstract Task NotifyError(string title, string message, CancellationToken cancellationToken = default);
+ protected abstract Task PromptForRavenDBIndexRebuild(RavenDBIndexChanges indexChanges, CancellationToken cancellationToken = default);
public async Task CanAddInstance(bool allowExpiredLicense = false, CancellationToken cancellationToken = default)
{
@@ -168,6 +169,16 @@ public async Task CanUpgradeInstance(BaseService instance, bool forceUpgra
return false;
}
+ // The database answers only while the instance runs, so this check comes before the instance stops.
+ if (instance is ServiceControlBaseService serviceControlInstance && !forceUpgradeDb)
+ {
+ var indexChanges = await RavenDBIndexChangeDetection.Detect(serviceControlInstance, cancellationToken).ConfigureAwait(false);
+ if (indexChanges is { Changes.Count: > 0 } && !await PromptForRavenDBIndexRebuild(indexChanges, cancellationToken).ConfigureAwait(false))
+ {
+ return false;
+ }
+ }
+
return true;
}
diff --git a/src/ServiceControlInstaller.Engine/Validation/RavenDBIndexChangeDetection.cs b/src/ServiceControlInstaller.Engine/Validation/RavenDBIndexChangeDetection.cs
new file mode 100644
index 0000000000..deb4287f60
--- /dev/null
+++ b/src/ServiceControlInstaller.Engine/Validation/RavenDBIndexChangeDetection.cs
@@ -0,0 +1,58 @@
+namespace ServiceControlInstaller.Engine.Validation
+{
+ using System;
+ using System.Collections.Generic;
+ using System.IO;
+ using System.Linq;
+ using System.Net.Http;
+ using System.Text.Json;
+ using System.Threading;
+ using System.Threading.Tasks;
+ using Instances;
+
+ public static class RavenDBIndexChangeDetection
+ {
+ // Returns null when the check is not possible: the instance does not use the embedded RavenDB, or its database
+ // does not answer. The database answers only while the instance runs, so run this check before the instance stops.
+ public static async Task Detect(ServiceControlBaseService instance, CancellationToken cancellationToken = default)
+ {
+ if (instance is not IPersistenceConfig { PersistenceManifest.Name: StorageEngineNames.RavenDB } || instance.DatabaseMaintenancePort is not { } port)
+ {
+ return null;
+ }
+
+ var target = LoadBundledDefinitions(instance is ServiceControlAuditInstance ? "Particular.ServiceControl.Audit" : "Particular.ServiceControl");
+ if (target == null)
+ {
+ return null;
+ }
+
+ try
+ {
+ using var http = new HttpClient { BaseAddress = new Uri($"http://localhost:{port}"), Timeout = TimeSpan.FromSeconds(15) };
+
+ return await RavenDBIndexChanges.Detect(instance.Name, target, http.GetStringAsync, cancellationToken).ConfigureAwait(false);
+ }
+ catch (Exception e) when (e is HttpRequestException or JsonException or KeyNotFoundException or InvalidOperationException || (e is OperationCanceledException && !cancellationToken.IsCancellationRequested))
+ {
+ return null;
+ }
+ }
+
+ // The index definitions of the version that this installer deploys.
+ static List LoadBundledDefinitions(string instanceFolder)
+ {
+ var assembly = typeof(RavenDBIndexChangeDetection).Assembly;
+ var resourceName = assembly.GetManifestResourceNames()
+ .FirstOrDefault(name => name.Replace('/', '\\').Equals($@"{instanceFolder}\Persisters\RavenDB\{RavenDBIndexDefinitions.ManifestFileName}", StringComparison.OrdinalIgnoreCase));
+
+ if (resourceName == null)
+ {
+ return null;
+ }
+
+ using var reader = new StreamReader(assembly.GetManifestResourceStream(resourceName)!);
+ return RavenDBIndexDefinitions.Parse(reader.ReadToEnd());
+ }
+ }
+}
diff --git a/src/ServiceControlInstaller.Engine/Validation/RavenDBIndexChanges.cs b/src/ServiceControlInstaller.Engine/Validation/RavenDBIndexChanges.cs
new file mode 100644
index 0000000000..eb1b3cb6e6
--- /dev/null
+++ b/src/ServiceControlInstaller.Engine/Validation/RavenDBIndexChanges.cs
@@ -0,0 +1,213 @@
+namespace ServiceControlInstaller.Engine.Validation
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Globalization;
+ using System.Linq;
+ using System.Text;
+ using System.Text.Json;
+ using System.Threading;
+ using System.Threading.Tasks;
+
+ // This file has no dependency on the rest of the installer engine. The RavenDB persistence test projects compile it
+ // too, to verify the comparison against a real RavenDB server.
+
+ public class RavenDBIndexDefinition
+ {
+ public string Name { get; init; }
+
+ public string LockMode { get; init; }
+
+ // The canonical JSON text of each definition part that makes RavenDB rebuild the index when it changes.
+ public IReadOnlyDictionary Parts { get; init; }
+
+ public bool IsLocked => LockMode is "LockedIgnore" or "LockedError";
+ }
+
+ public static class RavenDBIndexDefinitions
+ {
+ public const string ManifestFileName = "index-definitions.manifest";
+
+ // Mirrors IndexDeployment.RebuildTriggeringDifferences in ServiceControl.RavenDB.
+ public static readonly string[] RebuildTriggeringParts = ["Maps", "Reduce", "Fields", "AdditionalSources", "AdditionalAssemblies"];
+
+ // Reads the index definitions from a manifest ("Indexes") or from the response of GET /databases/{name}/indexes ("Results").
+ public static List Parse(string json)
+ {
+ using var document = JsonDocument.Parse(json);
+
+ if (!document.RootElement.TryGetProperty("Indexes", out var indexes) && !document.RootElement.TryGetProperty("Results", out indexes))
+ {
+ throw new JsonException("The document has no Indexes or Results property.");
+ }
+
+ var definitions = new List();
+
+ foreach (var index in indexes.EnumerateArray())
+ {
+ var parts = new Dictionary();
+
+ foreach (var part in RebuildTriggeringParts)
+ {
+ parts[part] = index.TryGetProperty(part, out var value) ? Canonicalize(value, isSet: SetParts.Contains(part)) : null;
+ }
+
+ definitions.Add(new RavenDBIndexDefinition
+ {
+ Name = index.GetProperty("Name").GetString(),
+ LockMode = index.TryGetProperty("LockMode", out var lockMode) && lockMode.ValueKind == JsonValueKind.String ? lockMode.GetString() : null,
+ Parts = parts
+ });
+ }
+
+ return definitions;
+ }
+
+ public static string[] GetRebuildTriggeringDifferences(RavenDBIndexDefinition existing, RavenDBIndexDefinition target) =>
+ RebuildTriggeringParts.Where(part => !string.Equals(existing.Parts[part], target.Parts[part], StringComparison.Ordinal)).ToArray();
+
+ // Null, an empty object and an empty collection are the same value: RavenDB returns unset options as null.
+ // Line endings are normalized because the client renders a map with the line ending of its operating system.
+ static string Canonicalize(JsonElement element, bool isSet = false)
+ {
+ if (element.ValueKind is JsonValueKind.Null or JsonValueKind.Undefined)
+ {
+ return null;
+ }
+
+ if (element.ValueKind == JsonValueKind.String)
+ {
+ return JsonSerializer.Serialize(element.GetString().Replace("\r\n", "\n"));
+ }
+
+ if (element.ValueKind == JsonValueKind.Object)
+ {
+ var properties = element.EnumerateObject()
+ .Select(property => (property.Name, Value: Canonicalize(property.Value)))
+ .Where(property => property.Value != null)
+ .OrderBy(property => property.Name, StringComparer.Ordinal)
+ .Select(property => $"{JsonSerializer.Serialize(property.Name)}:{property.Value}")
+ .ToList();
+
+ return properties.Count == 0 ? null : $"{{{string.Join(",", properties)}}}";
+ }
+
+ if (element.ValueKind == JsonValueKind.Array)
+ {
+ var items = element.EnumerateArray().Select(item => Canonicalize(item)).Where(item => item != null);
+ var list = (isSet ? items.OrderBy(item => item, StringComparer.Ordinal) : items).ToList();
+
+ return list.Count == 0 ? null : $"[{string.Join(",", list)}]";
+ }
+
+ return element.GetRawText();
+ }
+
+ static readonly HashSet SetParts = ["Maps", "AdditionalAssemblies"];
+ }
+
+ public record RavenDBIndexChange(string Database, string IndexName, string[] Differences, bool IsLocked, long? Entries);
+
+ // The indexes that RavenDB rebuilds after an upgrade, because the definition in the new version differs from the one on the server.
+ public class RavenDBIndexChanges
+ {
+ public string InstanceName { get; init; }
+
+ public IReadOnlyList Changes { get; init; }
+
+ public bool HasLockedIndexes => Changes.Any(change => change.IsLocked);
+
+ // httpGet receives a path relative to the RavenDB server URL and returns the response body.
+ public static async Task Detect(string instanceName, IReadOnlyList target, Func> httpGet, CancellationToken cancellationToken = default)
+ {
+ var targetByName = target.ToDictionary(definition => definition.Name, StringComparer.OrdinalIgnoreCase);
+ var changes = new List();
+
+ foreach (var database in await GetDatabaseNames(httpGet, cancellationToken).ConfigureAwait(false))
+ {
+ var path = $"/databases/{Uri.EscapeDataString(database)}/indexes";
+ var existing = RavenDBIndexDefinitions.Parse(await httpGet($"{path}?start=0&pageSize={int.MaxValue}", cancellationToken).ConfigureAwait(false));
+ Dictionary entries = null;
+
+ foreach (var existingDefinition in existing)
+ {
+ if (!targetByName.TryGetValue(existingDefinition.Name, out var targetDefinition))
+ {
+ continue;
+ }
+
+ var differences = RavenDBIndexDefinitions.GetRebuildTriggeringDifferences(existingDefinition, targetDefinition);
+ if (differences.Length == 0)
+ {
+ continue;
+ }
+
+ entries ??= await TryGetEntryCounts(httpGet, path, cancellationToken).ConfigureAwait(false);
+ changes.Add(new RavenDBIndexChange(database, existingDefinition.Name, differences, existingDefinition.IsLocked,
+ entries.TryGetValue(existingDefinition.Name, out var count) ? count : null));
+ }
+ }
+
+ return new RavenDBIndexChanges { InstanceName = instanceName, Changes = changes };
+ }
+
+ static async Task> GetDatabaseNames(Func> httpGet, CancellationToken cancellationToken)
+ {
+ using var document = JsonDocument.Parse(await httpGet($"/databases?start=0&pageSize={int.MaxValue}&namesOnly=true", cancellationToken).ConfigureAwait(false));
+
+ return document.RootElement.GetProperty("Databases").EnumerateArray().Select(name => name.GetString()).ToList();
+ }
+
+ // The number of index entries shows the operator how large the rebuild is. The upgrade check works without it.
+ static async Task> TryGetEntryCounts(Func> httpGet, string indexesPath, CancellationToken cancellationToken)
+ {
+ var counts = new Dictionary(StringComparer.OrdinalIgnoreCase);
+
+ try
+ {
+ using var document = JsonDocument.Parse(await httpGet($"{indexesPath}/stats", cancellationToken).ConfigureAwait(false));
+
+ foreach (var stats in document.RootElement.GetProperty("Results").EnumerateArray())
+ {
+ counts[stats.GetProperty("Name").GetString()] = stats.GetProperty("EntriesCount").GetInt64();
+ }
+ }
+ catch (Exception e) when (e is not OperationCanceledException || !cancellationToken.IsCancellationRequested)
+ {
+ counts.Clear();
+ }
+
+ return counts;
+ }
+
+ public string Describe()
+ {
+ var text = new StringBuilder();
+ text.AppendLine($"The upgrade of {InstanceName} changes the definition of {Changes.Count} RavenDB {(Changes.Count == 1 ? "index" : "indexes")}:");
+ text.AppendLine();
+
+ foreach (var change in Changes)
+ {
+ var size = change.Entries is { } entries ? $", {entries.ToString("N0", CultureInfo.InvariantCulture)} entries" : "";
+ var locked = change.IsLocked ? ", LOCKED" : "";
+ text.AppendLine($"• {change.IndexName} (database {change.Database}{size}{locked}): {string.Join(", ", change.Differences)} changed");
+ }
+
+ text.AppendLine();
+ text.AppendLine("RavenDB rebuilds each of these indexes when the upgraded instance starts. The instance stays available during a rebuild. The rebuild of a very large index can take hours or days. During that time the instance uses more CPU, RAM and disk IO, ingestion is slower, and the database needs disk space for the old and the new index.");
+
+ if (HasLockedIndexes)
+ {
+ text.AppendLine();
+ text.AppendLine("The upgraded instance does not start while a changed index is locked. Unlock the indexes marked LOCKED in RavenDB Studio before the upgrade.");
+ }
+
+ text.AppendLine();
+ text.Append($"More information: {UpgradeGuidanceUrl}");
+
+ return text.ToString();
+ }
+
+ public const string UpgradeGuidanceUrl = "https://docs.particular.net/servicecontrol/upgrades/#upgrades-that-rebuild-indexes";
+ }
+}
diff --git a/src/TestHelper/IndexDefinitionManifest.cs b/src/TestHelper/IndexDefinitionManifest.cs
new file mode 100644
index 0000000000..92206a2b63
--- /dev/null
+++ b/src/TestHelper/IndexDefinitionManifest.cs
@@ -0,0 +1,53 @@
+namespace TestHelper;
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text.Encodings.Web;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+using Raven.Client.Documents.Indexes;
+using ServiceControl.RavenDB;
+
+///
+/// Renders the manifest that ships with a RavenDB persister. The installer compares it with the index definitions on
+/// the server before an upgrade. It holds only the parts of a definition that make RavenDB rebuild the index.
+///
+public static class IndexDefinitionManifest
+{
+ public const string FileName = "index-definitions.manifest";
+
+ public static string Render(IEnumerable indexes)
+ {
+ var definitions = indexes
+ .OrderBy(index => index.IndexName, StringComparer.Ordinal)
+ .Select(index =>
+ {
+ // The conventions change the rendered definition, for example how enum literals appear in a map.
+ index.Conventions = DocumentStoreConventions.Create();
+ var definition = index.CreateIndexDefinition();
+
+ return new
+ {
+ Name = index.IndexName,
+ Maps = definition.Maps.Select(NormalizeLineEndings).OrderBy(map => map, StringComparer.Ordinal),
+ Reduce = NormalizeLineEndings(definition.Reduce),
+ Fields = new SortedDictionary(definition.Fields, StringComparer.Ordinal),
+ AdditionalSources = new SortedDictionary(definition.AdditionalSources.ToDictionary(source => source.Key, source => NormalizeLineEndings(source.Value)), StringComparer.Ordinal),
+ definition.AdditionalAssemblies
+ };
+ });
+
+ return NormalizeLineEndings(JsonSerializer.Serialize(new { Indexes = definitions }, JsonOptions)) + "\n";
+ }
+
+ public static string NormalizeLineEndings(string text) => text?.Replace("\r\n", "\n");
+
+ static readonly JsonSerializerOptions JsonOptions = new()
+ {
+ WriteIndented = true,
+ Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
+ Converters = { new JsonStringEnumConverter() },
+ DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
+ };
+}