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 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) if (schema == null)
{ {
@ -126,7 +126,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Contents
if (contentEntity != null) 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) if (schema == null)
{ {

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

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

81
tools/Migrate_01/Migration01.cs

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

Loading…
Cancel
Save