|
|
@ -5,13 +5,11 @@ |
|
|
// All rights reserved. Licensed under the MIT license.
|
|
|
// All rights reserved. Licensed under the MIT license.
|
|
|
// ==========================================================================
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
|
using MongoDB.Bson; |
|
|
using MongoDB.Bson; |
|
|
using MongoDB.Driver; |
|
|
using MongoDB.Driver; |
|
|
using Newtonsoft.Json.Linq; |
|
|
using Newtonsoft.Json.Linq; |
|
|
using Squidex.Domain.Apps.Events; |
|
|
|
|
|
using Squidex.Infrastructure; |
|
|
|
|
|
using Squidex.Infrastructure.EventSourcing; |
|
|
using Squidex.Infrastructure.EventSourcing; |
|
|
using Squidex.Infrastructure.Migrations; |
|
|
using Squidex.Infrastructure.Migrations; |
|
|
using Squidex.Infrastructure.MongoDb; |
|
|
using Squidex.Infrastructure.MongoDb; |
|
|
@ -35,24 +33,37 @@ namespace Migrate_01.Migrations |
|
|
|
|
|
|
|
|
var filter = Builders<BsonDocument>.Filter; |
|
|
var filter = Builders<BsonDocument>.Filter; |
|
|
|
|
|
|
|
|
|
|
|
var writesBatches = new List<WriteModel<BsonDocument>>(); |
|
|
|
|
|
|
|
|
|
|
|
async Task WriteAsync(WriteModel<BsonDocument> model, bool force) |
|
|
|
|
|
{ |
|
|
|
|
|
if (model != null) |
|
|
|
|
|
{ |
|
|
|
|
|
writesBatches.Add(model); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (writesBatches.Count == 1000 || (force && writesBatches.Count > 0)) |
|
|
|
|
|
{ |
|
|
|
|
|
await collection.BulkWriteAsync(writesBatches); |
|
|
|
|
|
|
|
|
|
|
|
writesBatches.Clear(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
await collection.Find(new BsonDocument()).ForEachAsync(async commit => |
|
|
await collection.Find(new BsonDocument()).ForEachAsync(async commit => |
|
|
{ |
|
|
{ |
|
|
foreach (BsonDocument @event in commit["Events"].AsBsonArray) |
|
|
foreach (BsonDocument @event in commit["Events"].AsBsonArray) |
|
|
{ |
|
|
{ |
|
|
var meta = JObject.Parse(@event["Metadata"].AsString); |
|
|
var meta = JObject.Parse(@event["Metadata"].AsString); |
|
|
var data = JObject.Parse(@event["Payload"].AsString); |
|
|
|
|
|
|
|
|
|
|
|
if (data.TryGetValue("appId", out var appId)) |
|
|
|
|
|
{ |
|
|
|
|
|
meta[SquidexHeaders.AppId] = NamedId<Guid>.Parse(appId.ToString(), Guid.TryParse).Id; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@event.Remove("EventId"); |
|
|
@event.Remove("EventId"); |
|
|
@event["Metadata"] = meta.ToBson(); |
|
|
@event["Metadata"] = meta.ToBson(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
await collection.ReplaceOneAsync(filter.Eq("_id", commit["_id"].AsString), commit); |
|
|
await WriteAsync(new ReplaceOneModel<BsonDocument>(filter.Eq("_id", commit["_id"].AsString), commit), false); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
await WriteAsync(null, true); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|