-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
31 lines (24 loc) · 781 Bytes
/
Copy pathdatabase.py
File metadata and controls
31 lines (24 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
# Load environment variables
DB_USER = os.getenv("DB_USER", "root")
DB_PASSWORD = os.getenv("DB_PASSWORD", "")
DB_HOST = os.getenv("DB_HOST", "db")
DB_NAME = os.getenv("DB_DATABASE", "payitdb")
DB_PORT = os.getenv("DB_PORT", "3306")
# Correct variable name
DATABASE_URL = f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
# Create engine
engine = create_engine(DATABASE_URL, echo=True)
# Session maker
Session = sessionmaker(bind=engine, autoflush=False, autocommit=False)
# Base class for models
Base = declarative_base()
# Dependency for FastAPI routes
def get_db():
db = Session()
try:
yield db
finally:
db.close()