begin;

update public.birth_time_rectification_question_handoffs
set state = 'consumed',
    claim_action_id = null,
    lease_expires_at = null,
    consumed_at = coalesce(consumed_at, pg_catalog.now()),
    updated_at = pg_catalog.now()
where state <> 'consumed';

update public.birth_time_rectification_v4_handoffs
set state = 'consumed',
    claim_action_id = null,
    lease_expires_at = null,
    consumed_at = coalesce(consumed_at, pg_catalog.now()),
    updated_at = pg_catalog.now()
where state <> 'consumed';

update public.birth_time_rectification_v4_jobs
set status = 'stale',
    worker_id = null,
    lease_expires_at = null,
    updated_at = pg_catalog.now()
where status in ('pending', 'processing');

update public.birth_time_rectification_v4_cases
set status = 'abandoned',
    current_question = null,
    updated_at = pg_catalog.now()
where status <> 'abandoned'
  and accepted_range_start is null;

update public.birth_time_rectification_cases
set status = 'abandoned',
    pending_consultation_question = null,
    updated_at = pg_catalog.now()
where status not in ('confirmed', 'completed', 'abandoned');

commit;
