-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathschema.sql
More file actions
67 lines (55 loc) · 2.53 KB
/
schema.sql
File metadata and controls
67 lines (55 loc) · 2.53 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
CREATE TABLE IF NOT EXISTS doctors (
id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
email TEXT NOT NULL,
specialty TEXT NOT NULL DEFAULT 'General'
);
CREATE TABLE IF NOT EXISTS patients (
id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
email TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS slots (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
doctor_id UUID NOT NULL REFERENCES doctors(id) ON DELETE CASCADE,
start_time TIMESTAMPTZ NOT NULL,
end_time TIMESTAMPTZ NOT NULL,
is_booked BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TABLE IF NOT EXISTS appointments (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
patient_id UUID NOT NULL REFERENCES patients(id) ON DELETE CASCADE,
doctor_id UUID NOT NULL REFERENCES doctors(id) ON DELETE CASCADE,
slot_id UUID NOT NULL REFERENCES slots(id) ON DELETE CASCADE,
status TEXT NOT NULL DEFAULT 'active'
CHECK (status IN ('active', 'done', 'cancelled')),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS system_admins (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
);
ALTER TABLE doctors ENABLE ROW LEVEL SECURITY;
ALTER TABLE patients ENABLE ROW LEVEL SECURITY;
ALTER TABLE slots ENABLE ROW LEVEL SECURITY;
ALTER TABLE appointments ENABLE ROW LEVEL SECURITY;
ALTER TABLE system_admins ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "authenticated_read_doctors" ON doctors;
DROP POLICY IF EXISTS "patients_read_own" ON patients;
DROP POLICY IF EXISTS "authenticated_read_slots" ON slots;
DROP POLICY IF EXISTS "patient_read_own_appointments" ON appointments;
DROP POLICY IF EXISTS "doctor_read_own_appointments" ON appointments;
CREATE POLICY "authenticated_read_doctors" ON doctors
FOR SELECT USING (auth.role() = 'authenticated');
CREATE POLICY "patients_read_own" ON patients
FOR SELECT USING (auth.uid() = id);
CREATE POLICY "authenticated_read_slots" ON slots
FOR SELECT USING (auth.role() = 'authenticated');
CREATE POLICY "patient_read_own_appointments" ON appointments
FOR SELECT USING (auth.uid() = patient_id);
CREATE POLICY "doctor_read_own_appointments" ON appointments
FOR SELECT USING (auth.uid() = doctor_id);
INSERT INTO system_admins (email, password) VALUES
('admin@test.com', 'admin123')
ON CONFLICT (email) DO NOTHING;