mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.9 KiB
87 lines
2.9 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Squidex.Infrastructure.Log;
|
|
|
|
namespace Squidex.Infrastructure.Migrations
|
|
{
|
|
public sealed class Migrator
|
|
{
|
|
private readonly ISemanticLog log;
|
|
private readonly IMigrationStatus migrationStatus;
|
|
private readonly IMigrationPath migrationPath;
|
|
|
|
public int LockWaitMs { get; set; } = 500;
|
|
|
|
public Migrator(IMigrationStatus migrationStatus, IMigrationPath migrationPath, ISemanticLog log)
|
|
{
|
|
Guard.NotNull(migrationStatus, nameof(migrationStatus));
|
|
Guard.NotNull(migrationPath, nameof(migrationPath));
|
|
Guard.NotNull(log, nameof(log));
|
|
|
|
this.migrationStatus = migrationStatus;
|
|
this.migrationPath = migrationPath;
|
|
|
|
this.log = log;
|
|
}
|
|
|
|
public async Task MigrateAsync()
|
|
{
|
|
var version = 0;
|
|
|
|
try
|
|
{
|
|
while (!await migrationStatus.TryLockAsync())
|
|
{
|
|
log.LogInformation(w => w
|
|
.WriteProperty("action", "Migrate")
|
|
.WriteProperty("mesage", $"Waiting {LockWaitMs}ms to acquire lock."));
|
|
|
|
await Task.Delay(LockWaitMs);
|
|
}
|
|
|
|
version = await migrationStatus.GetVersionAsync();
|
|
|
|
while (true)
|
|
{
|
|
var migrationStep = migrationPath.GetNext(version);
|
|
|
|
if (migrationStep.Migrations == null || !migrationStep.Migrations.Any())
|
|
{
|
|
break;
|
|
}
|
|
|
|
foreach (var migration in migrationStep.Migrations)
|
|
{
|
|
var name = migration.GetType().ToString();
|
|
|
|
log.LogInformation(w => w
|
|
.WriteProperty("action", "Migration")
|
|
.WriteProperty("status", "Started")
|
|
.WriteProperty("migrator", name));
|
|
|
|
using (log.MeasureInformation(w => w
|
|
.WriteProperty("action", "Migration")
|
|
.WriteProperty("status", "Completed")
|
|
.WriteProperty("migrator", name)))
|
|
{
|
|
await migration.UpdateAsync();
|
|
}
|
|
}
|
|
|
|
version = migrationStep.Version;
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
await migrationStatus.UnlockAsync(version);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|