-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_schema.sql
More file actions
71 lines (57 loc) · 2.36 KB
/
Copy pathsupabase_schema.sql
File metadata and controls
71 lines (57 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
-- ============================================
-- SUPABASE DATABASE SCHEMA FOR COMMENTS
-- ============================================
-- Copy and paste this entire script into Supabase SQL Editor
-- Then click "Run" button
-- 1. Create comments table
CREATE TABLE IF NOT EXISTS comments (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
name TEXT NOT NULL,
comment TEXT NOT NULL,
timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW(),
likes INTEGER DEFAULT 0,
is_admin BOOLEAN DEFAULT FALSE,
is_pinned BOOLEAN DEFAULT FALSE,
pinned_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- 2. Create replies table
CREATE TABLE IF NOT EXISTS replies (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
comment_id UUID NOT NULL REFERENCES comments(id) ON DELETE CASCADE,
name TEXT NOT NULL,
comment TEXT NOT NULL,
is_admin BOOLEAN DEFAULT FALSE,
timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW(),
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- 3. Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_comments_created_at ON comments(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_comments_pinned ON comments(is_pinned DESC, pinned_at DESC);
CREATE INDEX IF NOT EXISTS idx_replies_comment_id ON replies(comment_id);
CREATE INDEX IF NOT EXISTS idx_replies_created_at ON replies(created_at ASC);
-- 4. Enable Row Level Security (RLS)
ALTER TABLE comments ENABLE ROW LEVEL SECURITY;
ALTER TABLE replies ENABLE ROW LEVEL SECURITY;
-- 5. Create policies to allow public read/write access
-- Comments policies
CREATE POLICY "Enable read access for all users" ON comments
FOR SELECT USING (true);
CREATE POLICY "Enable insert access for all users" ON comments
FOR INSERT WITH CHECK (true);
CREATE POLICY "Enable update access for all users" ON comments
FOR UPDATE USING (true);
CREATE POLICY "Enable delete access for all users" ON comments
FOR DELETE USING (true);
-- Replies policies
CREATE POLICY "Enable read access for all users" ON replies
FOR SELECT USING (true);
CREATE POLICY "Enable insert access for all users" ON replies
FOR INSERT WITH CHECK (true);
CREATE POLICY "Enable update access for all users" ON replies
FOR UPDATE USING (true);
CREATE POLICY "Enable delete access for all users" ON replies
FOR DELETE USING (true);
-- ============================================
-- DONE! Your tables are ready to use
-- ============================================