90de55535d
One migration, ScriptForeignKeysAndExternalIdUniqueness: - FK_VirtualTag_Script / FK_ScriptedAlarm_Script (Restrict) onto Script.ScriptId, which becomes NOT NULL and an alternate key (unique constraint UX_Script_LogicalId replaces the index). - UX_Equipment_ZTag / UX_Equipment_SAPID filtered UNIQUE indexes (blank = no identifier, the deploy gate's reading) replace the non-unique IX_ ones. - ExternalIdReservation + sp_ReleaseExternalIdReservation dropped (decision: retire — no writer since the generation model; uniqueness is gate + indexes). The /reservations page, query, ReservationKind, DraftSnapshot.ActiveReservations and the reservation pre-flight rule go too. - A read-only pre-check THROWs 51049 naming every offending row class (NULL ScriptId, dangling VirtualTag/ScriptedAlarm script, duplicated ZTag/SAPID, any reservation row) and changes nothing; Down restores the previous shape. Authoring mirrors so the constraints are never the first to say no: UnsTreeService refuses a missing script for VirtualTag/ScriptedAlarm create/update, and a ZTag/SAPID another equipment holds on create/update/import (owner named; import keeps the clean rows); values stored trimmed. ScriptEdit maps a concurrent-bind FK refusal to a message. Migration pre-check, Down round-trip, FK and index behaviour pinned against real SQL with positive controls.
87 lines
3.6 KiB
C#
87 lines
3.6 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;";
|
|
}
|
|
|
|
/// <summary>Opens a context on the scratch database.</summary>
|
|
/// <returns>A new context.</returns>
|
|
public OtOpcUaConfigDbContext NewContext() =>
|
|
new(new DbContextOptionsBuilder<OtOpcUaConfigDbContext>().UseSqlServer(ConnectionString).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);
|
|
}
|
|
}
|