RC2 Breaking - Ensure migrations persist the executed key, when executed. (#16113)

* Adds new functionality to the migrations.

This requires a migration to call Context.SetDone() on the migration context. This happens automatically on scoped migrations before the scope is completed. But migrations inheriting the UnScopedMigrationBase needs to call this manually, inside the scopes or when it is considered done.

Thereby, we minimize the risk (and eliminate it for SqlServer) that a migration is executed but the state is not saved.

If a migration is executed without the SetDone is called, the migration upgrader throws an error, so we do not start executing the next migration

* Updated tests

* Renamed after review suggestion

* Rename in test

* More renaming after review

* Remove public modifier from interface

* Add missing space in exception message

---------

Co-authored-by: nikolajlauridsen <nikolajlauridsen@protonmail.ch>
This commit is contained in:
Bjarke Berg
2024-04-22 12:21:16 +02:00
committed by GitHub
parent c6898ec3f6
commit 7b46d44eeb
12 changed files with 116 additions and 22 deletions

View File

@@ -2,16 +2,22 @@
// See LICENSE for more details.
using System.Linq;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Migrations;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.Migrations;
using Umbraco.Cms.Infrastructure.Migrations.Install;
using Umbraco.Cms.Infrastructure.Migrations.Upgrade;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
using Umbraco.Cms.Infrastructure.Scoping;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
@@ -23,7 +29,22 @@ public class AdvancedMigrationTests : UmbracoIntegrationTest
{
private IUmbracoVersion UmbracoVersion => GetRequiredService<IUmbracoVersion>();
private IEventAggregator EventAggregator => GetRequiredService<IEventAggregator>();
private IMigrationPlanExecutor MigrationPlanExecutor => GetRequiredService<IMigrationPlanExecutor>();
private ICoreScopeProvider CoreScopeProvider => GetRequiredService<ICoreScopeProvider>();
private IScopeAccessor ScopeAccessor => GetRequiredService<IScopeAccessor>();
private ILoggerFactory LoggerFactory => GetRequiredService<ILoggerFactory>();
private IMigrationBuilder MigrationBuilder => GetRequiredService<IMigrationBuilder>();
private IUmbracoDatabaseFactory UmbracoDatabaseFactory => GetRequiredService<IUmbracoDatabaseFactory>();
private IPublishedSnapshotService PublishedSnapshotService => GetRequiredService<IPublishedSnapshotService>();
private DistributedCache DistributedCache => GetRequiredService<DistributedCache>();
private IMigrationPlanExecutor MigrationPlanExecutor => new MigrationPlanExecutor(
CoreScopeProvider,
ScopeAccessor,
LoggerFactory,
MigrationBuilder,
UmbracoDatabaseFactory,
PublishedSnapshotService,
DistributedCache,
Mock.Of<IKeyValueService>());
[Test]
public void CreateTableOfTDto()

View File

@@ -40,7 +40,7 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Migrations.Expres
.WithColumn("bar").AsInt32().PrimaryKey("PK_foo")
.Do();
// (TableName, ColumnName, ConstraintName)
// (TableName, ColumnName, ConstraintName)
var constraint = database.SqlContext.SqlSyntax.GetConstraintsPerColumn(database).Single();
Assert.Multiple(() =>
@@ -92,7 +92,7 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Migrations.Expres
.ForeignKey("MY_SUPER_COOL_FK", "foo", "bar")
.Do();
// (TableName, ColumnName, ConstraintName)
// (TableName, ColumnName, ConstraintName)
var constraint = database.SqlContext.SqlSyntax
.GetConstraintsPerColumn(database)
.Single(x => x.Item3 == "MY_SUPER_COOL_FK");

View File

@@ -263,6 +263,8 @@ internal class AssertScopeUnscopedTestMigration : UnscopedMigrationBase
using var scope = _scopeProvider.CreateScope();
Assert.IsNull(((Scope)scope).ParentScope);
Context.Complete();
}
}
@@ -354,4 +356,4 @@ internal class SimpleMigrationStep : MigrationBase
: base(context) => _logger = logger;
protected override void Migrate() => _logger.LogDebug("Here be migration");
}
}