-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_schema.sql
More file actions
96 lines (82 loc) · 3.37 KB
/
supabase_schema.sql
File metadata and controls
96 lines (82 loc) · 3.37 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
-- AI Chat App - Supabase Database Schema
-- Run this script in your Supabase SQL Editor
-- Enable UUID extension
create extension if not exists "uuid-ossp";
-- Create chat_sessions table
create table if not exists chat_sessions (
id uuid default uuid_generate_v4() primary key,
user_id uuid references auth.users(id) on delete cascade not null,
title text not null,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
updated_at timestamp with time zone default timezone('utc'::text, now()) not null
);
-- Create messages table
create table if not exists messages (
id uuid default uuid_generate_v4() primary key,
session_id uuid references chat_sessions(id) on delete cascade not null,
content text not null,
is_user boolean not null,
timestamp timestamp with time zone default timezone('utc'::text, now()) not null,
audio_url text
);
-- Enable Row Level Security (RLS)
alter table chat_sessions enable row level security;
alter table messages enable row level security;
-- Drop existing policies if they exist
drop policy if exists "Users can view their own chat sessions" on chat_sessions;
drop policy if exists "Users can create their own chat sessions" on chat_sessions;
drop policy if exists "Users can update their own chat sessions" on chat_sessions;
drop policy if exists "Users can delete their own chat sessions" on chat_sessions;
drop policy if exists "Users can view messages from their chat sessions" on messages;
drop policy if exists "Users can create messages in their chat sessions" on messages;
-- Chat Sessions Policies
create policy "Users can view their own chat sessions"
on chat_sessions for select
using (auth.uid() = user_id);
create policy "Users can create their own chat sessions"
on chat_sessions for insert
with check (auth.uid() = user_id);
create policy "Users can update their own chat sessions"
on chat_sessions for update
using (auth.uid() = user_id);
create policy "Users can delete their own chat sessions"
on chat_sessions for delete
using (auth.uid() = user_id);
-- Messages Policies
create policy "Users can view messages from their chat sessions"
on messages for select
using (
exists (
select 1 from chat_sessions
where chat_sessions.id = messages.session_id
and chat_sessions.user_id = auth.uid()
)
);
create policy "Users can create messages in their chat sessions"
on messages for insert
with check (
exists (
select 1 from chat_sessions
where chat_sessions.id = messages.session_id
and chat_sessions.user_id = auth.uid()
)
);
-- Create indexes for better performance
create index if not exists chat_sessions_user_id_idx on chat_sessions(user_id);
create index if not exists chat_sessions_updated_at_idx on chat_sessions(updated_at desc);
create index if not exists messages_session_id_idx on messages(session_id);
create index if not exists messages_timestamp_idx on messages(timestamp);
-- Create function to update updated_at timestamp
create or replace function update_updated_at_column()
returns trigger as $$
begin
new.updated_at = now();
return new;
end;
$$ language plpgsql;
-- Create trigger to automatically update updated_at
drop trigger if exists update_chat_sessions_updated_at on chat_sessions;
create trigger update_chat_sessions_updated_at
before update on chat_sessions
for each row
execute function update_updated_at_column();