Spaces:
Sleeping
Sleeping
Download backend/schema.sql from kishankrish19/Chat: direct link, hf CLI and curl.
- Browser
- Download file 8.56 kB
-
https://huggingface.co/spaces/kishankrish19/Chat/resolve/main/backend/schema.sql
- Command line
-
hf download hf://spaces/kishankrish19/Chat/backend/schema.sql
-
curl -L -o schema.sql https://huggingface.co/spaces/kishankrish19/Chat/resolve/main/backend/schema.sql
8.56 kB
| -- 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); | |