-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
330 lines (275 loc) · 11.6 KB
/
Copy pathschema.sql
File metadata and controls
330 lines (275 loc) · 11.6 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
324
325
326
327
328
329
330
-- OpenHours — Supabase Schema
-- Run this in the Supabase SQL Editor after enabling the vector extension.
--
-- Step 1: Dashboard → Database → Extensions → enable "vector"
-- Step 2: Paste and run this entire file in the SQL Editor
-- ============================================================
-- Extensions
-- ============================================================
create extension if not exists vector;
-- ============================================================
-- Profiles (extends Supabase auth.users)
-- ============================================================
create table if not exists profiles (
id uuid references auth.users(id) on delete cascade primary key,
role text not null check (role in ('professor', 'student')),
full_name text,
created_at timestamptz default now()
);
-- Auto-create a profile row when a new user signs up
create or replace function handle_new_user()
returns trigger language plpgsql security definer as $
begin
insert into public.profiles (id, role, full_name)
values (
new.id,
coalesce(new.raw_user_meta_data->>'role', 'student'),
coalesce(new.raw_user_meta_data->>'full_name', '')
)
on conflict (id) do nothing;
return new;
end;
$;
drop trigger if exists on_auth_user_created on auth.users;
create trigger on_auth_user_created
after insert on auth.users
for each row execute procedure handle_new_user();
-- ============================================================
-- Courses
-- ============================================================
create table if not exists courses (
id uuid primary key default gen_random_uuid(),
professor_id uuid references profiles(id) on delete cascade not null,
name text not null,
description text,
join_code text unique not null default upper(substring(replace(gen_random_uuid()::text, '-', ''), 1, 6)),
created_at timestamptz default now()
);
create index if not exists courses_professor_id_idx on courses(professor_id);
create index if not exists courses_join_code_idx on courses(join_code);
-- ============================================================
-- Enrollments (student ↔ course membership via join code)
-- ============================================================
create table if not exists enrollments (
id uuid primary key default gen_random_uuid(),
student_id uuid references profiles(id) on delete cascade not null,
course_id uuid references courses(id) on delete cascade not null,
created_at timestamptz default now(),
unique(student_id, course_id)
);
create index if not exists enrollments_student_id_idx on enrollments(student_id);
create index if not exists enrollments_course_id_idx on enrollments(course_id);
-- ============================================================
-- Documents (parsed chunks + pgvector embeddings)
-- ============================================================
create table if not exists documents (
id uuid primary key default gen_random_uuid(),
course_id uuid references courses(id) on delete cascade not null,
content text not null,
embedding vector(1536), -- OpenAI text-embedding-3-small dimensions
source_file text,
created_at timestamptz default now()
);
create index if not exists documents_course_id_idx on documents(course_id);
-- IVFFlat index for fast approximate nearest-neighbour search
-- (create after inserting data; lists ≈ rows/1000, min 10)
-- create index documents_embedding_idx on documents
-- using ivfflat (embedding vector_cosine_ops) with (lists = 100);
-- ============================================================
-- Semantic search function (called by the FastAPI backend)
-- ============================================================
create or replace function match_documents(
query_embedding vector(1536),
match_course_id uuid,
match_count int default 6
)
returns table (content text, similarity float)
language sql stable
as $
select
content,
1 - (embedding <-> query_embedding) as similarity
from documents
where course_id = match_course_id
and embedding is not null
order by embedding <-> query_embedding
limit match_count;
$;
-- ============================================================
-- Chat sessions (student conversation history)
-- ============================================================
create table if not exists chat_sessions (
id uuid primary key default gen_random_uuid(),
student_id uuid references profiles(id) on delete cascade not null,
course_id uuid references courses(id) on delete cascade not null,
title text not null,
pinned boolean not null default false,
created_at timestamptz default now(),
updated_at timestamptz default now()
);
create index if not exists chat_sessions_student_course_idx
on chat_sessions(student_id, course_id);
create index if not exists chat_sessions_updated_at_idx
on chat_sessions(updated_at desc);
-- ============================================================
-- Chat messages (one row per turn within a session)
-- ============================================================
create table if not exists chat_messages (
id uuid primary key default gen_random_uuid(),
session_id uuid references chat_sessions(id) on delete cascade not null,
role text not null check (role in ('user', 'assistant')),
content text not null,
created_at timestamptz default now()
);
create index if not exists chat_messages_session_id_idx
on chat_messages(session_id, created_at);
-- ============================================================
-- Office hours bookings
-- (Unused — feature not exposed in current UI; kept for future use.)
-- ============================================================
create table if not exists bookings (
id uuid primary key default gen_random_uuid(),
student_id uuid references profiles(id) on delete cascade not null,
course_id uuid references courses(id) on delete cascade not null,
message text,
status text not null default 'pending'
check (status in ('pending', 'confirmed', 'declined')),
created_at timestamptz default now()
);
create index if not exists bookings_course_id_idx on bookings(course_id);
create index if not exists bookings_student_id_idx on bookings(student_id);
create index if not exists bookings_status_idx on bookings(status);
-- ============================================================
-- Question logs (for analytics)
-- ============================================================
create table if not exists question_logs (
id uuid primary key default gen_random_uuid(),
course_id uuid references courses(id) on delete cascade not null,
question text not null,
created_at timestamptz default now()
);
create index if not exists question_logs_course_id_idx on question_logs(course_id);
create index if not exists question_logs_created_at_idx on question_logs(created_at desc);
-- ============================================================
-- Row Level Security
-- ============================================================
alter table profiles enable row level security;
alter table courses enable row level security;
alter table enrollments enable row level security;
alter table documents enable row level security;
alter table chat_sessions enable row level security;
alter table chat_messages enable row level security;
alter table bookings enable row level security;
alter table question_logs enable row level security;
-- ------------------------------------------------------------
-- Profiles: users can read/update their own row
-- ------------------------------------------------------------
create policy "profiles: own row" on profiles
for all using (auth.uid() = id);
-- ------------------------------------------------------------
-- Courses
-- Professors manage their own courses.
-- Students can read courses (needed for join code lookup and
-- enrolled course display). Uses profiles check to avoid
-- recursive policy evaluation.
-- ------------------------------------------------------------
create policy "courses: professor owns" on courses
for all using (auth.uid() = professor_id)
with check (auth.uid() = professor_id);
create policy "courses: students read" on courses
for select using (
exists (
select 1 from profiles
where id = auth.uid() and role = 'student'
)
);
-- ------------------------------------------------------------
-- Enrollments
-- Students manage their own enrollments (join / leave).
-- Professors can read enrollments for their courses.
-- ------------------------------------------------------------
create policy "enrollments: student owns" on enrollments
for all using (auth.uid() = student_id)
with check (auth.uid() = student_id);
create policy "enrollments: professor reads" on enrollments
for select using (
exists (
select 1 from courses
where courses.id = enrollments.course_id
and courses.professor_id = auth.uid()
)
);
-- ------------------------------------------------------------
-- Documents
-- Professors manage docs for their courses.
-- Students can read all documents (RAG queries go through the
-- backend service role, but direct reads are also allowed).
-- ------------------------------------------------------------
create policy "documents: professor owns" on documents
for all using (
exists (
select 1 from courses
where courses.id = documents.course_id
and courses.professor_id = auth.uid()
)
);
create policy "documents: students read" on documents
for select using (
exists (
select 1 from profiles
where id = auth.uid() and role = 'student'
)
);
-- ------------------------------------------------------------
-- Chat sessions
-- Students fully manage their own sessions.
-- ------------------------------------------------------------
create policy "chat_sessions: student owns" on chat_sessions
for all using (auth.uid() = student_id)
with check (auth.uid() = student_id);
-- ------------------------------------------------------------
-- Chat messages
-- Students manage messages within their own sessions.
-- ------------------------------------------------------------
create policy "chat_messages: student owns" on chat_messages
for all using (
exists (
select 1 from chat_sessions
where chat_sessions.id = chat_messages.session_id
and chat_sessions.student_id = auth.uid()
)
);
-- ------------------------------------------------------------
-- Bookings (unused in current UI, kept for future use)
-- ------------------------------------------------------------
create policy "bookings: student owns" on bookings
for all using (auth.uid() = student_id);
create policy "bookings: professor reads" on bookings
for select using (
exists (
select 1 from courses
where courses.id = bookings.course_id
and courses.professor_id = auth.uid()
)
);
create policy "bookings: professor updates" on bookings
for update using (
exists (
select 1 from courses
where courses.id = bookings.course_id
and courses.professor_id = auth.uid()
)
);
-- ------------------------------------------------------------
-- Question logs
-- Backend writes via service role (bypasses RLS).
-- Professors can read logs for their courses.
-- ------------------------------------------------------------
create policy "question_logs: professor reads" on question_logs
for select using (
exists (
select 1 from courses
where courses.id = question_logs.course_id
and courses.professor_id = auth.uid()
)
);