Spaces:
Paused
Paused
Commit ·
00e18f2
1
Parent(s): 628320b
Refactor database connection to use pymssql; update session management and requirements
Browse files- app/db/session.py +20 -20
- 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
|
| 12 |
DB = settings.SQLSERVER_DB
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
"
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
pool_pre_ping=True,
|
| 29 |
-
|
| 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
|