-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsupabase_setup.sql
More file actions
78 lines (66 loc) · 2.35 KB
/
Copy pathsupabase_setup.sql
File metadata and controls
78 lines (66 loc) · 2.35 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
72
73
74
75
76
77
78
-- Create tables for chat storage
-- Conversations table
create table if not exists conversations (
id uuid primary key,
title text not null,
"lastMessage" text,
timestamp timestamptz not null default now(),
user_id uuid not null references auth.users(id) on delete cascade
);
-- Messages table
create table if not exists messages (
id uuid primary key,
content text not null,
sender text not null check (sender in ('user', 'assistant')),
timestamp timestamptz not null default now(),
conversation_id uuid not null references conversations(id) on delete cascade,
attachments jsonb
);
-- Enable Row Level Security (RLS)
alter table conversations enable row level security;
alter table messages enable row level security;
-- Create policies for conversations (users can only access their own conversations)
create policy "Users can view their own conversations"
on conversations for select
using (auth.uid() = user_id);
create policy "Users can insert their own conversations"
on conversations for insert
with check (auth.uid() = user_id);
create policy "Users can update their own conversations"
on conversations for update
using (auth.uid() = user_id);
create policy "Users can delete their own conversations"
on conversations for delete
using (auth.uid() = user_id);
-- Create policies for messages (users can only access messages in their own conversations)
create policy "Users can view messages in their conversations"
on messages for select
using (
conversation_id in (
select id from conversations where user_id = auth.uid()
)
);
create policy "Users can insert messages in their conversations"
on messages for insert
with check (
conversation_id in (
select id from conversations where user_id = auth.uid()
)
);
create policy "Users can update messages in their conversations"
on messages for update
using (
conversation_id in (
select id from conversations where user_id = auth.uid()
)
);
create policy "Users can delete messages in their conversations"
on messages for delete
using (
conversation_id in (
select id from conversations where user_id = auth.uid()
)
);
-- Create indexes for performance
create index if not exists conversations_user_id_idx on conversations(user_id);
create index if not exists messages_conversation_id_idx on messages(conversation_id);