From 40b5f3904a91a4fe6c891bab5370d590472fb566 Mon Sep 17 00:00:00 2001 From: dmolinari Date: Tue, 21 Apr 2026 11:18:53 -0300 Subject: [PATCH] fix(bd): V023 idempotent guard for SYSTEM_VERSIONING OFF (PRC-001) Wrap 'ALTER TABLE ... SET SYSTEM_VERSIONING = OFF' in temporal_type=2 check. Without this guard, re-running V023 on a DB where a previous partial run already turned SYSTEM_VERSIONING OFF fails with: 'SYSTEM_VERSIONING is not turned ON for table...' Found while applying on SIGCM2 dev after a prior interrupted run. Now truly idempotent: safe to re-run in any state (fully applied, never applied, or partially applied with SYSTEM_VERSIONING already OFF). --- ...ctor_chargeable_char_config_to_product_type.sql | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/database/migrations/V023__refactor_chargeable_char_config_to_product_type.sql b/database/migrations/V023__refactor_chargeable_char_config_to_product_type.sql index 5724118..acef49f 100644 --- a/database/migrations/V023__refactor_chargeable_char_config_to_product_type.sql +++ b/database/migrations/V023__refactor_chargeable_char_config_to_product_type.sql @@ -38,9 +38,17 @@ IF EXISTS (SELECT 1 FROM sys.tables WHERE name = 'ChargeableCharConfig' AND sche BEGIN PRINT 'V023: MedioId column found — proceeding with refactor.'; - -- ─── 1. Turn OFF SYSTEM_VERSIONING ───────────────────────────────── - ALTER TABLE dbo.ChargeableCharConfig SET (SYSTEM_VERSIONING = OFF); - PRINT 'V023: SYSTEM_VERSIONING = OFF.'; + -- ─── 1. Turn OFF SYSTEM_VERSIONING (idempotent — skip if already OFF) ─── + IF EXISTS (SELECT 1 FROM sys.tables + WHERE name = 'ChargeableCharConfig' + AND schema_id = SCHEMA_ID('dbo') + AND temporal_type = 2) -- 2 = SYSTEM_VERSIONED_TEMPORAL_TABLE + BEGIN + ALTER TABLE dbo.ChargeableCharConfig SET (SYSTEM_VERSIONING = OFF); + PRINT 'V023: SYSTEM_VERSIONING = OFF.'; + END + ELSE + PRINT 'V023: SYSTEM_VERSIONING already OFF — skipping.'; -- ─── 2. Drop indexes that reference MedioId ──────────────────────── IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'UX_ChargeableCharConfig_Vigente'