NitinBot001 commited on
Commit
233e17d
Β·
verified Β·
1 Parent(s): 3c3c9ca

Update db.py

Browse files
Files changed (1) hide show
  1. db.py +16 -8
db.py CHANGED
@@ -24,29 +24,37 @@ from typing import Optional
24
 
25
  from supabase import create_client, Client
26
 
27
- SUPABASE_URL = os.getenv("SUPABASE_URL", "")
28
- SUPABASE_KEY = os.getenv("SUPABASE_KEY", "")
29
-
30
  _supabase: Optional[Client] = None
31
 
32
  TABLE = "files"
33
 
34
 
35
  def _get_client() -> Client:
 
 
 
 
 
36
  global _supabase
37
  if _supabase is None:
38
- if not SUPABASE_URL or not SUPABASE_KEY:
 
 
39
  raise RuntimeError(
40
  "SUPABASE_URL and SUPABASE_KEY must be set in environment / .env"
41
  )
42
- _supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
43
  return _supabase
44
 
45
 
46
  def init_db():
47
  """Verify Supabase connection by performing a lightweight query."""
48
- client = _get_client()
49
- client.table(TABLE).select("file_id").limit(1).execute()
 
 
 
 
50
 
51
 
52
  # ──────────────────────────────────────────────────
@@ -132,4 +140,4 @@ def count_files() -> int:
132
  .select("file_id", count="exact")
133
  .execute()
134
  )
135
- return resp.count or 0
 
24
 
25
  from supabase import create_client, Client
26
 
 
 
 
27
  _supabase: Optional[Client] = None
28
 
29
  TABLE = "files"
30
 
31
 
32
  def _get_client() -> Client:
33
+ """
34
+ FIX: env vars are now read INSIDE this function (not at module level).
35
+ This ensures load_dotenv() has already run before we read them,
36
+ and avoids caching empty strings at import time.
37
+ """
38
  global _supabase
39
  if _supabase is None:
40
+ url = os.getenv("SUPABASE_URL", "")
41
+ key = os.getenv("SUPABASE_KEY", "")
42
+ if not url or not key:
43
  raise RuntimeError(
44
  "SUPABASE_URL and SUPABASE_KEY must be set in environment / .env"
45
  )
46
+ _supabase = create_client(url, key)
47
  return _supabase
48
 
49
 
50
  def init_db():
51
  """Verify Supabase connection by performing a lightweight query."""
52
+ try:
53
+ client = _get_client()
54
+ client.table(TABLE).select("file_id").limit(1).execute()
55
+ except Exception as exc:
56
+ # Re-raise with a clearer message so _startup() can log it properly
57
+ raise RuntimeError(f"Supabase connection failed: {exc}") from exc
58
 
59
 
60
  # ──────────────────────────────────────────────────
 
140
  .select("file_id", count="exact")
141
  .execute()
142
  )
143
+ return resp.count or 0