do $$
begin
  if exists (
    select 1
    from pg_constraint
    where conname = 'badges_unlock_type_check'
  ) then
    alter table public.badges drop constraint badges_unlock_type_check;
  end if;

  alter table public.badges
    add constraint badges_unlock_type_check
    check (
      unlock_type is null
      or unlock_type in (
        'manual',
        'auto',
        'score',
        'check_in',
        'practice_count',
        'vocabulary_mastered',
        'mock_exam_score',
        'feedback_resolved',
        'activity_reward',
        'custom'
      )
    );
end $$;
