mirror of https://github.com/Squidex/squidex.git
7 changed files with 104 additions and 18 deletions
@ -0,0 +1,71 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Migration02_AddPatterns.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Squidex.Domain.Apps.Entities.Apps; |
||||
|
using Squidex.Domain.Apps.Entities.Apps.Commands; |
||||
|
using Squidex.Domain.Apps.Entities.Apps.Repositories; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.Migrations; |
||||
|
using Squidex.Infrastructure.States; |
||||
|
|
||||
|
namespace Migrate_01 |
||||
|
{ |
||||
|
public sealed class Migration02_AddPatterns : IMigration |
||||
|
{ |
||||
|
private readonly InitialPatterns initialPatterns; |
||||
|
private readonly IStateFactory stateFactory; |
||||
|
private readonly IAppRepository appRepository; |
||||
|
|
||||
|
public int FromVersion { get; } = 1; |
||||
|
|
||||
|
public int ToVersion { get; } = 2; |
||||
|
|
||||
|
public Migration02_AddPatterns( |
||||
|
InitialPatterns initialPatterns, |
||||
|
IStateFactory stateFactory, |
||||
|
IAppRepository appRepository) |
||||
|
{ |
||||
|
this.initialPatterns = initialPatterns; |
||||
|
this.appRepository = appRepository; |
||||
|
this.stateFactory = stateFactory; |
||||
|
} |
||||
|
|
||||
|
public async Task UpdateAsync() |
||||
|
{ |
||||
|
var ids = await appRepository.QueryAppIdsAsync(); |
||||
|
|
||||
|
foreach (var id in ids) |
||||
|
{ |
||||
|
var app = await stateFactory.CreateAsync<AppDomainObject>(id); |
||||
|
|
||||
|
if (app.State.Patterns.Count == 0) |
||||
|
{ |
||||
|
foreach (var pattern in initialPatterns.Values) |
||||
|
{ |
||||
|
var command = |
||||
|
new AddPattern |
||||
|
{ |
||||
|
Actor = app.State.CreatedBy, |
||||
|
AppId = new NamedId<Guid>(app.State.Id, app.State.Name), |
||||
|
Name = pattern.Name, |
||||
|
PatternId = Guid.NewGuid(), |
||||
|
Pattern = pattern.Pattern, |
||||
|
Message = pattern.Message |
||||
|
}; |
||||
|
|
||||
|
app.AddPattern(command); |
||||
|
} |
||||
|
|
||||
|
await app.WriteStateAsync(app.Version + initialPatterns.Count); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue