""" Simple script to create admin_rules table in Supabase. This uses the Supabase Management API or direct SQL execution. """ import os from dotenv import load_dotenv import httpx import json load_dotenv() SUPABASE_URL = os.getenv("SUPABASE_URL") SUPABASE_SERVICE_KEY = os.getenv("SUPABASE_SERVICE_KEY") if not SUPABASE_URL or not SUPABASE_SERVICE_KEY: print("āŒ Missing Supabase credentials!") print(" Set SUPABASE_URL and SUPABASE_SERVICE_KEY in .env file") exit(1) # Read the SQL file sql_file = Path("supabase_admin_rules_table.sql") if not sql_file.exists(): print(f"āŒ SQL file not found: {sql_file}") exit(1) with open(sql_file, "r") as f: sql_content = f.read() print("šŸ”— Connecting to Supabase...") print(f" URL: {SUPABASE_URL[:50]}...") # Method 1: Try using Supabase REST API with SQL execution # Note: This requires the pg_net extension or a custom function # Most Supabase projects don't allow direct SQL execution via REST API # Method 2: Use Supabase Python client to execute via RPC try: from supabase import create_client client = create_client(SUPABASE_URL, SUPABASE_SERVICE_KEY) # Split SQL into individual statements statements = [s.strip() for s in sql_content.split(";") if s.strip() and not s.strip().startswith("--")] print(f"\nšŸ“ Executing {len(statements)} SQL statements...") # Execute each statement # Note: Supabase Python client doesn't support direct SQL execution # We'll need to use a workaround or manual execution print("\nāš ļø Direct SQL execution via Python client is not supported.") print(" Supabase requires SQL to be executed via the SQL Editor.") print("\nšŸ“‹ Please follow these steps:") print(" 1. Go to: https://app.supabase.com") print(" 2. Select your project") print(" 3. Click 'SQL Editor' in the left sidebar") print(" 4. Click 'New query'") print(" 5. Copy the contents of: supabase_admin_rules_table.sql") print(" 6. Paste into the SQL Editor") print(" 7. Click 'Run' (or press Ctrl+Enter)") print("\nāœ… After running the SQL, the table will be created!") except ImportError: print("āŒ Supabase client not installed") print(" Run: pip install supabase") except Exception as e: print(f"āŒ Error: {e}") print("\nšŸ’” Manual setup required - see instructions above")