deploy(prod): DBA SQL trimmed to the 104 indexes + a definition check vs QA (data half already applied); scadaproj#59

Claude-Session: https://claude.ai/code/session_01GT67AD5mc2BnaaAfY3hqqQ
This commit is contained in:
Joseph Doherty
2026-11-20 15:01:37 -05:00
parent b5eff81dbc
commit 9c77b6ce5c
3 changed files with 65 additions and 98 deletions
@@ -39,7 +39,7 @@ So a DBA built the tables (not from our script, which writes the history), but w
## Fix (for a DBA; `wwpdsbrw` cannot)
`sql/prod-complete-dba-schema.sql` (sha256 `17e0e7d6bb6707b140f07b94c1969563e49bd41ca541275595c36971bc037a65`), generated
`sql/prod-complete-dba-schema.sql` (first version, superseded 2026-11-20 evening, see below), generated
from QA's metadata: 104 guarded `CREATE [UNIQUE] NONCLUSTERED INDEX` (AuditLog ones on `ps_AuditLog_Month(OccurredAtUtc)`),
QA's 65 history rows, both `schema_version` v1 rows, and the 5 seed mappings. One transaction, idempotent, refuses a
different migration state. Compiled against PROD under `SET NOEXEC ON` (names bind; nothing ran; history still 0).
@@ -53,6 +53,19 @@ As `wwpdsbrw`, `sql/prod-app-dml-setup.sql` (no IDENTITY_INSERT: the identity wa
`LdapGroupMappings` 1–5 (SCADA-* seed, as QA). Then `sb-role-mappings-prod.sql`: ids 6–10 = the five `_PROD` groups (D-P6).
Re-run of the DML script: no change (idempotent). Nonclustered indexes still **0**.
**Open (scadaproj#59):** DBA runs `prod-complete-dba-schema.sql` (only the 104 `CREATE INDEX` act now). Until then the
**Open (scadaproj#59):** DBA runs `prod-complete-dba-schema.sql` (now indexes + definition check only). Until then the
central must not start: the migration check passes now, but uniqueness is unenforced and the zbauth/zbsecrets migrators'
`CREATE INDEX` is denied. Verify: index count 104, schema diff vs QA 0 index differences.
## Follow-up 2026-11-20 evening: the remaining SQL trimmed to the DBA part (owner: "do the migration inserts … and anything else you can, then update the sql")
- Migration inserts: already in (65 rows, 19:48Z). Re-checked: history 65, LdapGroupMappings 10, nonclustered indexes 0.
- Anything else the app login can do: nothing. The EF script's remaining DML (DataConnections, Templates, scripts, ApiMethods,
ApiKeys, AuditLog copy) only rewrites existing rows, and every table is empty, so it is a no-op on PROD.
- `sql/prod-complete-dba-schema.sql` rewritten (sha256 `3e05e1673fb4ceb1abaf4df8c62649be3406c4275fc233b05dde52ea9fbaf3ff`):
the DML is gone (done); it is now (1) the 104 guarded `CREATE INDEX` in one transaction, refusing unless EF history is at
2e0ce81e, and (2) a read-only check of the 24 default / computed / check definitions against QA's values (hidden from
`wwpdsbrw`; duplicate PROD `schema_version` checks tolerated). Expected output: `104`, `0` mismatches, `65`, `10`.
- Tested: the check section on QA gives 104 / 0 mismatches; with one expected value planted wrong it reports 2 rows and a
count of 2. The whole script compiles on PROD under `SET NOEXEC ON` (nothing ran).
- `sql/prod-app-dml-setup.sql` (what ran at 19:48Z) sha256 `282eb2e2289fa535490e2deb2c09e72335341fbefec5d9ac8a5a7cce6a815856`.
@@ -1,14 +1,16 @@
-- ScadaBridge PROD (wondersqlpd / ScadaBridge): complete the DBA-built 2e0ce81e schema.
-- Generated 2026-11-20 from QA wonder-sql-vq03.ScadaBridge (same build, all 65 migrations applied).
-- The DBA build has every table, PK, FK, default, computed column and the AuditLog partition scheme,
-- but none of the 104 nonclustered indexes, no __EFMigrationsHistory rows, no schema_version rows and no seed rows
-- (LdapGroupMappings ids 1-5). All tables were empty at 2026-11-20 checks.
-- Run as a principal with ALTER/CREATE INDEX rights (db_owner or db_ddladmin); the app login wwpdsbrw cannot.
-- Idempotent: every statement is guarded. Run with: sqlcmd -S wondersqlpd.zmr.zimmer.com -d ScadaBridge -C -b -I -i <this file>
-- ScadaBridge PROD (wondersqlpd / ScadaBridge): the DBA half of the schema completion (scadaproj#59).
-- State 2026-11-20 19:48Z: tables, PKs, FKs, defaults, computed columns and AuditLog partitioning were built by the DBA;
-- the data half is DONE as the app login wwpdsbrw (prod-app-dml-setup.sql + sb-role-mappings-prod.sql): 65 EF history rows,
-- zbauth/zbsecrets schema_version v1, LdapGroupMappings 1-10. What is left needs DDL / VIEW DEFINITION, which wwpdsbrw lacks:
-- 1. the 104 nonclustered indexes (definitions generated from QA wonder-sql-vq03.ScadaBridge, same build 2e0ce81e);
-- 2. a read-only check that the 24 default / computed / check definitions match QA (hidden from wwpdsbrw).
-- Run as db_owner or db_ddladmin: sqlcmd -S wondersqlpd.zmr.zimmer.com -d ScadaBridge -C -b -I -i prod-complete-dba-schema.sql
-- Idempotent (every CREATE is guarded). Tables are empty, so the index builds take seconds. Expected final output:
-- nonclustered_indexes = 104, definition_mismatches = 0 (any mismatch rows are listed; report them, do not fix by hand).
SET XACT_ABORT ON; SET NOCOUNT ON;
IF DB_NAME() <> N'ScadaBridge' BEGIN RAISERROR('wrong database',16,1); RETURN; END
IF EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory) AND NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20261115221558_RemoveAuditLogHashChain')
BEGIN RAISERROR('history present but not at 2e0ce81e - stop',16,1); RETURN; END
IF DB_NAME() <> N'ScadaBridge' THROW 50000, 'wrong database', 1;
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20261115221558_RemoveAuditLogHashChain')
THROW 50001, 'EF history is not at 2e0ce81e (20261115221558_RemoveAuditLogHashChain) - stop and ask', 1;
BEGIN TRAN;
-- 1. 104 nonclustered indexes
@@ -220,90 +222,42 @@ IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE object_id = OBJECT_ID(N'[zbauth].
CREATE NONCLUSTERED INDEX [ix_api_keys_revoked_utc] ON [zbauth].[api_keys] ([revoked_utc]);
IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE object_id = OBJECT_ID(N'[zbsecrets].[secret]') AND name = N'ix_secret_updated_utc')
CREATE NONCLUSTERED INDEX [ix_secret_updated_utc] ON [zbsecrets].[secret] ([updated_utc]);
-- 2. EF migration history: 65 rows = QA's
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260317065749_InitialSchema') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260317065749_InitialSchema', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260318025613_AddSiteNodeAddresses') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260318025613_AddSiteNodeAddresses', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260321164227_AddGrpcNodeAddresses') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260321164227_AddGrpcNodeAddresses', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260321210402_AddSiteIdToDataConnections') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260321210402_AddSiteIdToDataConnections', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260511144149_AddPrimaryBackupDataConnections') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260511144149_AddPrimaryBackupDataConnections', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260511144239_AddTemplateFolders') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260511144239_AddTemplateFolders', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260512121446_AddDerivedTemplateFields') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260512121446_AddDerivedTemplateFields', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260512122746_MigrateCompositionsToDerived') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260512122746_MigrateCompositionsToDerived', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260512211204_MigrateParametersToJsonSchema') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260512211204_MigrateParametersToJsonSchema', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260513055537_AddInstanceAlarmOverrides') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260513055537_AddInstanceAlarmOverrides', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260517000628_AddDerivedAlarmFields') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260517000628_AddDerivedAlarmFields', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260517010521_EncryptSecretColumns') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260517010521_EncryptSecretColumns', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260517020720_BoundGrpcNodeAddressLength') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260517020720_BoundGrpcNodeAddressLength', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260517073000_HashApiKeyValue') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260517073000_HashApiKeyValue', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260518214444_ContainedDerivedTemplateNames') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260518214444_ContainedDerivedTemplateNames', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260519050659_AddNotificationsTable') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260519050659_AddNotificationsTable', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260520142214_AddAuditLogTable') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260520142214_AddAuditLogTable', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260520180431_AddSiteCallsTable') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260520180431_AddSiteCallsTable', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260521184044_AddAuditLogExecutionId') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260521184044_AddAuditLogExecutionId', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260521193048_AddNotificationOriginExecutionId') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260521193048_AddNotificationOriginExecutionId', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260521210254_AddAuditLogParentExecutionId') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260521210254_AddAuditLogParentExecutionId', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260521220924_AddNotificationOriginParentExecutionId') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260521220924_AddNotificationOriginParentExecutionId', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260523201754_AddAuditLogSourceNode') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260523201754_AddAuditLogSourceNode', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260523201950_AddNotificationSourceNode') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260523201950_AddNotificationSourceNode', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260523202131_AddSiteCallSourceNode') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260523202131_AddSiteCallSourceNode', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260524075016_AddBundleImportIdToAuditLog') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260524075016_AddBundleImportIdToAuditLog', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260528155520_AddInstanceConnectionBindingOverride') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260528155520_AddInstanceConnectionBindingOverride', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260529195700_AddNativeAlarmSources') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260529195700_AddNativeAlarmSources', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260602092753_RetireInboundApiKeyStore') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260602092753_RetireInboundApiKeyStore', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260602113822_CanonicalizeRoles') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260602113822_CanonicalizeRoles', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260602174346_CollapseAuditLogToCanonical') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260602174346_CollapseAuditLogToCanonical', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260615171137_ResyncLdapGroupMappingSeed') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260615171137_ResyncLdapGroupMappingSeed', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260615183757_AddTemplateScriptExecutionTimeout') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260615183757_AddTemplateScriptExecutionTimeout', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260616192131_AddListAttributeElementType') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260616192131_AddListAttributeElementType', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260617234323_AddKpiSampleTable') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260617234323_AddKpiSampleTable', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260618060853_AddPendingSecuredWriteTable') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260618060853_AddPendingSecuredWriteTable', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260618152548_AddSharedSchema') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260618152548_AddSharedSchema', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260619135543_AddSmsNotifications') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260619135543_AddSmsNotifications', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260619191520_SmsFromNumberOptional') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260619191520_SmsFromNumberOptional', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260626161432_AddPendingDeployment') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260626161432_AddPendingDeployment', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260709105112_FixAuditPurgerRoleGrants') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260709105112_FixAuditPurgerRoleGrants', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260709110614_AddSiteCallsNonTerminalIndex') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260709110614_AddSiteCallsNonTerminalIndex', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260710075422_AddExternalSystemTimeoutSeconds') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260710075422_AddExternalSystemTimeoutSeconds', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260710080636_AddSmtpOAuth2AuthorityScope') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260710080636_AddSmtpOAuth2AuthorityScope', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260710153953_AddKpiRollupHourlyTable') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260710153953_AddKpiRollupHourlyTable', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260713142234_AddSiteCallsTerminalIndex') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260713142234_AddSiteCallsTerminalIndex', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260810100836_AddSmtpConfigurationTransport') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260810100836_AddSmtpConfigurationTransport', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260814235335_AddAuditLogAndNotificationCoveringIndexes') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260814235335_AddAuditLogAndNotificationCoveringIndexes', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260815004957_AlignAuditLogEventIdUniqueness') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260815004957_AlignAuditLogEventIdUniqueness', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260815154803_AddBundleUnlockAttemptTable') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260815154803_AddBundleUnlockAttemptTable', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260815165452_AddDeploymentRecordListSortIndex') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260815165452_AddDeploymentRecordListSortIndex', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260819183326_AddSiteCallsListSortIndex') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260819183326_AddSiteCallsListSortIndex', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260819201210_AddNotificationsListSortIndex') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260819201210_AddNotificationsListSortIndex', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260906034113_AddApiMethodTraceAllCalls') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260906034113_AddApiMethodTraceAllCalls', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260909203046_EnforceGlobalInstanceNamesAndProtectProtocolConfiguration') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260909203046_EnforceGlobalInstanceNamesAndProtectProtocolConfiguration', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260911135501_AddAlarmMesReportable') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260911135501_AddAlarmMesReportable', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20260914173147_AddSiteArtifactRevision') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20260914173147_AddSiteArtifactRevision', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20261022133907_ReviewRemediationIndexes') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20261022133907_ReviewRemediationIndexes', N'10.0.7');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20261109200211_AddNativeAlarmSourceMesReportable') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20261109200211_AddNativeAlarmSourceMesReportable', N'10.0.13');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20261109203753_PrefixSearchIndexes') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20261109203753_PrefixSearchIndexes', N'10.0.13');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20261109211317_AddAuditLogHashChain') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20261109211317_AddAuditLogHashChain', N'10.0.13');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20261110032839_AddConfigGraphRowVersions') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20261110032839_AddConfigGraphRowVersions', N'10.0.13');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20261110033732_FlipAuditLogClusteredKey') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20261110033732_FlipAuditLogClusteredKey', N'10.0.13');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20261110120000_AuditMaintenanceGrants') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20261110120000_AuditMaintenanceGrants', N'10.0.13');
IF NOT EXISTS (SELECT 1 FROM dbo.__EFMigrationsHistory WHERE MigrationId = N'20261115221558_RemoveAuditLogHashChain') INSERT dbo.__EFMigrationsHistory (MigrationId, ProductVersion) VALUES (N'20261115221558_RemoveAuditLogHashChain', N'10.0.13');
-- 3. shared-lib schema_version rows (v1, as QA); the app migrators MERGE these too
IF NOT EXISTS (SELECT 1 FROM zbauth.schema_version) INSERT zbauth.schema_version (id, version, applied_utc) VALUES (1, 1, SYSDATETIMEOFFSET());
IF NOT EXISTS (SELECT 1 FROM zbsecrets.schema_version) INSERT zbsecrets.schema_version (id, version, applied_utc) VALUES (1, 1, CONVERT(nvarchar(64), SYSDATETIMEOFFSET(), 127));
-- 4. migration seed rows (EF HasData + later UPDATEs, final state = QA); sb-role-mappings-prod.sql adds the _PROD rows after these
IF NOT EXISTS (SELECT 1 FROM dbo.LdapGroupMappings)
BEGIN
SET IDENTITY_INSERT dbo.LdapGroupMappings ON;
INSERT dbo.LdapGroupMappings (Id, LdapGroupName, Role) VALUES
(1, N'SCADA-Admins', N'Administrator'), (2, N'SCADA-Designers', N'Designer'), (3, N'SCADA-Deploy-All', N'Deployer'),
(4, N'SCADA-Deploy-SiteA', N'Deployer'), (5, N'SCADA-Viewers', N'Viewer');
SET IDENTITY_INSERT dbo.LdapGroupMappings OFF;
END
COMMIT;
-- 2. definition check vs QA (read-only)
DECLARE @qa TABLE (kind varchar(10) NOT NULL, obj nvarchar(300) NOT NULL, def nvarchar(max) NULL);
INSERT @qa (kind, obj, def) VALUES
('CHECK', N'zbauth.schema_version', N'([id]=(1))'),
('CHECK', N'zbsecrets.schema_version', N'([id]=(1))'),
('COMPUTED', N'dbo.AuditLog.ExecutionId', N'(CONVERT([uniqueidentifier],json_value([DetailsJson],''$.executionId'')))'),
('COMPUTED', N'dbo.AuditLog.IngestedAtUtc', N'(CONVERT([datetime2](7),switchoffset(CONVERT([datetimeoffset],json_value([DetailsJson],''$.ingestedAtUtc'')),(0))))'),
('COMPUTED', N'dbo.AuditLog.Kind', N'(json_value([DetailsJson],''$.kind''))'),
('COMPUTED', N'dbo.AuditLog.ParentExecutionId', N'(CONVERT([uniqueidentifier],json_value([DetailsJson],''$.parentExecutionId'')))'),
('COMPUTED', N'dbo.AuditLog.SourceSiteId', N'(json_value([DetailsJson],''$.sourceSiteId''))'),
('COMPUTED', N'dbo.AuditLog.Status', N'(json_value([DetailsJson],''$.status''))'),
('DEFAULT', N'dbo.ApiMethods.TraceAllCalls', N'(CONVERT([bit],(0)))'),
('DEFAULT', N'dbo.DataConnections.FailoverRetryCount', N'((3))'),
('DEFAULT', N'dbo.DataConnections.SiteId', N'((0))'),
('DEFAULT', N'dbo.ExternalSystemDefinitions.TimeoutSeconds', N'((0))'),
('DEFAULT', N'dbo.NotificationLists.Type', N'(N''Email'')'),
('DEFAULT', N'dbo.TemplateAlarms.IsInherited', N'(CONVERT([bit],(0)))'),
('DEFAULT', N'dbo.TemplateAlarms.IsMesReportable', N'(CONVERT([bit],(0)))'),
('DEFAULT', N'dbo.TemplateAlarms.LockedInDerived', N'(CONVERT([bit],(0)))'),
('DEFAULT', N'dbo.TemplateAttributes.IsInherited', N'(CONVERT([bit],(0)))'),
('DEFAULT', N'dbo.TemplateAttributes.LockedInDerived', N'(CONVERT([bit],(0)))'),
('DEFAULT', N'dbo.TemplateNativeAlarmSources.IsMesReportable', N'(CONVERT([bit],(0)))'),
('DEFAULT', N'dbo.Templates.IsDerived', N'(CONVERT([bit],(0)))'),
('DEFAULT', N'dbo.TemplateScripts.IsInherited', N'(CONVERT([bit],(0)))'),
('DEFAULT', N'dbo.TemplateScripts.LockedInDerived', N'(CONVERT([bit],(0)))'),
('DEFAULT', N'zbsecrets.secret.is_deleted', N'((0))'),
('DEFAULT', N'zbsecrets.secret.revision', N'((0))');
DECLARE @prod TABLE (kind varchar(10) NOT NULL, obj nvarchar(300) NOT NULL, def nvarchar(max) NULL);
INSERT @prod SELECT 'DEFAULT', s.name+'.'+t.name+'.'+c.name, dc.definition FROM sys.default_constraints dc JOIN sys.columns c ON c.object_id=dc.parent_object_id AND c.column_id=dc.parent_column_id JOIN sys.tables t ON t.object_id=c.object_id JOIN sys.schemas s ON s.schema_id=t.schema_id
UNION ALL SELECT 'COMPUTED', s.name+'.'+t.name+'.'+c.name, cc.definition FROM sys.computed_columns cc JOIN sys.columns c ON c.object_id=cc.object_id AND c.column_id=cc.column_id JOIN sys.tables t ON t.object_id=c.object_id JOIN sys.schemas s ON s.schema_id=t.schema_id
UNION ALL SELECT 'CHECK', OBJECT_SCHEMA_NAME(parent_object_id)+'.'+OBJECT_NAME(parent_object_id), definition FROM sys.check_constraints;
-- PROD has two identical CHECK (id=1) constraints on each schema_version table (QA has one); duplicates are harmless, so DISTINCT.
SELECT 'only QA' side, * FROM (SELECT kind, obj, def FROM @qa EXCEPT SELECT DISTINCT kind, obj, def FROM @prod) x
UNION ALL SELECT 'only PROD', * FROM (SELECT DISTINCT kind, obj, def FROM @prod EXCEPT SELECT kind, obj, def FROM @qa) y ORDER BY 2, 3, 1;
SELECT (SELECT COUNT(*) FROM sys.indexes i JOIN sys.tables t ON t.object_id=i.object_id WHERE i.type=2) AS nonclustered_indexes_expect_104,
(SELECT COUNT(*) FROM dbo.__EFMigrationsHistory) AS migrations_expect_65,
(SELECT MAX(MigrationId) FROM dbo.__EFMigrationsHistory) AS last_migration,
(SELECT COUNT(*) FROM dbo.LdapGroupMappings) AS ldap_mappings_expect_5;
((SELECT COUNT(*) FROM (SELECT kind, obj, def FROM @qa EXCEPT SELECT kind, obj, def FROM @prod) a) + (SELECT COUNT(*) FROM (SELECT kind, obj, def FROM @prod EXCEPT SELECT kind, obj, def FROM @qa) b)) AS definition_mismatches_expect_0,
(SELECT COUNT(*) FROM dbo.__EFMigrationsHistory) AS migrations_65, (SELECT COUNT(*) FROM dbo.LdapGroupMappings) AS ldap_mappings_10;