Spaces:
Sleeping
Sleeping
Parthnuwal7 commited on
Commit ·
d95bc83
1
Parent(s): f9c18ef
Use analytics-data storage bucket
Browse files- database/db.py +37 -15
database/db.py
CHANGED
|
@@ -1,13 +1,40 @@
|
|
| 1 |
-
"""Database connection -
|
| 2 |
import os
|
| 3 |
from config import Config
|
| 4 |
|
| 5 |
-
# Storage mode:
|
| 6 |
STORAGE_MODE = None
|
| 7 |
_db_client = None
|
| 8 |
_db_error: str = None
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def _init_local():
|
| 12 |
"""Initialize local file storage"""
|
| 13 |
global STORAGE_MODE, _db_client, _db_error
|
|
@@ -16,17 +43,18 @@ def _init_local():
|
|
| 16 |
from database.local_storage import get_local_storage
|
| 17 |
_db_client = get_local_storage()
|
| 18 |
STORAGE_MODE = 'local'
|
| 19 |
-
print("[DB]
|
| 20 |
return True
|
| 21 |
except Exception as e:
|
| 22 |
-
_db_error = f"Failed to
|
| 23 |
print(f"[DB ERROR] {_db_error}")
|
| 24 |
return False
|
| 25 |
|
| 26 |
|
| 27 |
def _init_db():
|
| 28 |
-
"""Initialize
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
# Initialize on module load
|
|
@@ -34,31 +62,25 @@ _init_db()
|
|
| 34 |
|
| 35 |
|
| 36 |
def get_db():
|
| 37 |
-
"""Get database client
|
| 38 |
-
global _db_client, _db_error
|
| 39 |
-
|
| 40 |
if _db_client is None:
|
| 41 |
_init_db()
|
| 42 |
-
|
| 43 |
if _db_client is None:
|
| 44 |
-
raise RuntimeError(f"No database
|
| 45 |
-
|
| 46 |
return _db_client
|
| 47 |
|
| 48 |
|
| 49 |
def get_db_or_none():
|
| 50 |
-
"""Get database client or None (for health checks)"""
|
| 51 |
return _db_client
|
| 52 |
|
| 53 |
|
| 54 |
def get_db_error():
|
| 55 |
-
"""Get database initialization error if any"""
|
| 56 |
return _db_error
|
| 57 |
|
| 58 |
|
| 59 |
def get_storage_mode():
|
| 60 |
-
"""Get current storage mode: 'local'"""
|
| 61 |
return STORAGE_MODE
|
| 62 |
|
| 63 |
|
| 64 |
|
|
|
|
|
|
| 1 |
+
"""Database connection - Storage Bucket with Local fallback"""
|
| 2 |
import os
|
| 3 |
from config import Config
|
| 4 |
|
| 5 |
+
# Storage mode: 'storage_bucket' or 'local'
|
| 6 |
STORAGE_MODE = None
|
| 7 |
_db_client = None
|
| 8 |
_db_error: str = None
|
| 9 |
|
| 10 |
|
| 11 |
+
def _init_storage_bucket():
|
| 12 |
+
"""Try to initialize Supabase Storage Bucket DB"""
|
| 13 |
+
global STORAGE_MODE, _db_client, _db_error
|
| 14 |
+
|
| 15 |
+
supabase_url = Config.SUPABASE_URL
|
| 16 |
+
supabase_key = Config.SUPABASE_KEY
|
| 17 |
+
|
| 18 |
+
if not supabase_url or not supabase_key or supabase_key == '':
|
| 19 |
+
_db_error = "SUPABASE credentials not set"
|
| 20 |
+
print(f"[DB] {_db_error} - falling back to local storage")
|
| 21 |
+
return False
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
from supabase import create_client
|
| 25 |
+
from database.storage_bucket_db import StorageBucketDB
|
| 26 |
+
|
| 27 |
+
supabase_client = create_client(supabase_url, supabase_key)
|
| 28 |
+
_db_client = StorageBucketDB(supabase_client)
|
| 29 |
+
STORAGE_MODE = 'storage_bucket'
|
| 30 |
+
print("[DB] ✅ Using Supabase STORAGE BUCKET (analytics-data)")
|
| 31 |
+
return True
|
| 32 |
+
except Exception as e:
|
| 33 |
+
_db_error = f"Failed to init storage bucket: {str(e)}"
|
| 34 |
+
print(f"[DB ERROR] {_db_error}")
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
|
| 38 |
def _init_local():
|
| 39 |
"""Initialize local file storage"""
|
| 40 |
global STORAGE_MODE, _db_client, _db_error
|
|
|
|
| 43 |
from database.local_storage import get_local_storage
|
| 44 |
_db_client = get_local_storage()
|
| 45 |
STORAGE_MODE = 'local'
|
| 46 |
+
print("[DB] Using LOCAL FILE STORAGE (/tmp/analytics_data)")
|
| 47 |
return True
|
| 48 |
except Exception as e:
|
| 49 |
+
_db_error = f"Failed to init local storage: {str(e)}"
|
| 50 |
print(f"[DB ERROR] {_db_error}")
|
| 51 |
return False
|
| 52 |
|
| 53 |
|
| 54 |
def _init_db():
|
| 55 |
+
"""Initialize - tries Storage Bucket first, then local"""
|
| 56 |
+
if not _init_storage_bucket():
|
| 57 |
+
_init_local()
|
| 58 |
|
| 59 |
|
| 60 |
# Initialize on module load
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
def get_db():
|
| 65 |
+
"""Get database client"""
|
|
|
|
|
|
|
| 66 |
if _db_client is None:
|
| 67 |
_init_db()
|
|
|
|
| 68 |
if _db_client is None:
|
| 69 |
+
raise RuntimeError(f"No database: {_db_error}")
|
|
|
|
| 70 |
return _db_client
|
| 71 |
|
| 72 |
|
| 73 |
def get_db_or_none():
|
|
|
|
| 74 |
return _db_client
|
| 75 |
|
| 76 |
|
| 77 |
def get_db_error():
|
|
|
|
| 78 |
return _db_error
|
| 79 |
|
| 80 |
|
| 81 |
def get_storage_mode():
|
|
|
|
| 82 |
return STORAGE_MODE
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
+
|