feat(db): add SourceNode column + IX_AuditLog_Node_Occurred index to AuditLog

This commit is contained in:
Joseph Doherty
2026-05-23 16:18:57 -04:00
parent dfaa416ebe
commit 552d7832a3
9 changed files with 1899 additions and 11 deletions

View File

@@ -0,0 +1,59 @@
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)",
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");
}
}
}