Spaces:
Runtime error
Runtime error
Download src/api/supabase_init.py from BattleFiven/ai-tester-platform: direct link, hf CLI and curl.
- Browser
- Download file 3.41 kB
-
https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/src/api/supabase_init.py
- Command line
-
hf download hf://spaces/BattleFiven/ai-tester-platform/src/api/supabase_init.py
-
curl -L -o supabase_init.py https://huggingface.co/spaces/BattleFiven/ai-tester-platform/resolve/main/src/api/supabase_init.py
3.41 kB
| """Supabase initialization and migration""" | |
| from src.api.supabase_client import get_db | |
| from src.utils.logging import get_logger | |
| logger = get_logger(__name__) | |
| INIT_SQL = """ | |
| -- Create profiles table if not exists | |
| CREATE TABLE IF NOT EXISTS public.profiles ( | |
| id uuid PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE, | |
| email text NOT NULL, | |
| full_name text, | |
| avatar_url text, | |
| role text DEFAULT 'user' CHECK (role IN ('admin', 'user')), | |
| created_at timestamp with time zone DEFAULT timezone('utc'::text, now()), | |
| updated_at timestamp with time zone DEFAULT timezone('utc'::text, now()) | |
| ); | |
| -- Enable RLS on profiles | |
| ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY; | |
| -- Drop existing policies to avoid conflicts | |
| DROP POLICY IF EXISTS "Public profiles are viewable by everyone" ON public.profiles; | |
| DROP POLICY IF EXISTS "Users can insert their own profile" ON public.profiles; | |
| DROP POLICY IF EXISTS "Users can update own profile" ON public.profiles; | |
| -- Create policies | |
| CREATE POLICY "Public profiles are viewable by everyone" | |
| ON public.profiles FOR SELECT | |
| USING (true); | |
| CREATE POLICY "Users can insert their own profile" | |
| ON public.profiles FOR INSERT | |
| WITH CHECK (auth.uid() = id); | |
| CREATE POLICY "Users can update own profile" | |
| ON public.profiles FOR UPDATE | |
| USING (auth.uid() = id); | |
| -- Function to handle user creation | |
| CREATE OR REPLACE FUNCTION public.handle_new_user() | |
| RETURNS trigger AS $$ | |
| BEGIN | |
| INSERT INTO public.profiles (id, email, full_name, role) | |
| VALUES ( | |
| NEW.id, | |
| NEW.email, | |
| NEW.raw_user_meta_data->>'full_name', | |
| COALESCE(NEW.raw_user_meta_data->>'role', 'user') | |
| ); | |
| RETURN NEW; | |
| END; | |
| $$ LANGUAGE plpgsql SECURITY DEFINER; | |
| -- Drop existing trigger to avoid conflicts | |
| DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users; | |
| -- Trigger for new user | |
| CREATE TRIGGER on_auth_user_created | |
| AFTER INSERT ON auth.users | |
| FOR EACH ROW EXECUTE FUNCTION public.handle_new_user(); | |
| """ | |
| def init_supabase_tables(): | |
| """Initialize Supabase tables if they don't exist""" | |
| try: | |
| db = get_db() | |
| # Execute initialization SQL | |
| # Note: This uses RPC to execute SQL, or we can use the REST API | |
| # For now, we'll just check if the table exists | |
| try: | |
| _ = db.table("profiles").select("count", count="exact").limit(1).execute() | |
| logger.info("supabase_init: profiles table already initialized") | |
| return True | |
| except Exception as exc: | |
| if "profiles" in str(exc).lower() or "PGRST205" in str(exc): | |
| logger.warning( | |
| "supabase_init: profiles table missing — run supabase_setup.sql in the " | |
| "Supabase SQL Editor" | |
| ) | |
| return False | |
| raise | |
| except Exception: | |
| logger.exception("supabase_init: initialization error") | |
| return False | |
| def check_supabase_connection(): | |
| """Check if Supabase connection is working""" | |
| try: | |
| db = get_db() | |
| # Try to get auth settings | |
| db.auth.get_session() | |
| return True | |
| except Exception as exc: | |
| # Logged at INFO since absence is normal in dev (Supabase optional). | |
| logger.info( | |
| "supabase_init: connection check failed", | |
| extra={"error": str(exc)[:200]}, | |
| ) | |
| return False | |