BEGIN TRANSACTION; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109200211_AddNativeAlarmSourceMesReportable' ) BEGIN ALTER TABLE [TemplateNativeAlarmSources] ADD [IsMesReportable] bit NOT NULL DEFAULT CAST(0 AS bit); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109200211_AddNativeAlarmSourceMesReportable' ) BEGIN ALTER TABLE [InstanceNativeAlarmSourceOverrides] ADD [IsMesReportableOverride] bit NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109200211_AddNativeAlarmSourceMesReportable' ) BEGIN INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'20261109200211_AddNativeAlarmSourceMesReportable', N'10.0.13'); END; COMMIT; GO BEGIN TRANSACTION; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109203753_PrefixSearchIndexes' ) BEGIN CREATE INDEX [IX_DeploymentRecords_DeployedBy] ON [DeploymentRecords] ([DeployedBy]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109203753_PrefixSearchIndexes' ) BEGIN CREATE INDEX [IX_DeploymentRecords_RevisionHash] ON [DeploymentRecords] ([RevisionHash]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109203753_PrefixSearchIndexes' ) BEGIN CREATE INDEX [IX_AuditLogEntries_EntityName] ON [AuditLogEntries] ([EntityName]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109203753_PrefixSearchIndexes' ) BEGIN INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'20261109203753_PrefixSearchIndexes', N'10.0.13'); END; COMMIT; GO BEGIN TRANSACTION; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109211317_AddAuditLogHashChain' ) BEGIN ALTER TABLE [AuditLog] ADD [ChainSeq] bigint NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109211317_AddAuditLogHashChain' ) BEGIN ALTER TABLE [AuditLog] ADD [RowHash] binary(32) NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109211317_AddAuditLogHashChain' ) BEGIN CREATE TABLE [AuditLogChainHead] ( [ChainMonth] date NOT NULL, [LastSeq] bigint NOT NULL, [LastHash] binary(32) NOT NULL, [PurgedThroughSeq] bigint NULL, [PurgedThroughHash] binary(32) NULL, [UpdatedAtUtc] datetime2 NOT NULL, CONSTRAINT [PK_AuditLogChainHead] PRIMARY KEY ([ChainMonth]) ); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109211317_AddAuditLogHashChain' ) BEGIN CREATE TABLE [AuditLogChainTombstone] ( [ChainMonth] date NOT NULL, [ChainSeq] bigint NOT NULL, [RowHash] binary(32) NOT NULL, [EventId] uniqueidentifier NOT NULL, [PurgedAtUtc] datetime2 NOT NULL, CONSTRAINT [PK_AuditLogChainTombstone] PRIMARY KEY ([ChainMonth], [ChainSeq]) ); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109211317_AddAuditLogHashChain' ) BEGIN EXEC(N'CREATE NONCLUSTERED INDEX IX_AuditLog_Unchained ON dbo.AuditLog (OccurredAtUtc, EventId) WHERE ChainSeq IS NULL ON ps_AuditLog_Month(OccurredAtUtc);'); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109211317_AddAuditLogHashChain' ) BEGIN EXEC(N'CREATE NONCLUSTERED INDEX IX_AuditLog_ChainSeq ON dbo.AuditLog (ChainSeq) WHERE ChainSeq IS NOT NULL ON ps_AuditLog_Month(OccurredAtUtc);'); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109211317_AddAuditLogHashChain' ) BEGIN IF DATABASE_PRINCIPAL_ID('scadabridge_audit_chainer') IS NULL EXEC sp_executesql N'CREATE ROLE scadabridge_audit_chainer'; -- The chaining job reads unchained rows and writes ONLY the two link columns. GRANT SELECT ON dbo.AuditLog TO scadabridge_audit_chainer; GRANT UPDATE (ChainSeq, RowHash) ON dbo.AuditLog TO scadabridge_audit_chainer; GRANT SELECT, INSERT, UPDATE ON dbo.AuditLogChainHead TO scadabridge_audit_chainer; GRANT SELECT ON dbo.AuditLogChainTombstone TO scadabridge_audit_chainer; IF DATABASE_PRINCIPAL_ID('scadabridge_audit_purger') IS NULL EXEC sp_executesql N'CREATE ROLE scadabridge_audit_purger'; -- The per-channel purge writes a tombstone per chained row it removes; the partition -- switch moves the month's head to its checkpoint anchor and drops its tombstones. GRANT SELECT, UPDATE ON dbo.AuditLogChainHead TO scadabridge_audit_purger; GRANT SELECT, INSERT, DELETE ON dbo.AuditLogChainTombstone TO scadabridge_audit_purger; IF DATABASE_PRINCIPAL_ID('scadabridge_audit_writer') IS NULL EXEC sp_executesql N'CREATE ROLE scadabridge_audit_writer'; -- The ingest principal must never be able to forge chain state. DENY INSERT, UPDATE, DELETE ON dbo.AuditLogChainHead TO scadabridge_audit_writer; DENY INSERT, UPDATE, DELETE ON dbo.AuditLogChainTombstone TO scadabridge_audit_writer; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261109211317_AddAuditLogHashChain' ) BEGIN INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'20261109211317_AddAuditLogHashChain', N'10.0.13'); END; COMMIT; GO BEGIN TRANSACTION; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [TemplateScripts] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [Templates] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [TemplateNativeAlarmSources] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [TemplateCompositions] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [TemplateAttributes] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [TemplateAlarms] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [Sites] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [SharedScripts] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [Instances] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [InstanceNativeAlarmSourceOverrides] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [InstanceConnectionBindings] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [InstanceAttributeOverrides] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [InstanceAlarmOverrides] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [ExternalSystemMethods] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [ExternalSystemDefinitions] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [DeployedConfigSnapshots] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [DataConnections] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN ALTER TABLE [DatabaseConnectionDefinitions] ADD [RowVersion] rowversion NOT NULL; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_TemplateScripts_RowVersion] ON [TemplateScripts] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_Templates_RowVersion] ON [Templates] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_TemplateNativeAlarmSources_RowVersion] ON [TemplateNativeAlarmSources] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_TemplateCompositions_RowVersion] ON [TemplateCompositions] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_TemplateAttributes_RowVersion] ON [TemplateAttributes] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_TemplateAlarms_RowVersion] ON [TemplateAlarms] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_Sites_RowVersion] ON [Sites] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_SharedScripts_RowVersion] ON [SharedScripts] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_Instances_RowVersion] ON [Instances] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_InstanceNativeAlarmSourceOverrides_RowVersion] ON [InstanceNativeAlarmSourceOverrides] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_InstanceConnectionBindings_RowVersion] ON [InstanceConnectionBindings] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_InstanceAttributeOverrides_RowVersion] ON [InstanceAttributeOverrides] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_InstanceAlarmOverrides_RowVersion] ON [InstanceAlarmOverrides] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_ExternalSystemMethods_RowVersion] ON [ExternalSystemMethods] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_ExternalSystemDefinitions_RowVersion] ON [ExternalSystemDefinitions] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_DeployedConfigSnapshots_RowVersion] ON [DeployedConfigSnapshots] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_DataConnections_RowVersion] ON [DataConnections] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN CREATE INDEX [IX_DatabaseConnectionDefinitions_RowVersion] ON [DatabaseConnectionDefinitions] ([RowVersion]); END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110032839_AddConfigGraphRowVersions' ) BEGIN INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'20261110032839_AddConfigGraphRowVersions', N'10.0.13'); END; COMMIT; GO BEGIN TRANSACTION; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110033732_FlipAuditLogClusteredKey' ) BEGIN IF (SELECT c.name FROM sys.index_columns ic JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id WHERE ic.object_id = OBJECT_ID('dbo.AuditLog') AND ic.index_id = 1 AND ic.key_ordinal = 1) = N'EventId' BEGIN DECLARE @dropSql nvarchar(max) = N''; SELECT @dropSql = @dropSql + N'DROP INDEX ' + QUOTENAME(i.name) + N' ON dbo.AuditLog;' + CHAR(10) FROM sys.indexes i WHERE i.object_id = OBJECT_ID('dbo.AuditLog') AND i.type = 2 AND i.name IN ( N'IX_AuditLog_OccurredAtUtc', N'IX_AuditLog_Site_Occurred', N'IX_AuditLog_CorrelationId', N'IX_AuditLog_Channel_Status_Occurred', N'IX_AuditLog_Target_Occurred', N'IX_AuditLog_Execution', N'IX_AuditLog_ParentExecution', N'IX_AuditLog_Node_Occurred', N'IX_AuditLog_Kind_Occurred', N'IX_AuditLog_Actor_Occurred', N'IX_AuditLog_Unchained', N'IX_AuditLog_ChainSeq', N'UX_AuditLog_EventId_Occurred'); EXEC sp_executesql @dropSql; ALTER TABLE dbo.AuditLog DROP CONSTRAINT PK_AuditLog; ALTER TABLE dbo.AuditLog ADD CONSTRAINT PK_AuditLog PRIMARY KEY CLUSTERED (OccurredAtUtc, EventId) ON ps_AuditLog_Month(OccurredAtUtc); CREATE UNIQUE NONCLUSTERED INDEX UX_AuditLog_EventId_Occurred ON dbo.AuditLog (EventId, OccurredAtUtc) ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_OccurredAtUtc ON dbo.AuditLog (OccurredAtUtc DESC) INCLUDE (Status) ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_Site_Occurred ON dbo.AuditLog (SourceSiteId ASC, OccurredAtUtc DESC) ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_CorrelationId ON dbo.AuditLog (CorrelationId) WHERE CorrelationId IS NOT NULL ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_Channel_Status_Occurred ON dbo.AuditLog (Category ASC, Status ASC, OccurredAtUtc DESC) ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_Target_Occurred ON dbo.AuditLog (Target ASC, OccurredAtUtc DESC) WHERE Target IS NOT NULL ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_Execution ON dbo.AuditLog (ExecutionId) ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_ParentExecution ON dbo.AuditLog (ParentExecutionId) ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_Node_Occurred ON dbo.AuditLog (SourceNode, OccurredAtUtc) ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_Kind_Occurred ON dbo.AuditLog (Kind ASC, OccurredAtUtc DESC) ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_Actor_Occurred ON dbo.AuditLog (Actor ASC, OccurredAtUtc DESC) ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_Unchained ON dbo.AuditLog (OccurredAtUtc, EventId) WHERE ChainSeq IS NULL ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_ChainSeq ON dbo.AuditLog (ChainSeq) WHERE ChainSeq IS NOT NULL ON ps_AuditLog_Month(OccurredAtUtc); END END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110033732_FlipAuditLogClusteredKey' ) BEGIN INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'20261110033732_FlipAuditLogClusteredKey', N'10.0.13'); END; COMMIT; GO BEGIN TRANSACTION; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110120000_AuditMaintenanceGrants' ) BEGIN IF DATABASE_PRINCIPAL_ID('scadabridge_audit_purger') IS NULL EXEC sp_executesql N'CREATE ROLE scadabridge_audit_purger'; GRANT ALTER ANY DATASPACE TO scadabridge_audit_purger; GRANT UPDATE (SourceNode) ON dbo.AuditLog TO scadabridge_audit_purger; END; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20261110120000_AuditMaintenanceGrants' ) BEGIN INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'20261110120000_AuditMaintenanceGrants', N'10.0.13'); END; COMMIT; GO