-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-schema.sql
More file actions
147 lines (123 loc) · 4.79 KB
/
Copy pathsupabase-schema.sql
File metadata and controls
147 lines (123 loc) · 4.79 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
-- PULSE Database Schema for Supabase
-- Run this in Supabase SQL Editor
-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- 1. User Profiles Table (extends Supabase Auth)
CREATE TABLE IF NOT EXISTS user_profiles (
id UUID REFERENCES auth.users(id) PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
major TEXT,
student_id TEXT,
is_admin BOOLEAN DEFAULT FALSE,
joined_date TIMESTAMP DEFAULT NOW(),
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW()
);
-- 2. Course Progress Table
CREATE TABLE IF NOT EXISTS course_progress (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
course_id TEXT NOT NULL,
completed BOOLEAN DEFAULT FALSE,
completed_at TIMESTAMP,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW(),
UNIQUE(user_id, course_id)
);
-- 3. Item Progress Table (lessons, quizzes, projects)
CREATE TABLE IF NOT EXISTS item_progress (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
course_id TEXT NOT NULL,
unit_id TEXT NOT NULL,
item_id TEXT NOT NULL,
completed BOOLEAN DEFAULT FALSE,
quiz_score INTEGER,
code_html TEXT,
code_css TEXT,
code_javascript TEXT,
completed_at TIMESTAMP,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW(),
UNIQUE(user_id, item_id)
);
-- 4. Task Progress Table
CREATE TABLE IF NOT EXISTS task_progress (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
item_id TEXT NOT NULL,
task_id INTEGER NOT NULL,
completed BOOLEAN DEFAULT FALSE,
completed_at TIMESTAMP,
created_at TIMESTAMP DEFAULT NOW(),
UNIQUE(user_id, item_id, task_id)
);
-- Indexes for better performance
CREATE INDEX IF NOT EXISTS idx_course_progress_user ON course_progress(user_id);
CREATE INDEX IF NOT EXISTS idx_item_progress_user ON item_progress(user_id);
CREATE INDEX IF NOT EXISTS idx_item_progress_course ON item_progress(course_id);
CREATE INDEX IF NOT EXISTS idx_task_progress_user ON task_progress(user_id);
-- Row Level Security (RLS) Policies
ALTER TABLE user_profiles ENABLE ROW LEVEL SECURITY;
ALTER TABLE course_progress ENABLE ROW LEVEL SECURITY;
ALTER TABLE item_progress ENABLE ROW LEVEL SECURITY;
ALTER TABLE task_progress ENABLE ROW LEVEL SECURITY;
-- User Profiles Policies
CREATE POLICY "Users can view own profile" ON user_profiles
FOR SELECT USING (auth.uid() = id);
CREATE POLICY "Users can update own profile" ON user_profiles
FOR UPDATE USING (auth.uid() = id);
CREATE POLICY "Users can insert own profile" ON user_profiles
FOR INSERT WITH CHECK (auth.uid() = id);
-- Course Progress Policies
CREATE POLICY "Users can view own course progress" ON course_progress
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own course progress" ON course_progress
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own course progress" ON course_progress
FOR UPDATE USING (auth.uid() = user_id);
-- Item Progress Policies
CREATE POLICY "Users can view own item progress" ON item_progress
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own item progress" ON item_progress
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own item progress" ON item_progress
FOR UPDATE USING (auth.uid() = user_id);
-- Task Progress Policies
CREATE POLICY "Users can view own task progress" ON task_progress
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own task progress" ON task_progress
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own task progress" ON task_progress
FOR UPDATE USING (auth.uid() = user_id);
-- Function to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Triggers for updated_at
CREATE TRIGGER update_user_profiles_updated_at BEFORE UPDATE ON user_profiles
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_course_progress_updated_at BEFORE UPDATE ON course_progress
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_item_progress_updated_at BEFORE UPDATE ON item_progress
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
-- Create admin user function
CREATE OR REPLACE FUNCTION create_admin_user(
admin_email TEXT,
admin_password TEXT,
admin_name TEXT
)
RETURNS JSON AS $$
DECLARE
new_user_id UUID;
BEGIN
-- This would be called from your app after Supabase Auth signup
-- Just a placeholder for reference
RETURN json_build_object('success', true);
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;