feat(config-db): migration AuditLogChannelOccurredIndex — partition-aligned IX_AuditLog_Channel_Occurred (AUD-5, WP6 task 6.5)

Adds IX_AuditLog_Channel_Occurred (Category, OccurredAtUtc) ON
ps_AuditLog_Month(OccurredAtUtc) via raw SQL (EF CreateIndex cannot express
the partition scheme), like ReviewRemediationIndexes. Idempotent QA/DEV
script: docs/plans/sql/AuditLogChannelOccurredIndex.sql (sqlcmd -I). Kept in
its own commit so the integrator can regenerate it on merge with the other
WPs' migrations.

Index-name pins updated: AuditLogEntityTypeConfigurationTests and
FlipAuditLogClusteredKeyMigrationTests (head shape asserts the new index is
partition-aligned on ps_AuditLog_Month; the pre-flip round-trip shape does
not carry it).
This commit is contained in:
Joseph Doherty
2026-12-03 10:59:03 -05:00
parent b3fba593a3
commit 9a574746dc
6 changed files with 2402 additions and 1 deletions
@@ -0,0 +1,37 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
{
/// <summary>
/// AUD-5 (WP6 task 6.5): <c>IX_AuditLog_Channel_Occurred (Category, OccurredAtUtc)</c> for the
/// per-channel retention purge (<c>DELETE TOP (@batch) ... WHERE Category = @channel AND
/// OccurredAtUtc &lt; @threshold</c>), whose channel and age predicates were not adjacent in
/// any index.
/// </summary>
public partial class AuditLogChannelOccurredIndex : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
// AuditLog is monthly-partitioned (ps_AuditLog_Month) with a partition-switch purge,
// so the index MUST be partition-aligned. EF's CreateIndex cannot express the
// ON ps_AuditLog_Month(OccurredAtUtc) clause -> raw SQL, exactly as
// ReviewRemediationIndexes / AddAuditLogSourceNode. On a large production AuditLog
// consider WITH (ONLINE = ON) (Enterprise edition) in the generated script.
migrationBuilder.Sql(@"
CREATE NONCLUSTERED INDEX IX_AuditLog_Channel_Occurred
ON dbo.AuditLog (Category, 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_Channel_Occurred' AND object_id = OBJECT_ID('dbo.AuditLog'))
DROP INDEX IX_AuditLog_Channel_Occurred ON dbo.AuditLog;");
}
}
}
@@ -2028,6 +2028,9 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
.IsDescending(false, true)
.HasDatabaseName("IX_AuditLog_Actor_Occurred");
b.HasIndex("Channel", "OccurredAtUtc")
.HasDatabaseName("IX_AuditLog_Channel_Occurred");
b.HasIndex("EventId", "OccurredAtUtc")
.IsUnique()
.HasDatabaseName("UX_AuditLog_EventId_Occurred");