Add down migration tests

This commit is contained in:
Jordan Brown
2020-07-14 13:29:04 -04:00
parent 4f2b32b757
commit 561f9216ba
4 changed files with 161 additions and 10 deletions
@@ -18,7 +18,7 @@ namespace Tgstation.Server.Host.Database.Migrations
name: "HeartbeatSeconds",
table: "DreamDaemonSettings",
nullable: false,
defaultValue: 0u);
defaultValue: 0U);
}
/// <inheritdoc />
@@ -27,9 +27,44 @@ namespace Tgstation.Server.Host.Database.Migrations
if (migrationBuilder == null)
throw new ArgumentNullException(nameof(migrationBuilder));
migrationBuilder.DropColumn(
name: "HeartbeatSeconds",
table: "DreamDaemonSettings");
migrationBuilder.RenameTable(
name: "DreamDaemonSettings",
newName: "DreamDaemonSettings_down");
migrationBuilder.CreateTable(
name: "DreamDaemonSettings",
columns: table => new
{
Id = table.Column<long>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
AllowWebClient = table.Column<bool>(nullable: false),
SecurityLevel = table.Column<int>(nullable: false),
PrimaryPort = table.Column<ushort>(nullable: false),
SecondaryPort = table.Column<ushort>(nullable: false),
StartupTimeout = table.Column<uint>(nullable: false),
AutoStart = table.Column<bool>(nullable: false),
SoftRestart = table.Column<bool>(nullable: false),
SoftShutdown = table.Column<bool>(nullable: false),
ProcessId = table.Column<int>(nullable: true),
AccessToken = table.Column<string>(nullable: true),
InstanceId = table.Column<long>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_DreamDaemonSettings", x => x.Id);
table.ForeignKey(
name: "FK_DreamDaemonSettings_Instances_InstanceId",
column: x => x.InstanceId,
principalTable: "Instances",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.Sql(
$"INSERT INTO DreamDaemonSettings SELECT Id,AllowWebClient,SecurityLevel,PrimaryPort,SecondaryPort,StartupTimeout,AutoStart,SoftRestart,SoftShutdown,ProcessId,AccessToken,InstanceId FROM DreamDaemonSettings_down");
migrationBuilder.DropTable(
name: "DreamDaemonSettings_down");
}
}
}
@@ -25,9 +25,42 @@ namespace Tgstation.Server.Host.Database.Migrations
{
if (migrationBuilder == null)
throw new ArgumentNullException(nameof(migrationBuilder));
migrationBuilder.DropColumn(
name: "TopicRequestTimeout",
table: "DreamDaemonSettings");
migrationBuilder.RenameTable(
name: "DreamDaemonSettings",
newName: "DreamDaemonSettings_down");
migrationBuilder.CreateTable(
name: "DreamDaemonSettings",
columns: table => new
{
Id = table.Column<long>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
AllowWebClient = table.Column<bool>(nullable: false),
SecurityLevel = table.Column<int>(nullable: false),
PrimaryPort = table.Column<ushort>(nullable: false),
SecondaryPort = table.Column<ushort>(nullable: false),
StartupTimeout = table.Column<uint>(nullable: false),
AutoStart = table.Column<bool>(nullable: false),
InstanceId = table.Column<long>(nullable: false),
HeartbeatSeconds = table.Column<uint>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_DreamDaemonSettings", x => x.Id);
table.ForeignKey(
name: "FK_DreamDaemonSettings_Instances_InstanceId",
column: x => x.InstanceId,
principalTable: "Instances",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.Sql(
$"INSERT INTO DreamDaemonSettings SELECT Id,AllowWebClient,SecurityLevel,PrimaryPort,SecondaryPort,StartupTimeout,AutoStart,InstanceId,HeartbeatSeconds FROM DreamDaemonSettings_down");
migrationBuilder.DropTable(
name: "DreamDaemonSettings_down");
}
}
}
@@ -131,9 +131,37 @@ namespace Tgstation.Server.Host.Database.Migrations
name: "CompileJobs_down",
newName: "CompileJobs");
migrationBuilder.DropColumn(
name: "RequireDMApiValidation",
table: "DreamMakerSettings");
migrationBuilder.RenameTable(
name: "DreamMakerSettings",
newName: "DreamMakerSettings_down");
migrationBuilder.CreateTable(
name: "DreamMakerSettings",
columns: table => new
{
Id = table.Column<long>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
ProjectName = table.Column<string>(maxLength: 10000, nullable: true),
ApiValidationPort = table.Column<ushort>(nullable: false),
ApiValidationSecurityLevel = table.Column<int>(nullable: false),
InstanceId = table.Column<long>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_DreamMakerSettings", x => x.Id);
table.ForeignKey(
name: "FK_DreamMakerSettings_Instances_InstanceId",
column: x => x.InstanceId,
principalTable: "Instances",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.Sql(
$"INSERT INTO DreamMakerSettings SELECT Id,ProjectName,ApiValidationPort,ApiValidationSecurityLevel,InstanceId FROM DreamMakerSettings_down");
migrationBuilder.DropTable(
name: "DreamMakerSettings_down");
}
}
}
@@ -1,3 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
@@ -15,6 +19,9 @@ using System.Threading.Tasks;
using Tgstation.Server.Api;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Client;
using Tgstation.Server.Host.Configuration;
using Tgstation.Server.Host.Database;
using Tgstation.Server.Host.Database.Migrations;
using Tgstation.Server.Host.Extensions;
using Tgstation.Server.Host.System;
using Tgstation.Server.Tests.Instance;
@@ -107,6 +114,54 @@ namespace Tgstation.Server.Tests
} while (true);
}
#if DEBUG
[TestMethod]
public async Task TestDownMigrations()
{
var connectionString = Environment.GetEnvironmentVariable("TGS4_TEST_CONNECTION_STRING");
if (String.IsNullOrEmpty(connectionString))
Assert.Inconclusive("No connection string configured in env var TGS4_TEST_CONNECTION_STRING!");
var databaseTypeString = Environment.GetEnvironmentVariable("TGS4_TEST_DATABASE_TYPE");
if (!Enum.TryParse<DatabaseType>(databaseTypeString, out var databaseType))
Assert.Inconclusive("No/invalid database type configured in env var TGS4_TEST_DATABASE_TYPE!");
string migrationName = null;
DbContext CreateContext()
{
switch (databaseType)
{
case DatabaseType.MySql:
case DatabaseType.MariaDB:
migrationName = nameof(MYInitialCreate);
return new MySqlDatabaseContext(Host.Database.Design.DesignTimeDbContextFactoryHelpers.CreateDatabaseContextOptions<MySqlDatabaseContext>(databaseType, connectionString));
case DatabaseType.PostgresSql:
migrationName = nameof(PGCreate);
return new PostgresSqlDatabaseContext(Host.Database.Design.DesignTimeDbContextFactoryHelpers.CreateDatabaseContextOptions<PostgresSqlDatabaseContext>(databaseType, connectionString));
case DatabaseType.SqlServer:
migrationName = nameof(MSInitialCreate);
return new SqlServerDatabaseContext(Host.Database.Design.DesignTimeDbContextFactoryHelpers.CreateDatabaseContextOptions<SqlServerDatabaseContext>(databaseType, connectionString));
case DatabaseType.Sqlite:
migrationName = nameof(SLRebuild);
return new SqliteDatabaseContext(Host.Database.Design.DesignTimeDbContextFactoryHelpers.CreateDatabaseContextOptions<SqliteDatabaseContext>(databaseType, connectionString));
}
return null;
}
Task Delete(DbContext context) => databaseType == DatabaseType.Sqlite ? Task.CompletedTask : context.Database.EnsureCreatedAsync();
using var context = CreateContext();
await Delete(context);
await context.Database.MigrateAsync(default);
var dbServiceProvider = ((IInfrastructure<IServiceProvider>)context.Database).Instance;
var migrator = dbServiceProvider.GetRequiredService<IMigrator>();
await migrator.MigrateAsync(migrationName, default);
await Delete(context);
}
#endif
[TestMethod]
public async Task TestServer()
{