-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(habits): get_longest_streak db fn returns correct data (#152)
- Loading branch information
Showing
9 changed files
with
140 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 28 additions & 8 deletions
36
src/components/habit/habits-page/HabitLongestStreakCell.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
supabase/migrations/20250126165836_rewrite_get_longest_streak_function.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
DROP FUNCTION IF EXISTS get_longest_streak(integer); | ||
DROP TYPE IF EXISTS streak_info; | ||
|
||
CREATE TYPE streak_info AS ( | ||
streak_length integer, | ||
streak_start date, | ||
streak_end date | ||
); | ||
|
||
CREATE OR REPLACE FUNCTION get_longest_streak(p_habit_id integer) | ||
RETURNS streak_info AS $$ | ||
DECLARE | ||
result streak_info; | ||
BEGIN | ||
WITH daily_occurrences AS ( | ||
SELECT DISTINCT DATE(to_timestamp(timestamp/1000)) as occurrence_date | ||
FROM occurrences | ||
WHERE habit_id = p_habit_id | ||
ORDER BY occurrence_date | ||
), | ||
streaks AS ( | ||
SELECT | ||
occurrence_date, | ||
occurrence_date - (ROW_NUMBER() OVER (ORDER BY occurrence_date))::integer AS streak_group | ||
FROM daily_occurrences | ||
), | ||
streak_lengths AS ( | ||
SELECT | ||
streak_group, | ||
COUNT(*) as streak_length, | ||
MIN(occurrence_date) as streak_start, | ||
MAX(occurrence_date) as streak_end | ||
FROM streaks | ||
GROUP BY streak_group | ||
) | ||
SELECT | ||
streak_length, | ||
streak_start, | ||
streak_end | ||
INTO result | ||
FROM streak_lengths | ||
ORDER BY streak_length DESC | ||
LIMIT 1; | ||
|
||
IF result.streak_length IS NULL THEN | ||
result.streak_length := 0; | ||
result.streak_start := NULL; | ||
result.streak_end := NULL; | ||
END IF; | ||
|
||
RETURN result; | ||
END; | ||
$$ LANGUAGE plpgsql; |