UPDATE test_definition
SET json = JSON_SET(
    json,
    '$.supportedServices',
    JSON_ARRAY('Snowflake', 'BigQuery', 'Athena', 'Redshift', 'Postgres', 'MySQL', 'Mssql', 'Oracle', 'Trino', 'SapHana')
)
WHERE name = 'tableDiff'
  AND (
    JSON_EXTRACT(json, '$.supportedServices') IS NULL
    OR JSON_LENGTH(JSON_EXTRACT(json, '$.supportedServices')) = 0
  );

-- Add timezone parameter to tableDataToBeFresh test definition
UPDATE test_definition
SET json = JSON_ARRAY_APPEND(
    json,
    '$.parameterDefinition',
    JSON_OBJECT(
        'name', 'timezone',
        'displayName', 'Timezone',
        'description', 'Timezone for the range calculation',
        'dataType', 'STRING',
        'required', false,
        'optionValues', JSON_ARRAY(
            'UTC',
            'Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Cairo',
            'Africa/Casablanca', 'Africa/Johannesburg', 'Africa/Lagos', 'Africa/Nairobi', 'Africa/Tunis',
            'America/Anchorage', 'America/Argentina/Buenos_Aires', 'America/Bogota', 'America/Caracas',
            'America/Chicago', 'America/Denver', 'America/Edmonton', 'America/Halifax', 'America/Havana',
            'America/Lima', 'America/Los_Angeles', 'America/Manaus', 'America/Mexico_City', 'America/New_York',
            'America/Panama', 'America/Phoenix', 'America/Puerto_Rico', 'America/Regina', 'America/Santiago',
            'America/Sao_Paulo', 'America/St_Johns', 'America/Toronto', 'America/Vancouver', 'America/Winnipeg',
            'Antarctica/McMurdo',
            'Asia/Almaty', 'Asia/Amman', 'Asia/Baghdad', 'Asia/Baku', 'Asia/Bangkok', 'Asia/Beirut',
            'Asia/Colombo', 'Asia/Damascus', 'Asia/Dhaka', 'Asia/Dubai', 'Asia/Ho_Chi_Minh', 'Asia/Hong_Kong',
            'Asia/Istanbul', 'Asia/Jakarta', 'Asia/Jerusalem', 'Asia/Kabul', 'Asia/Karachi', 'Asia/Kathmandu',
            'Asia/Kolkata', 'Asia/Kuala_Lumpur', 'Asia/Kuwait', 'Asia/Manila', 'Asia/Muscat', 'Asia/Nicosia',
            'Asia/Novosibirsk', 'Asia/Qatar', 'Asia/Riyadh', 'Asia/Seoul', 'Asia/Shanghai', 'Asia/Singapore',
            'Asia/Taipei', 'Asia/Tashkent', 'Asia/Tehran', 'Asia/Tokyo', 'Asia/Vladivostok', 'Asia/Yangon',
            'Asia/Yekaterinburg',
            'Atlantic/Azores', 'Atlantic/Canary', 'Atlantic/Cape_Verde', 'Atlantic/Reykjavik',
            'Australia/Adelaide', 'Australia/Brisbane', 'Australia/Darwin', 'Australia/Hobart',
            'Australia/Melbourne', 'Australia/Perth', 'Australia/Sydney',
            'Europe/Amsterdam', 'Europe/Athens', 'Europe/Belgrade', 'Europe/Berlin', 'Europe/Brussels',
            'Europe/Bucharest', 'Europe/Budapest', 'Europe/Copenhagen', 'Europe/Dublin', 'Europe/Helsinki',
            'Europe/Kyiv', 'Europe/Lisbon', 'Europe/London', 'Europe/Madrid', 'Europe/Malta', 'Europe/Minsk',
            'Europe/Moscow', 'Europe/Oslo', 'Europe/Paris', 'Europe/Prague', 'Europe/Riga', 'Europe/Rome',
            'Europe/Samara', 'Europe/Sofia', 'Europe/Stockholm', 'Europe/Tallinn', 'Europe/Vienna',
            'Europe/Vilnius', 'Europe/Warsaw', 'Europe/Zurich',
            'Indian/Maldives', 'Indian/Mauritius',
            'Pacific/Auckland', 'Pacific/Fiji', 'Pacific/Guam', 'Pacific/Honolulu', 'Pacific/Noumea',
            'Pacific/Pago_Pago', 'Pacific/Port_Moresby', 'Pacific/Tahiti', 'Pacific/Tongatapu'
        ),
        'defaultValue', 'UTC'
    )
)
WHERE name = 'tableDataToBeFresh'
  AND NOT JSON_CONTAINS(JSON_EXTRACT(json, '$.parameterDefinition[*].name'), '"timezone"');

-- Add validatorClass to all system test definitions (set to PascalCase name + 'Validator' if not present)
UPDATE test_definition
SET json = JSON_SET(json, '$.validatorClass', CONCAT(UPPER(SUBSTRING(JSON_UNQUOTE(JSON_EXTRACT(json, '$.name')), 1, 1)), SUBSTRING(JSON_UNQUOTE(JSON_EXTRACT(json, '$.name')), 2), 'Validator'))
WHERE JSON_EXTRACT(json, '$.validatorClass') IS NULL
  AND JSON_EXTRACT(json, '$.provider') = 'system';

-- Remove orphaned inputPorts and outputPorts fields from data_product_entity
-- These fields were added in 1.10.x but removed in 1.12.x (now relationship-based)
UPDATE data_product_entity
SET json = JSON_REMOVE(JSON_REMOVE(json, '$.inputPorts'), '$.outputPorts')
WHERE JSON_CONTAINS_PATH(json, 'one', '$.inputPorts')
   OR JSON_CONTAINS_PATH(json, 'one', '$.outputPorts');

-- Remove orphaned inputPorts and outputPorts fields from entity_extension (version history)
UPDATE entity_extension
SET json = JSON_REMOVE(JSON_REMOVE(json, '$.inputPorts'), '$.outputPorts')
WHERE jsonSchema = 'dataProduct'
  AND (JSON_CONTAINS_PATH(json, 'one', '$.inputPorts')
       OR JSON_CONTAINS_PATH(json, 'one', '$.outputPorts'));
