-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-schema.sql
More file actions
50 lines (40 loc) · 1.71 KB
/
supabase-schema.sql
File metadata and controls
50 lines (40 loc) · 1.71 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
-- Enable Row Level Security
ALTER TABLE IF EXISTS public.documents DISABLE ROW LEVEL SECURITY;
DROP TABLE IF EXISTS public.documents;
-- Documents table for Y.js document state
CREATE TABLE public.documents (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
name TEXT NOT NULL,
owner_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
ydoc_state BYTEA, -- Y.js document state as binary
last_updated TIMESTAMPTZ DEFAULT NOW(),
created_at TIMESTAMPTZ DEFAULT NOW(),
-- Composite unique constraint for document name per user
CONSTRAINT unique_user_document UNIQUE (owner_id, name)
);
-- Enable Row Level Security
ALTER TABLE public.documents ENABLE ROW LEVEL SECURITY;
-- RLS Policies
CREATE POLICY "Users can view their own documents" ON public.documents
FOR SELECT USING (auth.uid() = owner_id);
CREATE POLICY "Users can insert their own documents" ON public.documents
FOR INSERT WITH CHECK (auth.uid() = owner_id);
CREATE POLICY "Users can update their own documents" ON public.documents
FOR UPDATE USING (auth.uid() = owner_id);
CREATE POLICY "Users can delete their own documents" ON public.documents
FOR DELETE USING (auth.uid() = owner_id);
-- Enable realtime for real-time collaboration notifications
ALTER PUBLICATION supabase_realtime ADD TABLE public.documents;
-- Function to automatically update last_updated timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.last_updated = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
-- Trigger to automatically update last_updated
CREATE TRIGGER update_documents_updated_at
BEFORE UPDATE ON public.documents
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();