ibraheem007 commited on
Commit
a6615e3
Β·
verified Β·
1 Parent(s): 6a7f44a

Update db/connection.py

Browse files
Files changed (1) hide show
  1. 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 'supabase' in st.secrets:
28
- secrets = st.secrets["supabase"]
29
- st_config = {
30
- 'user': secrets.get('user'),
31
- 'password': secrets.get('password'),
32
- 'host': secrets.get('host'),
33
- 'port': secrets.get('port', '6543'), # Transaction Pooler port
34
- 'dbname': secrets.get('dbname')
35
- }
36
- if all(st_config.values()):
37
- config_sources.append(('Streamlit Secrets', st_config))
38
- print("βœ… Found Streamlit database configuration")
39
- except (ImportError, AttributeError, KeyError):
 
 
40
  pass
41
 
42
  # 3. Local Development Environment Variables
43
- from dotenv import load_dotenv
44
- load_dotenv() # Load .env file for local development
 
 
 
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"), # Default to transaction pooler
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
- init_db()
 
 
 
 
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()