ALTER TABLE IF EXISTS gateway_runner_policies
  ADD COLUMN IF NOT EXISTS single_source_policy jsonb NOT NULL DEFAULT '{"enabled":true}'::jsonb;

UPDATE gateway_runner_policies
SET single_source_policy = COALESCE(single_source_policy, '{}'::jsonb) || '{"enabled":true}'::jsonb,
    metadata = metadata || jsonb_build_object('singleSourcePolicy', '0050_runner_single_source_policy'),
    updated_at = now()
WHERE COALESCE(single_source_policy, '{}'::jsonb) = '{}'::jsonb
   OR NOT (COALESCE(single_source_policy, '{}'::jsonb) ? 'enabled');
