Files
scadalink-design/src/ScadaLink.ConfigurationDatabase/Migrations/20260523201754_AddAuditLogSourceNode.cs
Joseph Doherty 8fb9eb0ce7 chore(db): align SourceNode unicode metadata + document partition-aligned index recipe
Tidies flagged by code review on the T6/T7/T8 migration bundle:

- Add `.IsUnicode(false)` to the three SourceNode EF property mappings to
  match every other ASCII varchar column on the same entities. Physical
  column was already `varchar(64)` because `HasColumnType` wins, but the EF
  model metadata flag was inconsistent.
- Add `unicode: false` to the three AddColumn<string> calls in the migrations
  + their Designer snapshots so the historical snapshots match the model.
- Update the model snapshot to carry IsUnicode(false) on each SourceNode entry.
- Document the SELECT-list invariant on SiteCallAuditRepository.QueryAsync:
  EF Core's FromSqlInterpolated requires every entity-tracked column in the
  result set, so future SiteCall columns must extend the list too.
- Amend plan Task 6 Step 2 to document the partition-aligned raw-SQL index
  recipe and the staging-table sync requirement.
2026-05-23 16:45:19 -04:00

61 lines
2.7 KiB
C#

using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ScadaLink.ConfigurationDatabase.Migrations
{
/// <summary>
/// Adds the <c>SourceNode</c> column to the centralized <c>AuditLog</c> table (#23,
/// SourceNode-stamping). <c>SourceNode</c> identifies the cluster node that produced the
/// audit row (e.g. <c>node-a</c>, <c>central-a</c>) — ASCII-only, so <c>varchar(64)</c>
/// not <c>nvarchar</c>. <c>NULL</c> is valid (reconciled rows from a retired node,
/// central direct-write rows pre-this-feature).
///
/// The change is purely additive:
/// 1. <c>SourceNode varchar(64) NULL</c> is added with no default, so the operation
/// is a metadata-only <c>ALTER TABLE … ADD</c> — it does NOT rewrite the
/// monthly-partitioned <c>AuditLog</c> table, and historical rows stay <c>NULL</c>.
/// 2. <c>IX_AuditLog_Node_Occurred (SourceNode, OccurredAtUtc)</c> is created via raw
/// SQL so it lands on the <c>ps_AuditLog_Month(OccurredAtUtc)</c> partition scheme,
/// matching every other <c>IX_AuditLog_*</c> index. Keeping it partition-aligned
/// preserves the partition-switch purge path (see
/// AuditLogRepository.SwitchOutPartitionAsync).
/// </summary>
public partial class AddAuditLogSourceNode : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "SourceNode",
table: "AuditLog",
type: "varchar(64)",
unicode: false,
maxLength: 64,
nullable: true);
// Raw SQL so the index is created on the partition scheme — EF's
// CreateIndex cannot express the ON ps_AuditLog_Month(OccurredAtUtc)
// clause. Mirrors IX_AuditLog_ParentExecution (aligned, unfiltered here:
// NULL SourceNode is a legitimate query target, e.g. "rows produced
// before stamping shipped" — no HasFilter on this index).
migrationBuilder.Sql(@"
CREATE NONCLUSTERED INDEX IX_AuditLog_Node_Occurred
ON dbo.AuditLog (SourceNode, OccurredAtUtc)
ON ps_AuditLog_Month(OccurredAtUtc);");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"
IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_AuditLog_Node_Occurred' AND object_id = OBJECT_ID('dbo.AuditLog'))
DROP INDEX IX_AuditLog_Node_Occurred ON dbo.AuditLog;");
migrationBuilder.DropColumn(
name: "SourceNode",
table: "AuditLog");
}
}
}