Files
Gesti-n-Reservas-Naturcalab…/tools/test_supabase_connection.py

53 lines
1.9 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from dotenv import load_dotenv
import requests
import sys
# Load .env
load_dotenv()
SUPABASE_URL = os.getenv("VITE_SUPABASE_URL")
SUPABASE_KEY = os.getenv("VITE_SUPABASE_ANON_KEY")
if not SUPABASE_URL or not SUPABASE_KEY:
print("❌ Error: Credentials not found in .env")
sys.exit(1)
print(f"🔄 Testing connection to: {SUPABASE_URL}")
headers = {
"apikey": SUPABASE_KEY,
"Authorization": f"Bearer {SUPABASE_KEY}"
}
# Try to list tables (or just a basic query to health check)
# Since we might not have tables yet, querying '/' usually returns a 404 or doc,
# but querying '/rest/v1/' should give us info or at least connect.
# A better check is to query a table that doesn't exist and expect a specific error, OR
# if we created the table via dashboard, query it.
# As a handshake, we will try to query the REST endpoint top level.
try:
response = requests.get(f"{SUPABASE_URL}/rest/v1/", headers=headers)
# 200 or 404 is fine as long as it's not 401 Unauthorized or Connection Error
if response.status_code in [200, 404]:
print(f"✅ Connection Successful! (Status: {response.status_code})")
# Try to query 'reservations' table to see if it exists
resp_table = requests.get(f"{SUPABASE_URL}/rest/v1/reservations?select=count", headers=headers)
if resp_table.status_code == 200:
print("✅ Table 'reservations' found.")
elif resp_table.status_code == 404:
print("⚠️ Table 'reservations' NOT found. (You need to run db_schema.sql)")
else:
print(f" Table check status: {resp_table.status_code}")
elif response.status_code == 401:
print("❌ Authorization Failed (401). Check your Anon Key.")
sys.exit(1)
else:
print(f"⚠️ Unexpected Status: {response.status_code}")
except Exception as e:
print(f"❌ Connection Error: {e}")
sys.exit(1)