-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsupabase_setup.sql
More file actions
151 lines (134 loc) · 6.11 KB
/
supabase_setup.sql
File metadata and controls
151 lines (134 loc) · 6.11 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
148
149
150
151
-- SkillForge Supabase Setup Script
-- Run this in your Supabase SQL Editor to create the necessary tables
-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Create course_enrollments table
CREATE TABLE IF NOT EXISTS course_enrollments (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id TEXT NOT NULL,
course_id TEXT NOT NULL,
enrolled_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
progress INTEGER DEFAULT 0,
status TEXT DEFAULT 'active',
completed_at TIMESTAMP WITH TIME ZONE,
final_score INTEGER,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id, course_id)
);
-- Create forum_categories table
CREATE TABLE IF NOT EXISTS forum_categories (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
icon TEXT,
color TEXT,
post_count INTEGER DEFAULT 0,
last_activity TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create forum_posts table
CREATE TABLE IF NOT EXISTS forum_posts (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id TEXT NOT NULL,
category_id TEXT REFERENCES forum_categories(id),
title TEXT NOT NULL,
content TEXT NOT NULL,
tags TEXT[] DEFAULT '{}',
view_count INTEGER DEFAULT 0,
like_count INTEGER DEFAULT 0,
reply_count INTEGER DEFAULT 0,
is_pinned BOOLEAN DEFAULT FALSE,
is_locked BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create forum_replies table
CREATE TABLE IF NOT EXISTS forum_replies (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id TEXT NOT NULL,
post_id UUID REFERENCES forum_posts(id) ON DELETE CASCADE,
parent_reply_id UUID REFERENCES forum_replies(id) ON DELETE CASCADE,
content TEXT NOT NULL,
like_count INTEGER DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Insert default forum categories
INSERT INTO forum_categories (id, name, description, icon, color) VALUES
('general', 'General Discussion', 'General discussions about learning and development', '💬', 'blue'),
('react', 'React & Frontend', 'Discussions about React, JavaScript, and frontend development', '⚛️', 'cyan'),
('python', 'Python & Data Science', 'Python programming, data science, and machine learning discussions', '🐍', 'green'),
('blockchain', 'Blockchain & Web3', 'Blockchain development, DApps, and cryptocurrency discussions', '⛓️', 'purple'),
('career', 'Career & Jobs', 'Career advice, job opportunities, and professional development', '💼', 'orange'),
('help', 'Help & Support', 'Get help with courses, technical issues, and platform questions', '🆘', 'red')
ON CONFLICT (id) DO NOTHING;
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_course_enrollments_user_id ON course_enrollments(user_id);
CREATE INDEX IF NOT EXISTS idx_course_enrollments_course_id ON course_enrollments(course_id);
CREATE INDEX IF NOT EXISTS idx_forum_posts_category ON forum_posts(category_id);
CREATE INDEX IF NOT EXISTS idx_forum_posts_user ON forum_posts(user_id);
CREATE INDEX IF NOT EXISTS idx_forum_posts_created ON forum_posts(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_forum_replies_post ON forum_replies(post_id);
-- Enable Row Level Security
ALTER TABLE course_enrollments ENABLE ROW LEVEL SECURITY;
ALTER TABLE forum_posts ENABLE ROW LEVEL SECURITY;
ALTER TABLE forum_replies ENABLE ROW LEVEL SECURITY;
-- RLS Policies for course_enrollments
CREATE POLICY "Anyone can view course enrollments" ON course_enrollments FOR SELECT USING (true);
CREATE POLICY "Users can insert their own enrollments" ON course_enrollments FOR INSERT WITH CHECK (true);
CREATE POLICY "Users can update their own enrollments" ON course_enrollments FOR UPDATE USING (true);
-- RLS Policies for forum_posts
CREATE POLICY "Anyone can view forum posts" ON forum_posts FOR SELECT USING (true);
CREATE POLICY "Anyone can create posts" ON forum_posts FOR INSERT WITH CHECK (true);
CREATE POLICY "Users can update their own posts" ON forum_posts FOR UPDATE USING (true);
-- RLS Policies for forum_replies
CREATE POLICY "Anyone can view forum replies" ON forum_replies FOR SELECT USING (true);
CREATE POLICY "Anyone can create replies" ON forum_replies FOR INSERT WITH CHECK (true);
CREATE POLICY "Users can update their own replies" ON forum_replies FOR UPDATE USING (true);
-- Function to update timestamps
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
-- Triggers for updating timestamps
CREATE TRIGGER update_forum_posts_updated_at BEFORE UPDATE ON forum_posts FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_forum_replies_updated_at BEFORE UPDATE ON forum_replies FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
-- Function to update forum post counts
CREATE OR REPLACE FUNCTION update_forum_post_count()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'INSERT' THEN
UPDATE forum_categories SET post_count = post_count + 1, last_activity = NOW() WHERE id = NEW.category_id;
RETURN NEW;
ELSIF TG_OP = 'DELETE' THEN
UPDATE forum_categories SET post_count = post_count - 1 WHERE id = OLD.category_id;
RETURN OLD;
END IF;
RETURN NULL;
END;
$$ language 'plpgsql';
-- Trigger for forum post count
CREATE TRIGGER update_forum_category_post_count
AFTER INSERT OR DELETE ON forum_posts
FOR EACH ROW EXECUTE FUNCTION update_forum_post_count();
-- Function to update reply counts
CREATE OR REPLACE FUNCTION update_reply_count()
RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'INSERT' THEN
UPDATE forum_posts SET reply_count = reply_count + 1, updated_at = NOW() WHERE id = NEW.post_id;
RETURN NEW;
ELSIF TG_OP = 'DELETE' THEN
UPDATE forum_posts SET reply_count = reply_count - 1, updated_at = NOW() WHERE id = OLD.post_id;
RETURN OLD;
END IF;
RETURN NULL;
END;
$$ language 'plpgsql';
-- Trigger for reply count
CREATE TRIGGER update_post_reply_count
AFTER INSERT OR DELETE ON forum_replies
FOR EACH ROW EXECUTE FUNCTION update_reply_count();