File size: 2,407 Bytes
a477044
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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")