Files
HaBit/supabase/migrations/20260524160000_deduplicate_habits.sql
2026-05-23 22:12:00 -06:00

14 lines
468 B
SQL

-- Remove duplicate active habits with the same name for a user.
delete from public.habits duplicate
using public.habits keeper
where duplicate.user_id = keeper.user_id
and duplicate.name = keeper.name
and duplicate.active = true
and keeper.active = true
and duplicate.id <> keeper.id
and duplicate.created_at > keeper.created_at;
create unique index if not exists habits_user_id_name_active_unique
on public.habits (user_id, name)
where active = true;