Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Migrations/AdvancedMigrationTests.cs

298 lines
11 KiB
C#
Raw Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Linq;
using System.Threading.Tasks;
2020-09-17 12:52:25 +02:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
2017-11-01 10:42:46 +01:00
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Migrations;
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.DatabaseModelDefinitions;
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
2017-11-01 10:42:46 +01:00
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Migrations
2017-11-01 10:42:46 +01:00
{
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewEmptyPerTest)]
public class AdvancedMigrationTests : UmbracoIntegrationTest
2017-11-01 10:42:46 +01:00
{
private IUmbracoVersion UmbracoVersion => GetRequiredService<IUmbracoVersion>();
private IEventAggregator EventAggregator => GetRequiredService<IEventAggregator>();
private IMigrationPlanExecutor MigrationPlanExecutor => GetRequiredService<IMigrationPlanExecutor>();
2017-11-01 10:42:46 +01:00
[Test]
public void CreateTableOfTDto()
2017-11-01 10:42:46 +01:00
{
IMigrationBuilder builder = Mock.Of<IMigrationBuilder>();
Mock.Get(builder)
2017-12-22 12:29:56 +01:00
.Setup(x => x.Build(It.IsAny<Type>(), It.IsAny<IMigrationContext>()))
.Returns<Type, IMigrationContext>((t, c) =>
{
if (t != typeof(CreateTableOfTDtoMigration))
{
throw new NotSupportedException();
}
return new CreateTableOfTDtoMigration(c);
});
using (IScope scope = ScopeProvider.CreateScope())
2017-11-01 10:42:46 +01:00
{
2018-12-06 07:48:26 +01:00
var upgrader = new Upgrader(
2018-07-04 15:07:09 +02:00
new MigrationPlan("test")
2018-12-04 17:01:33 +01:00
.From(string.Empty)
.To<CreateTableOfTDtoMigration>("done"));
2017-12-22 12:29:56 +01:00
upgrader.Execute(MigrationPlanExecutor, ScopeProvider, Mock.Of<IKeyValueService>());
2017-11-01 10:42:46 +01:00
var helper = new DatabaseSchemaCreator(scope.Database, LoggerFactory.CreateLogger<DatabaseSchemaCreator>(), LoggerFactory, UmbracoVersion, EventAggregator);
bool exists = helper.TableExists("umbracoUser");
2017-11-01 10:42:46 +01:00
Assert.IsTrue(exists);
scope.Complete();
}
}
[Test]
public void DeleteKeysAndIndexesOfTDto()
2017-11-01 10:42:46 +01:00
{
IMigrationBuilder builder = Mock.Of<IMigrationBuilder>();
Mock.Get(builder)
2017-12-22 12:29:56 +01:00
.Setup(x => x.Build(It.IsAny<Type>(), It.IsAny<IMigrationContext>()))
.Returns<Type, IMigrationContext>((t, c) =>
{
switch (t.Name)
{
case "CreateTableOfTDtoMigration":
return new CreateTableOfTDtoMigration(c);
case "DeleteKeysAndIndexesMigration":
return new DeleteKeysAndIndexesMigration(c);
default:
throw new NotSupportedException();
}
});
using (IScope scope = ScopeProvider.CreateScope())
2017-11-01 10:42:46 +01:00
{
2018-12-06 07:48:26 +01:00
var upgrader = new Upgrader(
2018-07-04 15:07:09 +02:00
new MigrationPlan("test")
2018-12-04 17:01:33 +01:00
.From(string.Empty)
.To<CreateTableOfTDtoMigration>("a")
.To<DeleteKeysAndIndexesMigration>("done"));
2017-11-01 10:42:46 +01:00
upgrader.Execute(MigrationPlanExecutor, ScopeProvider, Mock.Of<IKeyValueService>());
2017-11-01 10:42:46 +01:00
scope.Complete();
}
}
[Test]
public void CreateKeysAndIndexesOfTDto()
2017-11-01 10:42:46 +01:00
{
IMigrationBuilder builder = Mock.Of<IMigrationBuilder>();
Mock.Get(builder)
2017-12-22 12:29:56 +01:00
.Setup(x => x.Build(It.IsAny<Type>(), It.IsAny<IMigrationContext>()))
.Returns<Type, IMigrationContext>((t, c) =>
{
switch (t.Name)
{
case "CreateTableOfTDtoMigration":
return new CreateTableOfTDtoMigration(c);
case "DeleteKeysAndIndexesMigration":
return new DeleteKeysAndIndexesMigration(c);
case "CreateKeysAndIndexesOfTDtoMigration":
return new CreateKeysAndIndexesOfTDtoMigration(c);
default:
throw new NotSupportedException();
}
});
using (IScope scope = ScopeProvider.CreateScope())
2017-11-01 10:42:46 +01:00
{
2018-12-06 07:48:26 +01:00
var upgrader = new Upgrader(
2018-07-04 15:07:09 +02:00
new MigrationPlan("test")
2018-12-04 17:01:33 +01:00
.From(string.Empty)
.To<CreateTableOfTDtoMigration>("a")
.To<DeleteKeysAndIndexesMigration>("b")
.To<CreateKeysAndIndexesOfTDtoMigration>("done"));
2017-11-01 10:42:46 +01:00
upgrader.Execute(MigrationPlanExecutor, ScopeProvider, Mock.Of<IKeyValueService>());
2017-11-01 10:42:46 +01:00
scope.Complete();
}
}
[Test]
public void CreateKeysAndIndexes()
2017-11-01 10:42:46 +01:00
{
IMigrationBuilder builder = Mock.Of<IMigrationBuilder>();
Mock.Get(builder)
2017-12-22 12:29:56 +01:00
.Setup(x => x.Build(It.IsAny<Type>(), It.IsAny<IMigrationContext>()))
.Returns<Type, IMigrationContext>((t, c) =>
{
switch (t.Name)
{
case "CreateTableOfTDtoMigration":
return new CreateTableOfTDtoMigration(c);
case "DeleteKeysAndIndexesMigration":
return new DeleteKeysAndIndexesMigration(c);
case "CreateKeysAndIndexesMigration":
return new CreateKeysAndIndexesMigration(c);
default:
throw new NotSupportedException();
}
});
using (IScope scope = ScopeProvider.CreateScope())
2017-11-01 10:42:46 +01:00
{
2018-12-06 07:48:26 +01:00
var upgrader = new Upgrader(
2018-07-04 15:07:09 +02:00
new MigrationPlan("test")
2018-12-04 17:01:33 +01:00
.From(string.Empty)
.To<CreateTableOfTDtoMigration>("a")
.To<DeleteKeysAndIndexesMigration>("b")
.To<CreateKeysAndIndexesMigration>("done"));
2017-11-01 10:42:46 +01:00
upgrader.Execute(MigrationPlanExecutor, ScopeProvider, Mock.Of<IKeyValueService>());
2017-11-01 10:42:46 +01:00
scope.Complete();
}
}
2017-11-01 15:35:37 +01:00
[Test]
public void CreateColumn()
2017-11-01 15:35:37 +01:00
{
IMigrationBuilder builder = Mock.Of<IMigrationBuilder>();
Mock.Get(builder)
2017-12-22 12:29:56 +01:00
.Setup(x => x.Build(It.IsAny<Type>(), It.IsAny<IMigrationContext>()))
.Returns<Type, IMigrationContext>((t, c) =>
{
switch (t.Name)
{
case "CreateTableOfTDtoMigration":
return new CreateTableOfTDtoMigration(c);
case "CreateColumnMigration":
return new CreateColumnMigration(c);
default:
throw new NotSupportedException();
}
});
using (IScope scope = ScopeProvider.CreateScope())
2017-11-01 15:35:37 +01:00
{
2018-12-06 07:48:26 +01:00
var upgrader = new Upgrader(
2018-07-04 15:07:09 +02:00
new MigrationPlan("test")
2018-12-04 17:01:33 +01:00
.From(string.Empty)
.To<CreateTableOfTDtoMigration>("a")
.To<CreateColumnMigration>("done"));
2017-11-01 15:35:37 +01:00
upgrader.Execute(MigrationPlanExecutor, ScopeProvider, Mock.Of<IKeyValueService>());
2017-11-01 15:35:37 +01:00
scope.Complete();
}
}
2017-11-01 10:42:46 +01:00
public class CreateTableOfTDtoMigration : MigrationBase
{
public CreateTableOfTDtoMigration(IMigrationContext context)
: base(context)
{
}
protected override void Migrate() =>
// Create User table with keys, indexes, etc.
Create.Table<UserDto>().Do();
2017-11-01 10:42:46 +01:00
}
public class DeleteKeysAndIndexesMigration : MigrationBase
{
public DeleteKeysAndIndexesMigration(IMigrationContext context)
: base(context)
{
}
2017-11-01 10:42:46 +01:00
protected override void Migrate()
2017-11-01 10:42:46 +01:00
{
2018-06-04 18:12:23 +02:00
// drops User table keys and indexes
// Execute.DropKeysAndIndexes("umbracoUser");
2017-11-01 10:42:46 +01:00
// drops *all* tables keys and indexes
2019-05-07 19:28:51 +02:00
var tables = SqlSyntax.GetTablesInSchema(Context.Database).ToList();
foreach (string table in tables)
{
2019-05-28 17:49:50 +02:00
Delete.KeysAndIndexes(table, false, true).Do();
}
foreach (string table in tables)
{
2019-05-28 17:49:50 +02:00
Delete.KeysAndIndexes(table, true, false).Do();
}
2017-11-01 10:42:46 +01:00
}
}
public class CreateKeysAndIndexesOfTDtoMigration : MigrationBase
{
public CreateKeysAndIndexesOfTDtoMigration(IMigrationContext context)
: base(context)
{
}
protected override void Migrate() =>
// Create User table keys and indexes.
Create.KeysAndIndexes<UserDto>().Do();
2017-11-01 10:42:46 +01:00
}
public class CreateKeysAndIndexesMigration : MigrationBase
{
public CreateKeysAndIndexesMigration(IMigrationContext context)
: base(context)
{
}
2017-11-01 10:42:46 +01:00
protected override void Migrate()
2017-11-01 10:42:46 +01:00
{
// Creates *all* tables keys and indexes
foreach (Type x in DatabaseSchemaCreator.OrderedTables)
2017-11-01 10:42:46 +01:00
{
// ok - for tests, restrict to Node
if (x != typeof(UserDto))
{
continue;
}
2017-11-01 10:42:46 +01:00
2018-03-21 14:40:59 +01:00
Create.KeysAndIndexes(x).Do();
2017-11-01 10:42:46 +01:00
}
}
}
2017-11-01 15:35:37 +01:00
public class CreateColumnMigration : MigrationBase
{
public CreateColumnMigration(IMigrationContext context)
: base(context)
{
}
2017-11-01 15:35:37 +01:00
protected override void Migrate()
2017-11-01 15:35:37 +01:00
{
// cannot delete the column without this, of course
2019-05-07 19:28:51 +02:00
Delete.KeysAndIndexes("umbracoUser").Do();
2017-11-01 15:35:37 +01:00
2018-06-04 18:12:23 +02:00
Delete.Column("id").FromTable("umbracoUser").Do();
2017-11-01 15:35:37 +01:00
TableDefinition table = DefinitionFactory.GetTableDefinition(typeof(UserDto), SqlSyntax);
ColumnDefinition column = table.Columns.First(x => x.Name == "id");
string create = SqlSyntax.Format(column); // returns [id] INTEGER NOT NULL IDENTITY(1060,1)
2018-06-04 18:12:23 +02:00
Database.Execute($"ALTER TABLE {SqlSyntax.GetQuotedTableName("umbracoUser")} ADD " + create);
2017-11-01 15:35:37 +01:00
}
}
2017-11-01 10:42:46 +01:00
}
}