-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
323 lines (286 loc) · 10.9 KB
/
schema.sql
File metadata and controls
323 lines (286 loc) · 10.9 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
-- Enable necessary extensions
-- This extension is typically needed for uuid_generate_v4()
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Set timezone for default timestamps
SET TIMEZONE TO 'UTC';
-- Create tables
-- public.profiles table
CREATE TABLE IF NOT EXISTS public.profiles (
id uuid REFERENCES auth.users ON DELETE CASCADE NOT NULL PRIMARY KEY,
email text NOT NULL UNIQUE,
full_name text,
avatar_url text,
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
);
-- public.user_settings table
CREATE TABLE IF NOT EXISTS public.user_settings (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
user_id uuid REFERENCES public.profiles(id) ON DELETE CASCADE NOT NULL,
name text,
occupation text,
traits jsonb,
theme text,
language text,
notification_preferences jsonb,
extra text,
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,
UNIQUE(user_id)
);
-- public.chats table
CREATE TABLE IF NOT EXISTS public.chats (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
user_id uuid REFERENCES public.profiles(id) ON DELETE CASCADE NOT NULL,
title text NOT NULL,
model 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,
metadata jsonb,
public boolean NOT NULL DEFAULT false -- Whether this chat is public/shareable
);
-- public.messages table
-- Note: id is TEXT as per your requirement
CREATE TABLE IF NOT EXISTS public.messages (
id text DEFAULT uuid_generate_v4()::text PRIMARY KEY, -- Changed to TEXT
chat_id uuid REFERENCES public.chats(id) ON DELETE CASCADE NOT NULL,
role text NOT NULL,
content text NOT NULL,
type text NOT NULL,
created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
metadata jsonb
);
-- public.attachments table
-- Note: message_id is TEXT to match public.messages.id
CREATE TABLE IF NOT EXISTS public.attachments (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
message_id text REFERENCES public.messages(id) ON DELETE CASCADE NOT NULL, -- Changed to TEXT
file_name text NOT NULL,
file_type text NOT NULL,
file_size bigint NOT NULL,
url text NOT NULL,
created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
metadata jsonb
);
-- All Chats Index table (public.chat_index)
-- Note: message_id is TEXT to match public.messages.id
CREATE TABLE IF NOT EXISTS public.chat_index (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
chat_id uuid REFERENCES public.chats(id) ON DELETE CASCADE NOT NULL,
message_id text REFERENCES public.messages(id) ON DELETE CASCADE, -- Changed to TEXT
type text NOT NULL, -- e.g. 'question', 'answer', 'code', 'summary', 'decision'
snippet text NOT NULL,
score float,
created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
metadata jsonb -- extensible for future use (e.g. tags, Gemini output, etc)
);
-- public.code_conversions table
-- Note: message_id is TEXT to match public.messages.id
CREATE TABLE IF NOT EXISTS public.code_conversions (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
message_id text REFERENCES public.messages(id) ON DELETE CASCADE NOT NULL, -- Changed to TEXT
code_block_index integer NOT NULL, -- e.g., 0 for the first block, 1 for the second
target_language text NOT NULL,
converted_content text NOT NULL,
created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
metadata jsonb,
UNIQUE (message_id, code_block_index, target_language) -- Ensures uniqueness for each block, each language
);
-- Create indexes
CREATE INDEX IF NOT EXISTS idx_profiles_email ON public.profiles(email);
CREATE INDEX IF NOT EXISTS idx_chats_user_id ON public.chats(user_id);
CREATE INDEX IF NOT EXISTS idx_messages_chat_id ON public.messages(chat_id);
-- Index for message_id on attachments - now on TEXT type
CREATE INDEX IF NOT EXISTS idx_attachments_message_id ON public.attachments(message_id);
CREATE INDEX IF NOT EXISTS idx_chat_index_chat_id ON public.chat_index(chat_id);
-- Index for message_id on chat_index - now on TEXT type
CREATE INDEX IF NOT EXISTS idx_chat_index_message_id ON public.chat_index(message_id);
-- Index for message_id on code_conversions - now on TEXT type
CREATE INDEX IF NOT EXISTS idx_code_conversions_message_id ON public.code_conversions(message_id);
-- Enable RLS policies
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.user_settings ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.chats ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.messages ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.attachments ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.chat_index ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.code_conversions ENABLE ROW LEVEL SECURITY;
-- Profiles policies
CREATE POLICY "Users can view their own profile"
ON public.profiles FOR SELECT
USING (auth.uid() = id);
CREATE POLICY "Users can update their own profile"
ON public.profiles FOR UPDATE
USING (auth.uid() = id);
CREATE POLICY "Users can insert their own profile"
ON public.profiles FOR INSERT
WITH CHECK (auth.uid() = id);
-- User settings policies
CREATE POLICY "Users can view their own settings"
ON public.user_settings FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can update their own settings"
ON public.user_settings FOR UPDATE
USING (auth.uid() = user_id);
CREATE POLICY "Users can insert their own settings"
ON public.user_settings FOR INSERT
WITH CHECK (auth.uid() = user_id);
-- Chats policies
CREATE POLICY "Users can view their own chats"
ON public.chats FOR SELECT
USING (auth.uid() = user_id OR public);
CREATE POLICY "Users can create their own chats"
ON public.chats FOR INSERT
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update their own chats"
ON public.chats FOR UPDATE
USING (auth.uid() = user_id);
CREATE POLICY "Users can delete their own chats"
ON public.chats FOR DELETE
USING (auth.uid() = user_id);
-- Messages policies
CREATE POLICY "Users can view messages in their chats"
ON public.messages FOR SELECT
USING (
EXISTS (
SELECT 1 FROM public.chats
WHERE chats.id = messages.chat_id
AND chats.user_id = auth.uid()
)
);
CREATE POLICY "Users can create messages in their chats"
ON public.messages FOR INSERT
WITH CHECK (
EXISTS (
SELECT 1 FROM public.chats
WHERE chats.id = messages.chat_id
AND chats.user_id = auth.uid()
)
);
CREATE POLICY "Users can update messages in their chats"
ON public.messages FOR UPDATE
USING (
EXISTS (
SELECT 1 FROM public.chats
WHERE chats.id = messages.chat_id
AND chats.user_id = auth.uid()
)
);
CREATE POLICY "Users can delete messages in their chats"
ON public.messages FOR DELETE
USING (
EXISTS (
SELECT 1 FROM public.chats
WHERE chats.id = messages.chat_id
AND chats.user_id = auth.uid()
)
);
-- Attachments policies
CREATE POLICY "Users can view attachments in their messages"
ON public.attachments FOR SELECT
USING (
EXISTS (
SELECT 1 FROM public.messages
JOIN public.chats ON chats.id = messages.chat_id
WHERE messages.id = attachments.message_id
AND chats.user_id = auth.uid()
)
);
CREATE POLICY "Users can create attachments in their messages"
ON public.attachments FOR INSERT
WITH CHECK (
EXISTS (
SELECT 1 FROM public.messages
JOIN public.chats ON chats.id = messages.chat_id
WHERE messages.id = attachments.message_id
AND chats.user_id = auth.uid()
)
);
CREATE POLICY "Users can delete attachments in their messages"
ON public.attachments FOR DELETE
USING (
EXISTS (
SELECT 1 FROM public.messages
JOIN public.chats ON chats.id = messages.chat_id
WHERE messages.id = attachments.message_id
AND chats.user_id = auth.uid()
)
);
-- RLS policies for chat_index
CREATE POLICY "Users can view index items in their chats"
ON public.chat_index FOR SELECT
USING (
EXISTS (
SELECT 1 FROM public.chats
WHERE chats.id = chat_index.chat_id
AND chats.user_id = auth.uid()
)
);
CREATE POLICY "Users can insert index items in their chats"
ON public.chat_index FOR INSERT
WITH CHECK (
EXISTS (
SELECT 1 FROM public.chats
WHERE chats.id = chat_index.chat_id
AND chats.user_id = auth.uid()
)
);
CREATE POLICY "Users can update index items in their chats"
ON public.chat_index FOR UPDATE
USING (
EXISTS (
SELECT 1 FROM public.chats
WHERE chats.id = chat_index.chat_id
AND chats.user_id = auth.uid()
)
);
CREATE POLICY "Users can delete index items in their chats"
ON public.chat_index FOR DELETE
USING (
EXISTS (
SELECT 1 FROM public.chats
WHERE chats.id = chat_index.chat_id
AND chats.user_id = auth.uid()
)
);
-- RLS policies for code_conversions
CREATE POLICY "Users can view code conversions in their chats"
ON public.code_conversions FOR SELECT
USING (
EXISTS (
SELECT 1 FROM public.messages
JOIN public.chats ON chats.id = messages.chat_id
WHERE messages.id = code_conversions.message_id
AND chats.user_id = auth.uid()
)
);
CREATE POLICY "Users can create code conversions in their chats"
ON public.code_conversions FOR INSERT
WITH CHECK (
EXISTS (
SELECT 1 FROM public.messages
JOIN public.chats ON chats.id = messages.chat_id
WHERE messages.id = code_conversions.message_id
AND chats.user_id = auth.uid()
)
);
CREATE POLICY "Users can update code conversions in their chats"
ON public.code_conversions FOR UPDATE
USING (
EXISTS (
SELECT 1 FROM public.messages
JOIN public.chats ON chats.id = messages.chat_id
WHERE messages.id = code_conversions.message_id
AND chats.user_id = auth.uid()
)
);
CREATE POLICY "Users can delete code conversions in their chats"
ON public.code_conversions FOR DELETE
USING (
EXISTS (
SELECT 1 FROM public.messages
JOIN public.chats ON chats.id = messages.chat_id
WHERE messages.id = code_conversions.message_id
AND chats.user_id = auth.uid()
)
);