2021-03-03 10:40:16 +11:00
|
|
|
// Copyright (c) Umbraco.
|
2020-12-20 08:36:11 +01:00
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2017-12-22 12:29:56 +01:00
|
|
|
using System.Data;
|
2020-09-17 13:36:26 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-11-15 19:34:32 +01:00
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Events;
|
2022-01-13 17:44:11 +00:00
|
|
|
using Umbraco.Cms.Core.Persistence.Querying;
|
2022-06-21 08:09:38 +02:00
|
|
|
using Umbraco.Cms.Core.Scoping;
|
2021-02-12 12:40:08 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Migrations;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
2022-01-13 17:44:11 +00:00
|
|
|
using Umbraco.Cms.Infrastructure.Scoping;
|
2022-06-21 08:09:38 +02:00
|
|
|
using IScope = Umbraco.Cms.Infrastructure.Scoping.IScope;
|
2022-04-26 10:22:37 +01:00
|
|
|
using IScopeProvider = Umbraco.Cms.Infrastructure.Scoping.IScopeProvider;
|
2022-06-21 08:09:38 +02:00
|
|
|
#if DEBUG_SCOPES
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
#endif
|
2017-12-22 12:29:56 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Migrations;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class MigrationTests
|
2017-12-22 12:29:56 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
public class TestScopeProvider : IScopeProvider, IScopeAccessor
|
2017-12-22 12:29:56 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
public TestScopeProvider(IScope scope) => AmbientScope = scope;
|
2017-12-22 12:29:56 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IScope CreateScope(
|
|
|
|
|
IsolationLevel isolationLevel = IsolationLevel.Unspecified,
|
|
|
|
|
RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
|
|
|
|
|
IEventDispatcher eventDispatcher = null,
|
|
|
|
|
IScopedNotificationPublisher notificationPublisher = null,
|
|
|
|
|
bool? scopeFileSystems = null,
|
|
|
|
|
bool callContext = false,
|
|
|
|
|
bool autoComplete = false) => AmbientScope;
|
2017-12-22 12:29:56 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IScope CreateDetachedScope(
|
|
|
|
|
IsolationLevel isolationLevel = IsolationLevel.Unspecified,
|
|
|
|
|
RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
|
|
|
|
|
IEventDispatcher eventDispatcher = null,
|
|
|
|
|
IScopedNotificationPublisher notificationPublisher = null,
|
|
|
|
|
bool? scopeFileSystems = null) => throw new NotImplementedException();
|
2017-12-22 12:29:56 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public void AttachScope(IScope scope, bool callContext = false) => throw new NotImplementedException();
|
2017-12-22 12:29:56 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IScope DetachScope() => throw new NotImplementedException();
|
2017-12-22 12:29:56 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IScopeContext Context { get; set; }
|
2020-09-17 13:36:26 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public IQuery<T> CreateQuery<T>() => SqlContext.Query<T>();
|
2020-12-08 09:19:51 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public ISqlContext SqlContext { get; set; }
|
2019-12-20 15:12:54 +11:00
|
|
|
|
2024-10-28 12:10:38 +01:00
|
|
|
public IScope AmbientScope { get; }
|
|
|
|
|
|
2019-12-20 15:12:54 +11:00
|
|
|
#if DEBUG_SCOPES
|
2024-10-28 12:10:38 +01:00
|
|
|
public IEnumerable<ScopeInfo> ScopeInfos => throw new NotImplementedException();
|
|
|
|
|
|
|
|
|
|
public ScopeInfo GetScopeInfo(IScope scope) => throw new NotImplementedException();
|
2019-12-20 15:12:54 +11:00
|
|
|
#endif
|
2024-10-28 12:10:38 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2018-11-15 19:34:32 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
private class TestPlan : MigrationPlan
|
|
|
|
|
{
|
|
|
|
|
public TestPlan()
|
|
|
|
|
: base("Test")
|
2021-06-15 15:05:57 +10:00
|
|
|
{
|
|
|
|
|
}
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2021-06-15 15:05:57 +10:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
private MigrationContext GetMigrationContext() =>
|
|
|
|
|
new(
|
|
|
|
|
new TestPlan(),
|
|
|
|
|
Mock.Of<IUmbracoDatabase>(),
|
|
|
|
|
Mock.Of<ILogger<MigrationContext>>());
|
2018-11-15 19:34:32 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
2025-03-03 11:54:22 +01:00
|
|
|
public async Task RunGoodMigration()
|
2022-06-21 08:09:38 +02:00
|
|
|
{
|
|
|
|
|
var migrationContext = GetMigrationContext();
|
|
|
|
|
MigrationBase migration = new GoodMigration(migrationContext);
|
2025-03-03 11:54:22 +01:00
|
|
|
await migration.RunAsync();
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void DetectBadMigration1()
|
|
|
|
|
{
|
|
|
|
|
var migrationContext = GetMigrationContext();
|
|
|
|
|
MigrationBase migration = new BadMigration1(migrationContext);
|
2025-03-03 11:54:22 +01:00
|
|
|
Assert.ThrowsAsync<IncompleteMigrationExpressionException>(migration.RunAsync);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void DetectBadMigration2()
|
|
|
|
|
{
|
|
|
|
|
var migrationContext = GetMigrationContext();
|
|
|
|
|
MigrationBase migration = new BadMigration2(migrationContext);
|
2025-03-03 11:54:22 +01:00
|
|
|
Assert.ThrowsAsync<IncompleteMigrationExpressionException>(migration.RunAsync);
|
2022-06-21 08:09:38 +02:00
|
|
|
}
|
2018-11-15 19:34:32 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public class GoodMigration : MigrationBase
|
|
|
|
|
{
|
|
|
|
|
public GoodMigration(IMigrationContext context)
|
|
|
|
|
: base(context)
|
2018-11-15 19:34:32 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
protected override void Migrate() => Execute.Sql(string.Empty).Do();
|
|
|
|
|
}
|
2020-12-08 09:19:51 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public class BadMigration1 : MigrationBase
|
|
|
|
|
{
|
|
|
|
|
public BadMigration1(IMigrationContext context)
|
|
|
|
|
: base(context)
|
|
|
|
|
{
|
2018-11-15 19:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
protected override void Migrate() => Alter.Table("foo"); // stop here, don't Do it
|
|
|
|
|
}
|
2020-12-08 09:19:51 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public class BadMigration2 : MigrationBase
|
|
|
|
|
{
|
|
|
|
|
public BadMigration2(IMigrationContext context)
|
|
|
|
|
: base(context)
|
|
|
|
|
{
|
2018-11-15 19:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
protected override void Migrate()
|
2018-11-15 19:34:32 +01:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
Alter.Table("foo"); // stop here, don't Do it
|
2018-11-15 19:34:32 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// and try to start another one
|
|
|
|
|
Alter.Table("bar");
|
2018-11-15 19:34:32 +01:00
|
|
|
}
|
2017-12-22 12:29:56 +01:00
|
|
|
}
|
|
|
|
|
}
|