added habit_streaks to habits table

This commit is contained in:
masongga
2026-05-23 22:00:56 -06:00
parent 82097f79ac
commit c86b04b3c8
2 changed files with 138 additions and 60 deletions

View File

@@ -0,0 +1,20 @@
alter table public.habits
add column if not exists streak int not null default 0,
add column if not exists last_completed_on date;
alter table public.habits
drop constraint if exists habits_streak_check;
alter table public.habits
add constraint habits_streak_check
check (streak >= 0);
update public.habits h
set last_completed_on = latest.completed_on
from (
select habit_id, max(completed_on) as completed_on
from public.habit_logs
group by habit_id
) as latest
where h.id = latest.habit_id
and h.last_completed_on is null;