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:
@@ -0,0 +1,39 @@
|
||||
-- AuditLogChannelOccurredIndex (2026-12-03, WP6 task 6.5 / review finding AUD-5). Run with
|
||||
-- QUOTED_IDENTIFIER ON (sqlcmd -I, or keep this header): AuditLog has persisted computed
|
||||
-- columns / filtered indexes and SQL Server raises error 1934 otherwise.
|
||||
-- Adds IX_AuditLog_Channel_Occurred (Category, OccurredAtUtc) for the per-channel retention
|
||||
-- purge. AuditLog is monthly-partitioned; the index is created ON ps_AuditLog_Month(OccurredAtUtc)
|
||||
-- so it stays partition-aligned and the partition-switch purge keeps working. Building it scans
|
||||
-- the whole AuditLog: on a large production table run in a quiet window, and on Enterprise
|
||||
-- edition add WITH (ONLINE = ON) to the CREATE INDEX statement.
|
||||
-- Ships with the binaries that bind the purge's @channel parameter as varchar(32); either order
|
||||
-- is safe (the index only helps; the old binaries' nvarchar binding simply cannot seek it).
|
||||
SET QUOTED_IDENTIFIER ON;
|
||||
SET ANSI_NULLS ON;
|
||||
GO
|
||||
|
||||
BEGIN TRANSACTION;
|
||||
IF NOT EXISTS (
|
||||
SELECT * FROM [__EFMigrationsHistory]
|
||||
WHERE [MigrationId] = N'20261203152558_AuditLogChannelOccurredIndex'
|
||||
)
|
||||
BEGIN
|
||||
|
||||
CREATE NONCLUSTERED INDEX IX_AuditLog_Channel_Occurred
|
||||
ON dbo.AuditLog (Category, OccurredAtUtc)
|
||||
ON ps_AuditLog_Month(OccurredAtUtc);
|
||||
END;
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT * FROM [__EFMigrationsHistory]
|
||||
WHERE [MigrationId] = N'20261203152558_AuditLogChannelOccurredIndex'
|
||||
)
|
||||
BEGIN
|
||||
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
|
||||
VALUES (N'20261203152558_AuditLogChannelOccurredIndex', N'10.0.13');
|
||||
END;
|
||||
|
||||
COMMIT;
|
||||
GO
|
||||
|
||||
|
||||
+2317
File diff suppressed because it is too large
Load Diff
+37
@@ -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 < @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;");
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -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");
|
||||
|
||||
+1
@@ -186,6 +186,7 @@ public class AuditLogEntityTypeConfigurationTests : IDisposable
|
||||
var expected = new[]
|
||||
{
|
||||
"IX_AuditLog_Actor_Occurred",
|
||||
"IX_AuditLog_Channel_Occurred", // AuditLogChannelOccurredIndex (AUD-5)
|
||||
"IX_AuditLog_Channel_Status_Occurred",
|
||||
"IX_AuditLog_CorrelationId",
|
||||
"IX_AuditLog_Execution",
|
||||
|
||||
+5
-1
@@ -70,7 +70,11 @@ public sealed class FlipAuditLogClusteredKeyMigrationTests : IClassFixture<MsSql
|
||||
Assert.True(indexes["PK_AuditLog"].Clustered);
|
||||
Assert.Equal("EventId, OccurredAtUtc", indexes["UX_AuditLog_EventId_Occurred"].Keys);
|
||||
Assert.True(indexes["UX_AuditLog_EventId_Occurred"].Unique);
|
||||
Assert.Equal(NonClusteredIndexes.Length + 2, indexes.Count);
|
||||
// + PK + UX + IX_AuditLog_Channel_Occurred (AuditLogChannelOccurredIndex, AUD-5 — added
|
||||
// after the flip, so the pre-flip round-trip shape below does not carry it).
|
||||
Assert.Equal(NonClusteredIndexes.Length + 3, indexes.Count);
|
||||
Assert.Equal("Category, OccurredAtUtc", indexes["IX_AuditLog_Channel_Occurred"].Keys);
|
||||
Assert.False(indexes["IX_AuditLog_Channel_Occurred"].Clustered);
|
||||
Assert.All(indexes.Values, i => Assert.Equal("ps_AuditLog_Month", i.DataSpace));
|
||||
AssertNonClusteredIndexes(indexes, withChainIndexes: false);
|
||||
Assert.Equal(ExpectedGrants, await ReadGrantsAsync());
|
||||
|
||||
Reference in New Issue
Block a user