-- 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'20260713142234_AddSiteCallsTerminalIndex'
)
BEGIN
    EXEC(N'CREATE INDEX [IX_SiteCalls_Terminal] ON [SiteCalls] ([TerminalAtUtc]) WHERE [TerminalAtUtc] IS NOT NULL');
END;

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

COMMIT;
GO

