Spaces:
Sleeping
Sleeping
File size: 10,097 Bytes
639bb77 16f3c9e 639bb77 | 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | -- VinClassroom Database Schema
-- Run this in Supabase SQL Editor
-- Enable necessary extensions
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Users (extends Supabase auth.users)
CREATE TABLE profiles (
id UUID REFERENCES auth.users(id) PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
display_name TEXT NOT NULL,
avatar_url TEXT,
bio TEXT,
color TEXT,
status TEXT DEFAULT 'offline',
last_seen TIMESTAMPTZ DEFAULT NOW(),
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Spaces
CREATE TABLE spaces (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
description TEXT,
icon_url TEXT,
owner_id UUID REFERENCES profiles(id),
is_private BOOLEAN DEFAULT false,
invite_code TEXT UNIQUE,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Rooms
CREATE TABLE rooms (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
space_id UUID REFERENCES spaces(id) ON DELETE CASCADE,
name TEXT NOT NULL,
description TEXT,
type TEXT DEFAULT 'text',
is_private BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Space Members
CREATE TABLE space_members (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
space_id UUID REFERENCES spaces(id) ON DELETE CASCADE,
user_id UUID REFERENCES profiles(id),
role TEXT DEFAULT 'member',
joined_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(space_id, user_id)
);
-- Room Members
CREATE TABLE room_members (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
room_id UUID REFERENCES rooms(id) ON DELETE CASCADE,
user_id UUID REFERENCES profiles(id),
joined_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(room_id, user_id)
);
-- Messages
CREATE TABLE messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
room_id UUID REFERENCES rooms(id) ON DELETE CASCADE,
user_id UUID REFERENCES profiles(id),
content TEXT,
reply_to_id UUID REFERENCES messages(id),
is_pinned BOOLEAN DEFAULT false,
is_edited BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
-- Message Attachments
CREATE TABLE message_attachments (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
message_id UUID REFERENCES messages(id) ON DELETE CASCADE,
file_name TEXT NOT NULL,
file_url TEXT NOT NULL,
file_type TEXT,
file_size INTEGER,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Reactions
CREATE TABLE reactions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
message_id UUID REFERENCES messages(id) ON DELETE CASCADE,
user_id UUID REFERENCES profiles(id),
emoji TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(message_id, user_id, emoji)
);
-- Direct Messages
CREATE TABLE dm_conversations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user1_id UUID REFERENCES profiles(id),
user2_id UUID REFERENCES profiles(id),
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user1_id, user2_id)
);
CREATE TABLE dm_messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
conversation_id UUID REFERENCES dm_conversations(id) ON DELETE CASCADE,
sender_id UUID REFERENCES profiles(id),
content TEXT,
is_read BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT NOW(),
deleted_at TIMESTAMPTZ
);
-- Notifications
CREATE TABLE notifications (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES profiles(id),
type TEXT NOT NULL,
title TEXT NOT NULL,
message TEXT,
data JSONB,
is_read BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Blocked Users
CREATE TABLE blocked_users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
blocker_id UUID REFERENCES profiles(id),
blocked_id UUID REFERENCES profiles(id),
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(blocker_id, blocked_id)
);
-- Space Invitations
CREATE TABLE space_invitations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
space_id UUID REFERENCES spaces(id) ON DELETE CASCADE,
email TEXT NOT NULL,
token TEXT UNIQUE NOT NULL,
invited_by UUID REFERENCES profiles(id),
status TEXT DEFAULT 'pending',
expires_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Files
CREATE TABLE files (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
uploader_id UUID REFERENCES profiles(id),
space_id UUID REFERENCES spaces(id),
room_id UUID REFERENCES rooms(id),
file_name TEXT NOT NULL,
file_url TEXT NOT NULL,
file_type TEXT,
file_size INTEGER,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Indexes for performance
CREATE INDEX idx_profiles_status ON profiles(status);
CREATE INDEX idx_profiles_last_seen ON profiles(last_seen);
CREATE INDEX idx_spaces_owner ON spaces(owner_id);
CREATE INDEX idx_spaces_invite_code ON spaces(invite_code);
CREATE INDEX idx_rooms_space ON rooms(space_id);
CREATE INDEX idx_space_members_space ON space_members(space_id);
CREATE INDEX idx_space_members_user ON space_members(user_id);
CREATE INDEX idx_room_members_room ON room_members(room_id);
CREATE INDEX idx_room_members_user ON room_members(user_id);
CREATE INDEX idx_messages_room ON messages(room_id);
CREATE INDEX idx_messages_user ON messages(user_id);
CREATE INDEX idx_messages_created_at ON messages(created_at);
CREATE INDEX idx_messages_reply_to ON messages(reply_to_id);
CREATE INDEX idx_reactions_message ON reactions(message_id);
CREATE INDEX idx_dm_conversations_user1 ON dm_conversations(user1_id);
CREATE INDEX idx_dm_conversations_user2 ON dm_conversations(user2_id);
CREATE INDEX idx_dm_messages_conversation ON dm_messages(conversation_id);
CREATE INDEX idx_notifications_user ON notifications(user_id);
CREATE INDEX idx_notifications_is_read ON notifications(is_read);
CREATE INDEX idx_files_space ON files(space_id);
CREATE INDEX idx_files_room ON files(room_id);
-- Enable Row Level Security
ALTER TABLE profiles ENABLE ROW LEVEL SECURITY;
ALTER TABLE spaces ENABLE ROW LEVEL SECURITY;
ALTER TABLE rooms ENABLE ROW LEVEL SECURITY;
ALTER TABLE space_members ENABLE ROW LEVEL SECURITY;
ALTER TABLE room_members ENABLE ROW LEVEL SECURITY;
ALTER TABLE messages ENABLE ROW LEVEL SECURITY;
ALTER TABLE message_attachments ENABLE ROW LEVEL SECURITY;
ALTER TABLE reactions ENABLE ROW LEVEL SECURITY;
ALTER TABLE dm_conversations ENABLE ROW LEVEL SECURITY;
ALTER TABLE dm_messages ENABLE ROW LEVEL SECURITY;
ALTER TABLE notifications ENABLE ROW LEVEL SECURITY;
ALTER TABLE blocked_users ENABLE ROW LEVEL SECURITY;
ALTER TABLE space_invitations ENABLE ROW LEVEL SECURITY;
ALTER TABLE files ENABLE ROW LEVEL SECURITY;
-- RLS Policies
-- Profiles: Users can read all profiles, update only their own
CREATE POLICY "Profiles are viewable by everyone" ON profiles
FOR SELECT USING (true);
CREATE POLICY "Users can update own profile" ON profiles
FOR UPDATE USING (auth.uid() = id);
-- Spaces: Members can view, owner can update/delete
CREATE POLICY "Spaces viewable by members" ON spaces
FOR SELECT USING (
EXISTS (
SELECT 1 FROM space_members WHERE space_id = id AND user_id = auth.uid()
) OR NOT is_private
);
CREATE POLICY "Spaces insertable by authenticated users" ON spaces
FOR INSERT WITH CHECK (auth.uid() = owner_id);
CREATE POLICY "Spaces updatable by owner" ON spaces
FOR UPDATE USING (auth.uid() = owner_id);
CREATE POLICY "Spaces deletable by owner" ON spaces
FOR DELETE USING (auth.uid() = owner_id);
-- Space Members: Viewable by space members
CREATE POLICY "Space members viewable by space members" ON space_members
FOR SELECT USING (
EXISTS (
SELECT 1 FROM space_members AS sm
WHERE sm.space_id = space_id AND sm.user_id = auth.uid()
)
);
-- Rooms: Viewable by space members
CREATE POLICY "Rooms viewable by space members" ON rooms
FOR SELECT USING (
EXISTS (
SELECT 1 FROM space_members
WHERE space_id = rooms.space_id AND user_id = auth.uid()
)
);
-- Messages: Viewable by room members
CREATE POLICY "Messages viewable by room members" ON messages
FOR SELECT USING (
EXISTS (
SELECT 1 FROM room_members
WHERE room_id = messages.room_id AND user_id = auth.uid()
)
);
CREATE POLICY "Users can insert own messages" ON messages
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own messages" ON messages
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own messages" ON messages
FOR DELETE USING (auth.uid() = user_id);
-- DM Conversations: Viewable by participants
CREATE POLICY "DM conversations viewable by participants" ON dm_conversations
FOR SELECT USING (auth.uid() = user1_id OR auth.uid() = user2_id);
-- DM Messages: Viewable by conversation participants
CREATE POLICY "DM messages viewable by participants" ON dm_messages
FOR SELECT USING (
EXISTS (
SELECT 1 FROM dm_conversations
WHERE id = conversation_id AND (user1_id = auth.uid() OR user2_id = auth.uid())
)
);
-- Notifications: Viewable by owner
CREATE POLICY "Notifications viewable by owner" ON notifications
FOR SELECT USING (auth.uid() = user_id);
-- Enable Realtime
ALTER PUBLICATION supabase_realtime ADD TABLE messages;
ALTER PUBLICATION supabase_realtime ADD TABLE dm_messages;
ALTER PUBLICATION supabase_realtime ADD TABLE files;
ALTER PUBLICATION supabase_realtime ADD TABLE reactions;
ALTER PUBLICATION supabase_realtime ADD TABLE notifications;
-- Functions for updated_at
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_profiles_updated_at BEFORE UPDATE ON profiles
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_spaces_updated_at BEFORE UPDATE ON spaces
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_rooms_updated_at BEFORE UPDATE ON rooms
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_messages_updated_at BEFORE UPDATE ON messages
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|