forked from mendsalbert/vivo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
104 lines (82 loc) · 2.93 KB
/
schema.sql
File metadata and controls
104 lines (82 loc) · 2.93 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
-- ============================================
-- Vivo Health - Supabase Database Schema
-- ============================================
-- Lab Reports Table
CREATE TABLE IF NOT EXISTS lab_reports (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
file_name TEXT NOT NULL,
raw_text TEXT NOT NULL,
structured_data JSONB,
ai_analysis TEXT,
uploaded_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_lab_reports_user_id ON lab_reports(user_id);
CREATE INDEX IF NOT EXISTS idx_lab_reports_uploaded_at ON lab_reports(uploaded_at DESC);
ALTER TABLE lab_reports ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "Users can view own lab reports" ON lab_reports;
DROP POLICY IF EXISTS "Users can insert own lab reports" ON lab_reports;
DROP POLICY IF EXISTS "Users can update own lab reports" ON lab_reports;
DROP POLICY IF EXISTS "Users can delete own lab reports" ON lab_reports;
CREATE POLICY "Users can view own lab reports"
ON lab_reports
FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own lab reports"
ON lab_reports
FOR INSERT
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own lab reports"
ON lab_reports
FOR UPDATE
USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own lab reports"
ON lab_reports
FOR DELETE
USING (auth.uid() = user_id);
-- Notes Table
CREATE TABLE IF NOT EXISTS notes (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
content TEXT NOT NULL,
encrypted BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_notes_user_id ON notes(user_id);
CREATE INDEX IF NOT EXISTS idx_notes_created_at ON notes(created_at DESC);
ALTER TABLE notes ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "Users can view own notes" ON notes;
DROP POLICY IF EXISTS "Users can insert own notes" ON notes;
DROP POLICY IF EXISTS "Users can update own notes" ON notes;
DROP POLICY IF EXISTS "Users can delete own notes" ON notes;
CREATE POLICY "Users can view own notes"
ON notes
FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own notes"
ON notes
FOR INSERT
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own notes"
ON notes
FOR UPDATE
USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own notes"
ON notes
FOR DELETE
USING (auth.uid() = user_id);
-- Optional tags column for notes (run only if missing)
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_name = 'notes' AND column_name = 'tags'
) THEN
ALTER TABLE notes ADD COLUMN tags TEXT[];
END IF;
END $$;