Browse Source

Migrator fixed.

pull/206/head
Sebastian Stehle 9 years ago
parent
commit
fc70342654
  1. 4
      src/Squidex.Domain.Apps.Entities.MongoDb/Contents/MongoContentRepository.cs
  2. 27
      src/Squidex/Config/Domain/SerializationServices.cs
  3. 81
      tools/Migrate_01/Migration01.cs

4
src/Squidex.Domain.Apps.Entities.MongoDb/Contents/MongoContentRepository.cs

@ -75,7 +75,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Contents
{
var documentId = $"{key}_{newVersion}";
var schema = await appProvider.GetSchemaAsync(value.AppId, value.SchemaId);
var schema = await appProvider.GetSchemaAsync(value.AppId, value.SchemaId, true);
if (schema == null)
{
@ -126,7 +126,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Contents
if (contentEntity != null)
{
var schema = await appProvider.GetSchemaAsync(contentEntity.AppId, contentEntity.SchemaId);
var schema = await appProvider.GetSchemaAsync(contentEntity.AppId, contentEntity.SchemaId, true);
if (schema == null)
{

27
src/Squidex/Config/Domain/SerializationServices.cs

@ -33,15 +33,10 @@ namespace Squidex.Config.Domain
.MapUnmapped(typeof(SquidexCoreModel).Assembly)
.MapUnmapped(typeof(SquidexEvents).Assembly)
.MapUnmapped(typeof(SquidexInfrastructure).Assembly);
private static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings();
private static readonly FieldRegistry FieldRegistry = new FieldRegistry(TypeNameRegistry);
public static JsonSerializerSettings DefaultJsonSettings
{
get { return SerializerSettings; }
}
private static readonly FieldRegistry FieldRegistry = new FieldRegistry(TypeNameRegistry);
private static void ConfigureJson(JsonSerializerSettings settings, TypeNameHandling typeNameHandling)
private static JsonSerializerSettings ConfigureJson(JsonSerializerSettings settings, TypeNameHandling typeNameHandling)
{
settings.SerializationBinder = new TypeNameSerializationBinder(TypeNameRegistry);
@ -70,21 +65,21 @@ namespace Squidex.Config.Domain
settings.TypeNameHandling = typeNameHandling;
settings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
}
static SerializationServices()
{
ConfigureJson(SerializerSettings, TypeNameHandling.Auto);
BsonJsonConvention.Register(JsonSerializer.Create(SerializerSettings));
return settings;
}
public static IServiceCollection AddMySerializers(this IServiceCollection services)
{
services.AddSingletonAs(t => TypeNameRegistry);
var serializerSettings = ConfigureJson(new JsonSerializerSettings(), TypeNameHandling.Auto);
var serializerInstance = JsonSerializer.Create(serializerSettings);
services.AddSingletonAs(t => FieldRegistry);
services.AddSingletonAs(t => SerializerSettings);
services.AddSingletonAs(t => JsonSerializer.Create(SerializerSettings));
services.AddSingletonAs(t => serializerSettings);
services.AddSingletonAs(t => serializerInstance);
services.AddSingletonAs(t => TypeNameRegistry);
BsonJsonConvention.Register(serializerInstance);
return services;
}

81
tools/Migrate_01/Migration01.cs

@ -7,8 +7,8 @@
// ==========================================================================
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Domain.Apps.Entities.Apps;
using Squidex.Domain.Apps.Entities.Assets;
@ -31,8 +31,8 @@ namespace Migrate_01
private readonly IEventStore eventStore;
private readonly IEventDataFormatter eventDataFormatter;
private readonly IStateFactory stateFactory;
private readonly Timer timer = new Timer { AutoReset = false, Interval = 5000 };
private readonly TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
private readonly Timer timer;
private readonly TaskCompletionSource<object> subscriptionTcs = new TaskCompletionSource<object>();
public int FromVersion { get; } = 0;
@ -49,68 +49,75 @@ namespace Migrate_01
this.eventStore = eventStore;
this.stateFactory = stateFactory;
timer.Elapsed += (sender, e) =>
{
tcs.TrySetResult(true);
};
timer = new Timer(d => subscriptionTcs.TrySetResult(true));
}
public async Task UpdateAsync()
{
var subscription = eventStore.CreateSubscription(this, ".*");
await tcs.Task;
await subscriptionTcs.Task;
await subscription.StopAsync();
}
public async Task OnEventAsync(IEventSubscription subscription, StoredEvent storedEvent)
{
var @event = ParseKnownEvent(storedEvent);
if (@event != null)
try
{
var version = storedEvent.EventStreamNumber;
timer.Change(Timeout.Infinite, Timeout.Infinite);
var @event = ParseKnownEvent(storedEvent);
if (@event.Payload is AssetEvent assetEvent)
if (@event != null)
{
var asset = await stateFactory.CreateAsync<AssetDomainObject>(assetEvent.AssetId);
var version = storedEvent.EventStreamNumber;
asset.UpdateState(asset.State.Apply(@event));
if (@event.Payload is AssetEvent assetEvent)
{
var asset = await stateFactory.CreateAsync<AssetDomainObject>(assetEvent.AssetId);
await asset.WriteStateAsync(version);
}
else if (@event.Payload is ContentEvent contentEvent)
{
var content = await stateFactory.CreateAsync<ContentDomainObject>(contentEvent.ContentId);
asset.UpdateState(asset.State.Apply(@event));
content.UpdateState(content.State.Apply(@event));
await asset.WriteStateAsync(version);
}
else if (@event.Payload is ContentEvent contentEvent)
{
var content = await stateFactory.CreateAsync<ContentDomainObject>(contentEvent.ContentId);
await content.WriteStateAsync(version);
}
else if (@event.Payload is SchemaEvent schemaEvent)
{
var schema = await stateFactory.GetSingleAsync<SchemaDomainObject>(schemaEvent.SchemaId.Id);
content.UpdateState(content.State.Apply(@event));
schema.UpdateState(schema.State.Apply(@event, fieldRegistry));
await content.WriteStateAsync(version);
}
else if (@event.Payload is SchemaEvent schemaEvent)
{
var schema = await stateFactory.GetSingleAsync<SchemaDomainObject>(schemaEvent.SchemaId.Id);
await schema.WriteStateAsync(version);
}
else if (@event.Payload is AppEvent appEvent)
{
var app = await stateFactory.GetSingleAsync<AppDomainObject>(appEvent.AppId.Id);
schema.UpdateState(schema.State.Apply(@event, fieldRegistry));
app.UpdateState(app.State.Apply(@event));
await schema.WriteStateAsync(version);
}
else if (@event.Payload is AppEvent appEvent)
{
var app = await stateFactory.GetSingleAsync<AppDomainObject>(appEvent.AppId.Id);
await app.WriteStateAsync(version);
app.UpdateState(app.State.Apply(@event));
await app.WriteStateAsync(version);
}
}
}
timer.Stop();
timer.Start();
timer.Change(5000, 0);
}
catch (Exception ex)
{
subscriptionTcs.SetException(ex);
}
}
public Task OnErrorAsync(IEventSubscription subscription, Exception exception)
{
subscriptionTcs.TrySetException(exception);
return TaskHelper.Done;
}

Loading…
Cancel
Save