Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """Test Supabase connection directly""" | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| try: | |
| from supabase import create_client | |
| supabase_url = os.getenv("SUPABASE_URL") | |
| supabase_key = os.getenv("SUPABASE_SERVICE_KEY") | |
| print("Testing Supabase Connection:") | |
| print("=" * 50) | |
| print(f"URL: {supabase_url}") | |
| print(f"Key length: {len(supabase_key) if supabase_key else 0}") | |
| print() | |
| if not supabase_url or not supabase_key: | |
| print("ERROR: Missing Supabase credentials") | |
| exit(1) | |
| print("Creating Supabase client...") | |
| client = create_client(supabase_url, supabase_key) | |
| print("[OK] Client created successfully") | |
| print() | |
| print("Testing table access...") | |
| tables = ["tool_usage_events", "redflag_violations", "rag_search_events", "agent_query_events"] | |
| for table in tables: | |
| try: | |
| result = client.table(table).select("id").limit(1).execute() | |
| print(f"[OK] Table '{table}' is accessible") | |
| except Exception as e: | |
| error_msg = str(e) | |
| if "does not exist" in error_msg.lower() or "relation" in error_msg.lower(): | |
| print(f"[ERROR] Table '{table}' does NOT exist") | |
| print(f" Solution: Run supabase_analytics_tables.sql in Supabase SQL Editor") | |
| elif "401" in error_msg or "Invalid API key" in error_msg: | |
| print(f"[ERROR] Table '{table}' access denied - Invalid API key") | |
| print(f" Error: {error_msg[:100]}") | |
| else: | |
| print(f"[ERROR] Table '{table}' error: {error_msg[:100]}") | |
| print() | |
| print("Testing insert...") | |
| try: | |
| test_payload = { | |
| "tenant_id": "test_connection", | |
| "tool_name": "connection_test", | |
| "timestamp": 1234567890, | |
| "success": True | |
| } | |
| result = client.table("tool_usage_events").insert(test_payload).execute() | |
| print("[OK] Test insert successful!") | |
| print(f" Inserted {len(result.data) if result.data else 1} row(s)") | |
| except Exception as e: | |
| error_msg = str(e) | |
| print(f"[ERROR] Test insert failed: {error_msg[:200]}") | |
| if "401" in error_msg or "Invalid API key" in error_msg: | |
| print(" This indicates an invalid API key") | |
| elif "does not exist" in error_msg.lower(): | |
| print(" This indicates the table doesn't exist") | |
| elif "RLS" in error_msg or "policy" in error_msg.lower(): | |
| print(" This indicates RLS policy blocking the insert") | |
| print() | |
| print("=" * 50) | |
| print("Connection test complete!") | |
| except ImportError: | |
| print("ERROR: supabase-py package not installed") | |
| print("Install it with: pip install supabase") | |
| except Exception as e: | |
| print(f"ERROR: {e}") | |
| import traceback | |
| traceback.print_exc() | |