-- ===================================================================
-- MARS-1049: Update Existing Configurations with Encoding Values
-- ===================================================================
-- Purpose: Set default encoding values for existing A_SOURCE_FILE_CONFIG records
-- Author: Grzegorz Michalski
-- Date: 2025-11-25
-- Version: 1.0.0
--
-- ENCODING STRATEGY:
-- - UTF8: Default for all configurations (modern Unicode standard)
-- - WE8MSWIN1252: Special case for CSDB templates (Windows-1252 compatibility)
-- ===================================================================

PROMPT ===============================================
PROMPT MARS-1049: Updating existing configurations with encoding values
PROMPT ===============================================

-- Check current state before updates
SELECT COUNT(*) AS TOTAL_CONFIGURATIONS,
       COUNT(CASE WHEN ENCODING IS NULL THEN 1 END) AS NULL_ENCODING_COUNT,
       COUNT(CASE WHEN ENCODING IS NOT NULL THEN 1 END) AS WITH_ENCODING_COUNT
FROM CT_MRDS.A_SOURCE_FILE_CONFIG;

-- Update all configurations to UTF8 (default)
UPDATE /*+ NOPARALLEL */ CT_MRDS.A_SOURCE_FILE_CONFIG 
SET ENCODING = 'UTF8'
WHERE ENCODING IS NULL;

PROMPT Updated configurations with UTF8 (default)

-- Commit first batch to avoid deadlock
COMMIT;

-- PROMPT Committed UTF8 updates - now processing CSDB configurations...

-- -- Update CSDB-specific configurations to Windows-1252
-- UPDATE /*+ NOPARALLEL */ CT_MRDS.A_SOURCE_FILE_CONFIG 
-- SET ENCODING = 'WE8MSWIN1252'
-- WHERE TEMPLATE_TABLE_NAME LIKE '%CSDB%';

-- PROMPT Updated CSDB configurations with WE8MSWIN1252

-- -- Commit changes
-- COMMIT;

-- Verify final state
PROMPT
PROMPT Final encoding distribution:
SELECT ENCODING, COUNT(*) AS COUNT
FROM CT_MRDS.A_SOURCE_FILE_CONFIG
GROUP BY ENCODING
ORDER BY ENCODING;

PROMPT ===============================================
PROMPT Encoding configuration update completed
PROMPT ===============================================