5aa7d24db6
Item 4: Configuration.Tests SchemaComplianceFixture + MigrationScratchDatabase and AdminUI.Tests AdminUiSqlServerFixture get the same 300 s migration command / 60 s connect timeouts ControlPlane.Tests' SqlServerTestDatabase got in #1111. (Server.Tests / Host.Tests have no SQL Server test-DB harness.) Item 5: all three harnesses (and ControlPlane's) retry database creation ONCE under a fresh database name when the migration fails with a lost __EFMigrationsLock (SQL 1223, 'Cannot release the application lock ... not currently held'), dropping the half-migrated database first. Any other failure behaves as before. Recognition is unit-tested in ControlPlane.Tests.
89 lines
3.7 KiB
C#
89 lines
3.7 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests;
|
|
|
|
/// <summary>
|
|
/// A private, throw-away ConfigDb on the shared test SQL Server that a test migrates to a chosen migration
|
|
/// (not necessarily the latest), so a migration's pre-check can be exercised against data that the NEXT
|
|
/// migration must refuse. Named with <see cref="SchemaComplianceFixture.TestDatabasePrefix"/> so an orphan is
|
|
/// reclaimed by the fixture's reaper; dropped in <see cref="Dispose"/>.
|
|
/// </summary>
|
|
internal sealed class MigrationScratchDatabase : IDisposable
|
|
{
|
|
private const string DefaultServer = "10.100.0.35,14330";
|
|
private const string DefaultSaPassword = "OtOpcUaDev_2026!";
|
|
|
|
/// <summary>Gets the database name.</summary>
|
|
public string DatabaseName { get; }
|
|
|
|
/// <summary>Gets the connection string to the scratch database.</summary>
|
|
public string ConnectionString { get; }
|
|
|
|
/// <summary>Creates the (empty) database; nothing is migrated until <see cref="MigrateTo"/>.</summary>
|
|
public MigrationScratchDatabase()
|
|
{
|
|
var server = Environment.GetEnvironmentVariable("OTOPCUA_CONFIG_TEST_SERVER") ?? DefaultServer;
|
|
var saPassword = Environment.GetEnvironmentVariable("OTOPCUA_CONFIG_TEST_SA_PASSWORD") ?? DefaultSaPassword;
|
|
DatabaseName = $"{SchemaComplianceFixture.TestDatabasePrefix}{DateTime.UtcNow:yyyyMMddHHmmss}_{Guid.NewGuid():N}";
|
|
ConnectionString =
|
|
$"Server={server};Database={DatabaseName};User Id=sa;Password={saPassword};TrustServerCertificate=True;Encrypt=False;" +
|
|
$"Connect Timeout={SchemaComplianceFixture.ConnectTimeoutSeconds};";
|
|
}
|
|
|
|
/// <summary>Opens a context on the scratch database.</summary>
|
|
/// <returns>A new context.</returns>
|
|
public OtOpcUaConfigDbContext NewContext() =>
|
|
new(new DbContextOptionsBuilder<OtOpcUaConfigDbContext>()
|
|
.UseSqlServer(ConnectionString, o => o.CommandTimeout(SchemaComplianceFixture.MigrationCommandTimeoutSeconds)).Options);
|
|
|
|
/// <summary>Migrates (up) to <paramref name="targetMigration"/>, or to the latest when null.</summary>
|
|
/// <param name="targetMigration">The migration id or name to stop at.</param>
|
|
public void MigrateTo(string? targetMigration = null)
|
|
{
|
|
using var db = NewContext();
|
|
db.GetService<IMigrator>().Migrate(targetMigration);
|
|
}
|
|
|
|
/// <summary>The most recently applied migration id.</summary>
|
|
/// <returns>The id, or null when none is applied.</returns>
|
|
public string? LastAppliedMigration()
|
|
{
|
|
using var db = NewContext();
|
|
return db.Database.GetAppliedMigrations().LastOrDefault();
|
|
}
|
|
|
|
/// <summary>Runs one non-query statement.</summary>
|
|
/// <param name="sql">The T-SQL.</param>
|
|
public void Execute(string sql)
|
|
{
|
|
using var conn = new SqlConnection(ConnectionString);
|
|
conn.Open();
|
|
using var cmd = conn.CreateCommand();
|
|
cmd.CommandText = sql;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
/// <summary>Runs one scalar query.</summary>
|
|
/// <param name="sql">The T-SQL.</param>
|
|
/// <returns>The scalar.</returns>
|
|
public object? Scalar(string sql)
|
|
{
|
|
using var conn = new SqlConnection(ConnectionString);
|
|
conn.Open();
|
|
using var cmd = conn.CreateCommand();
|
|
cmd.CommandText = sql;
|
|
return cmd.ExecuteScalar();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Dispose()
|
|
{
|
|
SqlConnection.ClearAllPools();
|
|
var master = new SqlConnectionStringBuilder(ConnectionString) { InitialCatalog = "master" }.ConnectionString;
|
|
SqlTestDatabaseReaper.TryDropDatabase(master, DatabaseName);
|
|
}
|
|
}
|