Spaces:
Sleeping
Sleeping
Shardul Chogale
security: secure Supabase functions, prevent Docker secrets leakage, and harden error handler (fixes #59)
b63bc10 Download supabase/comments_migration.sql from shard-c6/green-guard-api: direct link, hf CLI and curl.
- Browser
- Download file 2.24 kB
-
https://huggingface.co/spaces/shard-c6/green-guard-api/resolve/main/supabase/comments_migration.sql
- Command line
-
hf download hf://spaces/shard-c6/green-guard-api/supabase/comments_migration.sql
-
curl -L -o comments_migration.sql https://huggingface.co/spaces/shard-c6/green-guard-api/resolve/main/supabase/comments_migration.sql
2.24 kB
| -- ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| -- Green Guard v2 β Comments Feature Migration | |
| -- Run this in Supabase SQL Editor AFTER the main migration.sql | |
| -- ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| -- βββ COMMENTS TABLE βββββββββββββββββββββββββββββββββββββββββββββ | |
| CREATE TABLE IF NOT EXISTS comments ( | |
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | |
| post_id UUID NOT NULL REFERENCES posts(id) ON DELETE CASCADE, | |
| user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, | |
| content TEXT NOT NULL CHECK (char_length(content) > 0 AND char_length(content) <= 1000), | |
| created_at TIMESTAMPTZ DEFAULT now() | |
| ); | |
| CREATE INDEX IF NOT EXISTS idx_comments_post_id ON comments (post_id); | |
| CREATE INDEX IF NOT EXISTS idx_comments_user_id ON comments (user_id); | |
| -- Add comments_count to posts table | |
| ALTER TABLE posts ADD COLUMN IF NOT EXISTS comments_count INT DEFAULT 0; | |
| -- Counter RPCs | |
| CREATE OR REPLACE FUNCTION increment_comments(p_post_id UUID) RETURNS VOID AS $$ | |
| BEGIN | |
| UPDATE posts SET comments_count = comments_count + 1 WHERE id = p_post_id; | |
| END; | |
| $$ LANGUAGE plpgsql SECURITY DEFINER SET search_path = public; | |
| CREATE OR REPLACE FUNCTION decrement_comments(p_post_id UUID) RETURNS VOID AS $$ | |
| BEGIN | |
| UPDATE posts SET comments_count = GREATEST(0, comments_count - 1) WHERE id = p_post_id; | |
| END; | |
| $$ LANGUAGE plpgsql SECURITY DEFINER SET search_path = public; | |
| -- βββ COMMENTS RLS ββββββββββββββββββββββββββββββββββββββββββββββ | |
| ALTER TABLE comments ENABLE ROW LEVEL SECURITY; | |
| CREATE POLICY "comments_select_all" ON comments FOR SELECT USING (true); | |
| CREATE POLICY "comments_insert_auth" ON comments FOR INSERT WITH CHECK (auth.uid() = user_id); | |
| CREATE POLICY "comments_delete_own" ON comments FOR DELETE USING (auth.uid() = user_id); | |