Spaces:
Running
Running
Update db/connection.py
Browse files- db/connection.py +29 -19
db/connection.py
CHANGED
|
@@ -21,33 +21,39 @@ def get_database_config():
|
|
| 21 |
config_sources.append(('HuggingFace Secrets', hf_config))
|
| 22 |
print("β
Found HuggingFace database configuration")
|
| 23 |
|
| 24 |
-
# 2. Streamlit Secrets (For Streamlit Cloud)
|
| 25 |
try:
|
|
|
|
| 26 |
import streamlit as st
|
| 27 |
-
if hasattr(st, 'secrets') and
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
pass
|
| 41 |
|
| 42 |
# 3. Local Development Environment Variables
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
local_config = {
|
| 47 |
'user': os.getenv("DB_USER") or os.getenv("user"),
|
| 48 |
'password': os.getenv("DB_PASSWORD") or os.getenv("password"),
|
| 49 |
'host': os.getenv("DB_HOST") or os.getenv("host"),
|
| 50 |
-
'port': os.getenv("DB_PORT") or os.getenv("port", "6543"),
|
| 51 |
'dbname': os.getenv("DB_NAME") or os.getenv("dbname")
|
| 52 |
}
|
| 53 |
|
|
@@ -209,8 +215,12 @@ def get_database_status():
|
|
| 209 |
except Exception as e:
|
| 210 |
return {"status": "error", "message": str(e)}
|
| 211 |
|
| 212 |
-
# Initialize database on import
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
# Export status
|
| 216 |
database_status = get_database_status()
|
|
|
|
| 21 |
config_sources.append(('HuggingFace Secrets', hf_config))
|
| 22 |
print("β
Found HuggingFace database configuration")
|
| 23 |
|
| 24 |
+
# 2. Streamlit Secrets (For Streamlit Cloud) - FIXED: Better error handling
|
| 25 |
try:
|
| 26 |
+
# Only import streamlit if we're actually in Streamlit environment
|
| 27 |
import streamlit as st
|
| 28 |
+
if hasattr(st, 'secrets') and st.secrets:
|
| 29 |
+
if 'supabase' in st.secrets:
|
| 30 |
+
secrets = st.secrets["supabase"]
|
| 31 |
+
st_config = {
|
| 32 |
+
'user': secrets.get('user'),
|
| 33 |
+
'password': secrets.get('password'),
|
| 34 |
+
'host': secrets.get('host'),
|
| 35 |
+
'port': secrets.get('port', '6543'),
|
| 36 |
+
'dbname': secrets.get('dbname')
|
| 37 |
+
}
|
| 38 |
+
if all(st_config.values()):
|
| 39 |
+
config_sources.append(('Streamlit Secrets', st_config))
|
| 40 |
+
print("β
Found Streamlit database configuration")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
# Silently continue if not in Streamlit environment
|
| 43 |
pass
|
| 44 |
|
| 45 |
# 3. Local Development Environment Variables
|
| 46 |
+
try:
|
| 47 |
+
from dotenv import load_dotenv
|
| 48 |
+
load_dotenv() # Load .env file for local development
|
| 49 |
+
except ImportError:
|
| 50 |
+
pass # dotenv not available
|
| 51 |
|
| 52 |
local_config = {
|
| 53 |
'user': os.getenv("DB_USER") or os.getenv("user"),
|
| 54 |
'password': os.getenv("DB_PASSWORD") or os.getenv("password"),
|
| 55 |
'host': os.getenv("DB_HOST") or os.getenv("host"),
|
| 56 |
+
'port': os.getenv("DB_PORT") or os.getenv("port", "6543"),
|
| 57 |
'dbname': os.getenv("DB_NAME") or os.getenv("dbname")
|
| 58 |
}
|
| 59 |
|
|
|
|
| 215 |
except Exception as e:
|
| 216 |
return {"status": "error", "message": str(e)}
|
| 217 |
|
| 218 |
+
# Initialize database on import with error handling
|
| 219 |
+
try:
|
| 220 |
+
init_db()
|
| 221 |
+
except Exception as e:
|
| 222 |
+
print(f"β οΈ Database initialization failed: {e}")
|
| 223 |
+
print("π‘ App will run in database-less mode")
|
| 224 |
|
| 225 |
# Export status
|
| 226 |
database_status = get_database_status()
|