forked from ayush-that/codejeet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_schema.sql
More file actions
35 lines (31 loc) Β· 1.34 KB
/
database_schema.sql
File metadata and controls
35 lines (31 loc) Β· 1.34 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
CREATE TABLE IF NOT EXISTS user_progress (
id SERIAL PRIMARY KEY,
user_id TEXT NOT NULL,
question_slug TEXT NOT NULL,
completed BOOLEAN NOT NULL DEFAULT false,
completed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user_id, question_slug)
);
CREATE INDEX IF NOT EXISTS idx_user_progress_user_id ON user_progress(user_id);
CREATE INDEX IF NOT EXISTS idx_user_progress_question_slug ON user_progress(question_slug);
CREATE INDEX IF NOT EXISTS idx_user_progress_completed ON user_progress(completed);
ALTER TABLE user_progress ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can view their own progress" ON user_progress
FOR SELECT USING (auth.uid()::text = user_id);
CREATE POLICY "Users can insert their own progress" ON user_progress
FOR INSERT WITH CHECK (auth.uid()::text = user_id);
CREATE POLICY "Users can update their own progress" ON user_progress
FOR UPDATE USING (auth.uid()::text = user_id);
CREATE POLICY "Users can delete their own progress" ON user_progress
FOR DELETE USING (auth.uid()::text = user_id);
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER update_user_progress_updated_at
BEFORE UPDATE ON user_progress
EXECUTE FUNCTION update_updated_at_column();