forked from Dianger16/UnivGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_schema.sql
More file actions
62 lines (52 loc) · 2.2 KB
/
database_schema.sql
File metadata and controls
62 lines (52 loc) · 2.2 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
-- UniGPT Supabase Database Schema
-- Run this in your Supabase SQL Editor
-- 1. Profiles Table (Extends Supabase Auth)
CREATE TABLE IF NOT EXISTS public.profiles (
id UUID REFERENCES auth.users ON DELETE CASCADE PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
full_name TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'student', -- 'student', 'faculty', 'admin'
department TEXT,
avatar_url TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Enable RLS (Row Level Security)
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
-- 2. Documents Metadata Table
CREATE TABLE IF NOT EXISTS public.documents (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
uploader_id UUID REFERENCES public.profiles(id) ON DELETE SET NULL,
filename TEXT NOT NULL,
doc_type TEXT NOT NULL, -- 'student', 'faculty', 'admin', 'public'
department TEXT,
course TEXT,
tags TEXT[] DEFAULT '{}',
visibility BOOLEAN DEFAULT TRUE,
uploaded_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
ALTER TABLE public.documents ENABLE ROW LEVEL SECURITY;
-- 3. Conversations Table (Chat History)
CREATE TABLE IF NOT EXISTS public.conversations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES public.profiles(id) ON DELETE CASCADE,
role TEXT NOT NULL,
title TEXT,
messages JSONB DEFAULT '[]', -- Stores the array of {role, content}
last_active TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
ALTER TABLE public.conversations ENABLE ROW LEVEL SECURITY;
-- 4. Audit Logs Table
CREATE TABLE IF NOT EXISTS public.audit_logs (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
user_id UUID REFERENCES public.profiles(id) ON DELETE SET NULL,
action TEXT NOT NULL,
payload JSONB DEFAULT '{}',
ip_address TEXT,
timestamp TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
ALTER TABLE public.audit_logs ENABLE ROW LEVEL SECURITY;
-- 5. Policies (Basic examples, customize as needed)
-- Allow users to read their own profile
CREATE POLICY "Users can view own profile" ON public.profiles FOR SELECT USING (auth.uid() = id);
-- Allow admins/faculty to view all documents
CREATE POLICY "Faculty can view all documents" ON public.documents FOR SELECT USING (true);