﻿-- Run with QUOTED_IDENTIFIER ON. SiteCalls / Notifications / AuditLog all carry
-- filtered indexes (and AuditLog persisted computed columns), and SQL Server
-- refuses BOTH the filtered-index DDL below and any later DML on those tables
-- with error 1934 when the session has QUOTED_IDENTIFIER OFF -- which is exactly
-- what `sqlcmd` gives you by default (pass -I, or keep this header). The SETs sit
-- in their own batch so they are in force when the statements after the GO parse.
SET QUOTED_IDENTIFIER ON;
SET ANSI_NULLS ON;
GO

BEGIN TRANSACTION;
IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260815004957_AlignAuditLogEventIdUniqueness'
)
BEGIN

    IF EXISTS (SELECT 1 FROM sys.indexes
               WHERE name = 'UX_AuditLog_EventId' AND object_id = OBJECT_ID('dbo.AuditLog'))
        DROP INDEX UX_AuditLog_EventId ON dbo.AuditLog;
END;

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260815004957_AlignAuditLogEventIdUniqueness'
)
BEGIN
    INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
    VALUES (N'20260815004957_AlignAuditLogEventIdUniqueness', N'10.0.7');
END;

COMMIT;
GO

