﻿BEGIN TRANSACTION;
IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260814235335_AddAuditLogAndNotificationCoveringIndexes'
)
BEGIN
    EXEC(N'CREATE INDEX [IX_Notifications_Delivered] ON [Notifications] ([DeliveredAt]) WHERE [Status] = ''Delivered''');
END;

IF NOT EXISTS (
    SELECT * FROM [__EFMigrationsHistory]
    WHERE [MigrationId] = N'20260814235335_AddAuditLogAndNotificationCoveringIndexes'
)
BEGIN

    DROP INDEX IX_AuditLog_OccurredAtUtc ON dbo.AuditLog;

    CREATE NONCLUSTERED INDEX IX_AuditLog_OccurredAtUtc
    ON dbo.AuditLog (OccurredAtUtc DESC)
    INCLUDE (Status)
    ON ps_AuditLog_Month(OccurredAtUtc);
END;

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

COMMIT;
GO

