forked from jenshaak/CoachingAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-simplified-rls.sql
More file actions
71 lines (62 loc) · 2.16 KB
/
Copy pathsupabase-simplified-rls.sql
File metadata and controls
71 lines (62 loc) · 2.16 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
-- Create tables if they don't exist
CREATE TABLE IF NOT EXISTS "public"."threads" (
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"title" TEXT NOT NULL,
"user_id" TEXT, -- Change to TEXT to allow both UUID and string dev IDs
"tool_id" TEXT,
"metadata" JSONB DEFAULT '{}',
"created_at" TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
"updated_at" TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS "public"."messages" (
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"thread_id" UUID REFERENCES public.threads(id) ON DELETE CASCADE,
"role" TEXT NOT NULL,
"content" TEXT NOT NULL,
"timestamp" TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS "threads_user_id_idx" ON "public"."threads" ("user_id");
CREATE INDEX IF NOT EXISTS "messages_thread_id_idx" ON "public"."messages" ("thread_id");
-- Enable RLS but with permissive policies for development
ALTER TABLE "public"."threads" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "public"."messages" ENABLE ROW LEVEL SECURITY;
-- SIMPLIFIED POLICIES FOR DEVELOPMENT
-- Allow all operations on threads
CREATE POLICY "Allow all operations on threads"
ON "public"."threads"
FOR ALL
USING (true)
WITH CHECK (true);
-- Allow all operations on messages
CREATE POLICY "Allow all operations on messages"
ON "public"."messages"
FOR ALL
USING (true)
WITH CHECK (true);
-- Triggers to update the 'updated_at' timestamp on threads
CREATE OR REPLACE FUNCTION update_thread_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_thread_timestamp
BEFORE UPDATE ON "public"."threads"
FOR EACH ROW
EXECUTE PROCEDURE update_thread_updated_at();
-- Create trigger to update thread 'updated_at' when a new message is inserted
CREATE OR REPLACE FUNCTION update_thread_updated_at_on_message()
RETURNS TRIGGER AS $$
BEGIN
UPDATE "public"."threads"
SET updated_at = NOW()
WHERE id = NEW.thread_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_thread_on_message
AFTER INSERT ON "public"."messages"
FOR EACH ROW
EXECUTE PROCEDURE update_thread_updated_at_on_message();