-
Notifications
You must be signed in to change notification settings - Fork 1
Supabase streak data sync #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
| /* | ||
| # User Streaks Table | ||
|
|
||
| 1. New Table | ||
| - `user_streaks` - Per-user streak stats (1 row per user) | ||
| - `id` (bigint, primary key) | ||
| - `user_id` (uuid, references auth.users) | ||
| - `current_streak` (integer) | ||
| - `max_streak` (integer) | ||
| - `last_entry_date` (date, nullable) | ||
| - `last_access_time` (timestamptz, nullable) | ||
| - `created_at` (timestamp) | ||
| - `updated_at` (timestamp) | ||
|
|
||
| 2. Security | ||
| - Enable RLS | ||
| - Policies so users can manage only their own streak record | ||
| */ | ||
|
|
||
| -- Create user_streaks table | ||
| CREATE TABLE IF NOT EXISTS user_streaks ( | ||
| id bigserial PRIMARY KEY, | ||
| user_id uuid NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE, | ||
| current_streak integer NOT NULL DEFAULT 0, | ||
| max_streak integer NOT NULL DEFAULT 0, | ||
| last_entry_date date, | ||
| last_access_time timestamptz, | ||
| created_at timestamptz NOT NULL DEFAULT now(), | ||
| updated_at timestamptz NOT NULL DEFAULT now() | ||
| ); | ||
|
|
||
| -- Backfill and enforce NOT NULL constraints for existing rows (if any) | ||
| UPDATE user_streaks | ||
| SET created_at = now() | ||
| WHERE created_at IS NULL; | ||
|
|
||
| UPDATE user_streaks | ||
| SET updated_at = now() | ||
| WHERE updated_at IS NULL; | ||
|
|
||
| ALTER TABLE user_streaks | ||
| ALTER COLUMN created_at SET NOT NULL, | ||
| ALTER COLUMN updated_at SET NOT NULL; | ||
|
|
||
| -- Enable Row Level Security | ||
| ALTER TABLE user_streaks ENABLE ROW LEVEL SECURITY; | ||
|
|
||
| -- Policies: users can manage their own streak record | ||
| CREATE POLICY "Users can read own streak" | ||
| ON user_streaks | ||
| FOR SELECT | ||
| TO authenticated | ||
| USING (auth.uid() = user_id); | ||
|
|
||
| CREATE POLICY "Users can insert own streak" | ||
| ON user_streaks | ||
| FOR INSERT | ||
| TO authenticated | ||
| WITH CHECK (auth.uid() = user_id); | ||
|
|
||
| CREATE POLICY "Users can update own streak" | ||
| ON user_streaks | ||
| FOR UPDATE | ||
| TO authenticated | ||
| USING (auth.uid() = user_id) | ||
| WITH CHECK (auth.uid() = user_id); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
fortune710 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| CREATE POLICY "Users can delete own streak" | ||
| ON user_streaks | ||
| FOR DELETE | ||
| TO authenticated | ||
| USING (auth.uid() = user_id); | ||
|
|
||
| -- Indexes for performance | ||
| CREATE UNIQUE INDEX IF NOT EXISTS idx_user_streaks_user_id | ||
| ON user_streaks(user_id); | ||
|
|
||
| -- updated_at trigger | ||
| CREATE TRIGGER update_user_streaks_updated_at | ||
| BEFORE UPDATE ON user_streaks | ||
| FOR EACH ROW | ||
| EXECUTE FUNCTION update_updated_at_column(); | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.