MukeshKapoor25 commited on
Commit
00e18f2
·
1 Parent(s): 628320b

Refactor database connection to use pymssql; update session management and requirements

Browse files
Files changed (2) hide show
  1. app/db/session.py +20 -20
  2. requirements.txt +1 -0
app/db/session.py CHANGED
@@ -1,37 +1,37 @@
1
  # app/db/session.py
2
  import os
3
- import urllib.parse
4
  from sqlalchemy import create_engine
5
  from sqlalchemy.orm import sessionmaker
6
  from app.core.config import settings
7
 
 
8
  USER = settings.SQLSERVER_USER
9
  PWD = settings.SQLSERVER_PASSWORD
10
  HOST = settings.SQLSERVER_HOST
11
- PORT = settings.SQLSERVER_PORT or 31433 # <- default to 31433 if not set
12
  DB = settings.SQLSERVER_DB
13
 
14
- odbc_str = (
15
- "DRIVER={ODBC Driver 18 for SQL Server};"
16
- f"SERVER=tcp:{HOST},{PORT};"
17
- f"DATABASE={DB};"
18
- f"UID={USER};PWD={PWD};"
19
- "Encrypt=yes;"
20
- "TrustServerCertificate=yes;" # diagnostic: OK for dev. remove/flip in prod.
21
- "LoginTimeout=15;"
22
- )
23
- params = urllib.parse.quote_plus(odbc_str)
24
- DATABASE_URL = f"mssql+pyodbc:///?odbc_connect={params}"
25
-
26
- engine = create_engine(
27
- DATABASE_URL,
28
- pool_pre_ping=True,
29
- echo=False,
30
- connect_args={"timeout": 30},
31
- )
32
 
33
  SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
34
 
 
35
  def get_db():
36
  db = SessionLocal()
37
  try:
 
1
  # app/db/session.py
2
  import os
 
3
  from sqlalchemy import create_engine
4
  from sqlalchemy.orm import sessionmaker
5
  from app.core.config import settings
6
 
7
+ # Read connection settings from app configuration
8
  USER = settings.SQLSERVER_USER
9
  PWD = settings.SQLSERVER_PASSWORD
10
  HOST = settings.SQLSERVER_HOST
11
+ PORT = settings.SQLSERVER_PORT or 31433 # default port used by original project
12
  DB = settings.SQLSERVER_DB
13
 
14
+
15
+ def _build_pymssql_url(user: str, pwd: str, host: str, port: int, db: str) -> str:
16
+ """Build mssql+pymssql URL for SQLAlchemy."""
17
+ # pymssql connection string format: mssql+pymssql://<user>:<password>@<host>:<port>/<db>
18
+ # Note: passwords with special chars will be quoted by the URL constructor if needed.
19
+ return f"mssql+pymssql://{user}:{pwd}@{host}:{port}/{db}"
20
+
21
+
22
+ if not all([USER, PWD, HOST, DB]):
23
+ raise RuntimeError("Missing required SQL Server connection settings (SQLSERVER_USER, SQLSERVER_PASSWORD, SQLSERVER_HOST, SQLSERVER_DB)")
24
+
25
+ DATABASE_URL = _build_pymssql_url(USER, PWD, HOST, PORT, DB)
26
+
27
+ try:
28
+ engine = create_engine(DATABASE_URL, pool_pre_ping=True, echo=False)
29
+ except Exception as exc:
30
+ raise RuntimeError(f"Failed to create DB engine using pymssql: {exc}")
 
31
 
32
  SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
33
 
34
+
35
  def get_db():
36
  db = SessionLocal()
37
  try:
requirements.txt CHANGED
@@ -2,6 +2,7 @@ fastapi
2
  uvicorn[standard]
3
  sqlalchemy
4
  pyodbc
 
5
  python-dotenv
6
  passlib[bcrypt]
7
  python-jose
 
2
  uvicorn[standard]
3
  sqlalchemy
4
  pyodbc
5
+ pymssql
6
  python-dotenv
7
  passlib[bcrypt]
8
  python-jose