import os import sys from datetime import datetime, timezone from supabase import create_client, Client def main(): print("--- Supabase Connection Test ---") # 1. Check Env Vars url = os.getenv("SUPABASE_URL") key = os.getenv("SUPABASE_ANON_KEY") or os.getenv("SUPABASE_SERVICE_ROLE_KEY") enable_metrics = os.getenv("ENABLE_METRICS_LOGGING", "false").lower() == "true" print(f"ENABLE_METRICS_LOGGING: {enable_metrics}") print(f"SUPABASE_URL: {'Set' if url else 'Missing'}") print(f"SUPABASE_KEY: {'Set' if key else 'Missing'}") if not url or not key: print("❌ Missing Supabase credentials.") sys.exit(1) if not enable_metrics: print("⚠️ ENABLE_METRICS_LOGGING is not true. The backend would skip logging.") print(" Proceeding with test anyway to verify connection...") # 2. Connect try: supabase: Client = create_client(url, key) print("✅ Supabase client initialized.") except Exception as e: print(f"❌ Failed to initialize client: {e}") sys.exit(1) # 3. Test Insert payload = { "question": "TEST_CONNECTION_PROBE", "answer": "This is a test row from scripts/test_supabase_connection.py", "provenance": "test_script", "user_id": "test_user" } print(f"Attempting to insert into 'metrics_chat_answers': {payload}") try: response = supabase.table("metrics_chat_answers").insert(payload).execute() print("✅ Insert successful!") print("Response data:", response.data) except Exception as e: print(f"❌ Insert failed: {e}") print("Possible causes:") print(" - Table 'metrics_chat_answers' does not exist.") print(" - RLS (Row Level Security) blocks insertion for anon key.") print(" - Triggers or constraints failed.") if __name__ == "__main__": main()