-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_setup.sql
More file actions
67 lines (53 loc) · 2.36 KB
/
Copy pathsupabase_setup.sql
File metadata and controls
67 lines (53 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
-- Supabase tables for Chukdoo encrypted sync
-- Uses user_id column
-- Drop existing policies if they exist
DROP POLICY IF EXISTS "Users can view own todos" ON todos;
DROP POLICY IF EXISTS "Users can insert own todos" ON todos;
DROP POLICY IF EXISTS "Users can update own todos" ON todos;
DROP POLICY IF EXISTS "Users can delete own todos" ON todos;
DROP POLICY IF EXISTS "Users can view own projects" ON projects;
DROP POLICY IF EXISTS "Users can insert own projects" ON projects;
DROP POLICY IF EXISTS "Users can update own projects" ON projects;
DROP POLICY IF EXISTS "Users can delete own projects" ON projects;
-- Drop existing tables if they exist (WARNING: this deletes data!)
DROP TABLE IF EXISTS todos CASCADE;
DROP TABLE IF EXISTS projects CASCADE;
-- Todos table (encrypted)
CREATE TABLE todos (
id UUID PRIMARY KEY,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
encrypted_payload TEXT NOT NULL,
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Projects table (encrypted)
CREATE TABLE projects (
id UUID PRIMARY KEY,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
encrypted_payload TEXT NOT NULL,
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Enable Row Level Security
ALTER TABLE todos ENABLE ROW LEVEL SECURITY;
ALTER TABLE projects ENABLE ROW LEVEL SECURITY;
-- RLS Policies: Users can only access their own data
CREATE POLICY "Users can view own todos" ON todos
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own todos" ON todos
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own todos" ON todos
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own todos" ON todos
FOR DELETE USING (auth.uid() = user_id);
CREATE POLICY "Users can view own projects" ON projects
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own projects" ON projects
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own projects" ON projects
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own projects" ON projects
FOR DELETE USING (auth.uid() = user_id);
-- Indexes for performance
CREATE INDEX idx_todos_user_id ON todos(user_id);
CREATE INDEX idx_projects_user_id ON projects(user_id);
CREATE INDEX idx_todos_updated_at ON todos(updated_at);
CREATE INDEX idx_projects_updated_at ON projects(updated_at);