Spaces:
Sleeping
Sleeping
File size: 8,561 Bytes
b0798af | 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 | -- StreamWave Supabase PostgreSQL Schema Definition
-- Version 1.0 (Full Spotify Feature Parity)
-- 1. Enable Required Extensions
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE EXTENSION IF NOT EXISTS "pg_trgm";
CREATE EXTENSION IF NOT EXISTS "vector";
-- 2. Schema Definitions
-- Table: profiles
CREATE TABLE IF NOT EXISTS public.profiles (
id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
username TEXT UNIQUE NOT NULL CHECK (char_length(username) >= 3 AND char_length(username) <= 30),
display_name TEXT NOT NULL,
avatar_url TEXT,
bio TEXT,
country CHAR(2) DEFAULT 'US',
account_type TEXT DEFAULT 'listener' CHECK (account_type IN ('listener', 'artist', 'admin')),
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Table: artists
CREATE TABLE IF NOT EXISTS public.artists (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
owner_profile_id UUID REFERENCES public.profiles(id) ON DELETE SET NULL,
name TEXT NOT NULL,
bio TEXT,
image_url TEXT,
verified BOOLEAN DEFAULT FALSE,
monthly_listeners BIGINT DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Table: genres
CREATE TABLE IF NOT EXISTS public.genres (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT UNIQUE NOT NULL,
slug TEXT UNIQUE NOT NULL
);
-- Table: albums
CREATE TABLE IF NOT EXISTS public.albums (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title TEXT NOT NULL,
primary_artist_id UUID NOT NULL REFERENCES public.artists(id) ON DELETE CASCADE,
cover_url TEXT,
album_type TEXT DEFAULT 'album' CHECK (album_type IN ('album', 'single', 'ep', 'compilation')),
release_date DATE DEFAULT CURRENT_DATE,
genre_id UUID REFERENCES public.genres(id) ON DELETE SET NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Table: tracks
CREATE TABLE IF NOT EXISTS public.tracks (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title TEXT NOT NULL,
album_id UUID REFERENCES public.albums(id) ON DELETE CASCADE,
primary_artist_id UUID NOT NULL REFERENCES public.artists(id) ON DELETE CASCADE,
duration_ms INTEGER NOT NULL,
track_number INTEGER DEFAULT 1,
audio_path TEXT NOT NULL,
cover_url TEXT,
explicit BOOLEAN DEFAULT FALSE,
isrc TEXT,
play_count BIGINT DEFAULT 0,
embedding vector(512),
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Table: track_artists (Featured / Collaborating Artists)
CREATE TABLE IF NOT EXISTS public.track_artists (
track_id UUID REFERENCES public.tracks(id) ON DELETE CASCADE,
artist_id UUID REFERENCES public.artists(id) ON DELETE CASCADE,
role TEXT DEFAULT 'featured',
PRIMARY KEY (track_id, artist_id)
);
-- Table: playlists
CREATE TABLE IF NOT EXISTS public.playlists (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
owner_id UUID NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
title TEXT NOT NULL,
description TEXT,
cover_url TEXT,
is_public BOOLEAN DEFAULT TRUE,
is_collaborative BOOLEAN DEFAULT FALSE,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Table: playlist_tracks
CREATE TABLE IF NOT EXISTS public.playlist_tracks (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
playlist_id UUID NOT NULL REFERENCES public.playlists(id) ON DELETE CASCADE,
track_id UUID NOT NULL REFERENCES public.tracks(id) ON DELETE CASCADE,
added_by UUID REFERENCES public.profiles(id) ON DELETE SET NULL,
position INTEGER NOT NULL,
added_at TIMESTAMPTZ DEFAULT NOW()
);
-- Library Saved Tables
CREATE TABLE IF NOT EXISTS public.library_tracks (
user_id UUID REFERENCES public.profiles(id) ON DELETE CASCADE,
track_id UUID REFERENCES public.tracks(id) ON DELETE CASCADE,
saved_at TIMESTAMPTZ DEFAULT NOW(),
PRIMARY KEY (user_id, track_id)
);
CREATE TABLE IF NOT EXISTS public.library_albums (
user_id UUID REFERENCES public.profiles(id) ON DELETE CASCADE,
album_id UUID REFERENCES public.albums(id) ON DELETE CASCADE,
saved_at TIMESTAMPTZ DEFAULT NOW(),
PRIMARY KEY (user_id, album_id)
);
CREATE TABLE IF NOT EXISTS public.library_playlists (
user_id UUID REFERENCES public.profiles(id) ON DELETE CASCADE,
playlist_id UUID REFERENCES public.playlists(id) ON DELETE CASCADE,
saved_at TIMESTAMPTZ DEFAULT NOW(),
PRIMARY KEY (user_id, playlist_id)
);
-- Table: follows
CREATE TABLE IF NOT EXISTS public.follows (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
follower_id UUID NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
target_type TEXT NOT NULL CHECK (target_type IN ('user', 'artist')),
target_id UUID NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(follower_id, target_type, target_id)
);
-- Table: play_history
CREATE TABLE IF NOT EXISTS public.play_history (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
track_id UUID NOT NULL REFERENCES public.tracks(id) ON DELETE CASCADE,
context_type TEXT CHECK (context_type IN ('playlist', 'album', 'artist', 'search', 'radio')),
context_id UUID,
ms_played INTEGER DEFAULT 0,
played_at TIMESTAMPTZ DEFAULT NOW()
);
-- Table: devices
CREATE TABLE IF NOT EXISTS public.devices (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
device_name TEXT NOT NULL,
device_type TEXT CHECK (device_type IN ('web', 'desktop', 'ios', 'android')),
last_active_at TIMESTAMPTZ DEFAULT NOW()
);
-- Table: playback_state
CREATE TABLE IF NOT EXISTS public.playback_state (
user_id UUID PRIMARY KEY REFERENCES public.profiles(id) ON DELETE CASCADE,
active_device_id UUID REFERENCES public.devices(id) ON DELETE SET NULL,
current_track_id UUID REFERENCES public.tracks(id) ON DELETE SET NULL,
position_ms INTEGER DEFAULT 0,
is_playing BOOLEAN DEFAULT FALSE,
shuffle BOOLEAN DEFAULT FALSE,
repeat_mode TEXT DEFAULT 'off' CHECK (repeat_mode IN ('off', 'track', 'context')),
queue JSONB DEFAULT '[]'::jsonb,
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Table: lyrics
CREATE TABLE IF NOT EXISTS public.lyrics (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
track_id UUID UNIQUE NOT NULL REFERENCES public.tracks(id) ON DELETE CASCADE,
content TEXT NOT NULL,
synced_lyrics JSONB,
language TEXT DEFAULT 'en'
);
-- Table: subscriptions
CREATE TABLE IF NOT EXISTS public.subscriptions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID UNIQUE NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
plan TEXT DEFAULT 'free' CHECK (plan IN ('free', 'premium_individual', 'premium_duo', 'premium_family', 'premium_student')),
status TEXT DEFAULT 'active' CHECK (status IN ('active', 'trialing', 'past_due', 'canceled')),
stripe_customer_id TEXT,
stripe_subscription_id TEXT,
current_period_end TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Table: notifications
CREATE TABLE IF NOT EXISTS public.notifications (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
type TEXT NOT NULL CHECK (type IN ('new_release', 'playlist_invite', 'new_follower', 'system')),
payload JSONB DEFAULT '{}'::jsonb,
is_read BOOLEAN DEFAULT FALSE,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- 3. Row Level Security (RLS) Policies
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Public profiles are readable by everyone" ON public.profiles FOR SELECT USING (true);
CREATE POLICY "Users can update their own profile" ON public.profiles FOR UPDATE USING (auth.uid() = id);
ALTER TABLE public.playlists ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Public playlists are readable" ON public.playlists FOR SELECT USING (is_public = TRUE OR owner_id = auth.uid());
CREATE POLICY "Users can insert playlists" ON public.playlists FOR INSERT WITH CHECK (auth.uid() = owner_id);
CREATE POLICY "Owners can update playlists" ON public.playlists FOR UPDATE USING (auth.uid() = owner_id);
CREATE POLICY "Owners can delete playlists" ON public.playlists FOR DELETE USING (auth.uid() = owner_id);
ALTER TABLE public.tracks ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Public tracks read access" ON public.tracks FOR SELECT USING (true);
ALTER TABLE public.albums ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Public albums read access" ON public.albums FOR SELECT USING (true);
ALTER TABLE public.artists ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Public artists read access" ON public.artists FOR SELECT USING (true);
|