umer6016 commited on
Commit
3d93756
·
1 Parent(s): 49462f7

Add /api/test-supabase debug endpoint

Browse files
Files changed (2) hide show
  1. backend/app/api/debug.py +68 -0
  2. backend/app/main.py +2 -1
backend/app/api/debug.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter
2
+ import os
3
+ import json
4
+ from datetime import datetime
5
+ from supabase import create_client, Client
6
+
7
+ router = APIRouter()
8
+
9
+ @router.get("/test-supabase", summary="Debug Supabase Connection")
10
+ async def test_supabase():
11
+ """
12
+ Debug endpoint to test Supabase connection and insertion.
13
+ WARNING: Public endpoint for debugging only. Remove after use.
14
+ """
15
+ results = {
16
+ "timestamp": datetime.now().isoformat(),
17
+ "env_vars": {},
18
+ "connection": "pending",
19
+ "insert": "pending",
20
+ "error": None
21
+ }
22
+
23
+ # 1. Check Env Vars
24
+ url = os.getenv("SUPABASE_URL")
25
+ key = os.getenv("SUPABASE_ANON_KEY") or os.getenv("SUPABASE_SERVICE_ROLE_KEY")
26
+ enable_metrics = os.getenv("ENABLE_METRICS_LOGGING", "false").lower() == "true"
27
+
28
+ results["env_vars"] = {
29
+ "SUPABASE_URL_SET": bool(url),
30
+ "SUPABASE_KEY_SET": bool(key),
31
+ "ENABLE_METRICS_LOGGING": enable_metrics,
32
+ "ENABLE_METRICS_LOGGING_RAW": os.getenv("ENABLE_METRICS_LOGGING")
33
+ }
34
+
35
+ if not url or not key:
36
+ results["connection"] = "failed"
37
+ results["error"] = "Missing SUPABASE_URL or SUPABASE_KEY env vars"
38
+ return results
39
+
40
+ # 2. Connect
41
+ try:
42
+ supabase: Client = create_client(url, key)
43
+ results["connection"] = "success"
44
+ except Exception as e:
45
+ results["connection"] = "failed"
46
+ results["error"] = f"Client init failed: {str(e)}"
47
+ return results
48
+
49
+ # 3. Test Insert
50
+ payload = {
51
+ "question": "DEBUG_ENDPOINT_PROBE",
52
+ "answer": "This is a test row from /api/test-supabase",
53
+ "provenance": "debug_endpoint",
54
+ "user_id": "debug_user"
55
+ }
56
+
57
+ try:
58
+ response = supabase.table("metrics_chat_answers").insert(payload).execute()
59
+ results["insert"] = "success"
60
+ results["data"] = response.data
61
+ except Exception as e:
62
+ results["insert"] = "failed"
63
+ results["error"] = f"Insert failed: {str(e)}"
64
+ # Add hint about RLS
65
+ if "policy" in str(e).lower() or "permission" in str(e).lower():
66
+ results["hint"] = "Check Row Level Security (RLS) policies for 'metrics_chat_answers' table."
67
+
68
+ return results
backend/app/main.py CHANGED
@@ -1,7 +1,7 @@
1
  from fastapi import FastAPI
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from .api.router import api_router
4
-
5
 
6
  def get_application() -> FastAPI:
7
  app = FastAPI(
@@ -24,6 +24,7 @@ def get_application() -> FastAPI:
24
  )
25
 
26
  app.include_router(api_router, prefix="/api")
 
27
 
28
  # Serve static files (Frontend)
29
  import os
 
1
  from fastapi import FastAPI
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from .api.router import api_router
4
+ from .api.debug import router as debug_router
5
 
6
  def get_application() -> FastAPI:
7
  app = FastAPI(
 
24
  )
25
 
26
  app.include_router(api_router, prefix="/api")
27
+ app.include_router(debug_router, prefix="/api")
28
 
29
  # Serve static files (Frontend)
30
  import os