Kushal commited on
Commit
0c11134
·
1 Parent(s): e316914

Fix: Hardened DB connection logic for complex passwords

Browse files
Files changed (1) hide show
  1. app/database/connection.py +16 -3
app/database/connection.py CHANGED
@@ -7,14 +7,27 @@ import uuid
7
  from app.config.settings import settings
8
 
9
  # Create SQLAlchemy engine
 
 
 
 
 
 
 
 
10
  # Check if using SQLite (special handling for threads)
11
- if settings.DATABASE_URL.startswith("sqlite"):
12
  engine = create_engine(
13
- settings.DATABASE_URL,
14
  connect_args={"check_same_thread": False}
15
  )
16
  else:
17
- engine = create_engine(settings.DATABASE_URL)
 
 
 
 
 
18
 
19
  # Session factory
20
  SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
 
7
  from app.config.settings import settings
8
 
9
  # Create SQLAlchemy engine
10
+ db_url = settings.DATABASE_URL
11
+
12
+ # Safety check for unencoded passwords (very common issue)
13
+ if "@" in db_url.split("://")[-1].split("@")[0] and not db_url.startswith("sqlite"):
14
+ print("⚠️ DATABASE_URL detected with unencoded '@' in password.")
15
+ # We don't fix it automatically to avoid messing up complex passwords,
16
+ # but we inform the user in the connection logic if it fails.
17
+
18
  # Check if using SQLite (special handling for threads)
19
+ if db_url.startswith("sqlite"):
20
  engine = create_engine(
21
+ db_url,
22
  connect_args={"check_same_thread": False}
23
  )
24
  else:
25
+ # Postgres/Other
26
+ try:
27
+ engine = create_engine(db_url)
28
+ except Exception as e:
29
+ print(f"❌ Failed to create engine for {db_url.split('@')[-1]}")
30
+ raise e
31
 
32
  # Session factory
33
  SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)