using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ScadaLink.ConfigurationDatabase.Migrations
{
///
/// Adds the SourceNode column to the centralized AuditLog table (#23,
/// SourceNode-stamping). SourceNode identifies the cluster node that produced the
/// audit row (e.g. node-a, central-a) — ASCII-only, so varchar(64)
/// not nvarchar. NULL is valid (reconciled rows from a retired node,
/// central direct-write rows pre-this-feature).
///
/// The change is purely additive:
/// 1. SourceNode varchar(64) NULL is added with no default, so the operation
/// is a metadata-only ALTER TABLE … ADD — it does NOT rewrite the
/// monthly-partitioned AuditLog table, and historical rows stay NULL.
/// 2. IX_AuditLog_Node_Occurred (SourceNode, OccurredAtUtc) is created via raw
/// SQL so it lands on the ps_AuditLog_Month(OccurredAtUtc) partition scheme,
/// matching every other IX_AuditLog_* index. Keeping it partition-aligned
/// preserves the partition-switch purge path (see
/// AuditLogRepository.SwitchOutPartitionAsync).
///
public partial class AddAuditLogSourceNode : Migration
{
///
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn(
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);");
}
///
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");
}
}
}