using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace ScadaLink.ConfigurationDatabase.Migrations { /// /// Adds the ParentExecutionId correlation column to the centralized /// AuditLog table (#23). ParentExecutionId carries the /// ExecutionId of the execution that spawned this run, letting a /// spawned execution point back at its spawner — a sibling to the universal /// per-run ExecutionId. /// /// The change is purely additive: /// 1. ParentExecutionId uniqueidentifier 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 (no backfill). /// 2. IX_AuditLog_ParentExecution 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 AddAuditLogParentExecutionId : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: "ParentExecutionId", table: "AuditLog", type: "uniqueidentifier", 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_Execution (filtered, aligned). migrationBuilder.Sql(@" CREATE NONCLUSTERED INDEX IX_AuditLog_ParentExecution ON dbo.AuditLog (ParentExecutionId) WHERE ParentExecutionId IS NOT NULL ON ps_AuditLog_Month(OccurredAtUtc);"); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.Sql(@" IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_AuditLog_ParentExecution' AND object_id = OBJECT_ID('dbo.AuditLog')) DROP INDEX IX_AuditLog_ParentExecution ON dbo.AuditLog;"); migrationBuilder.DropColumn( name: "ParentExecutionId", table: "AuditLog"); } } }