31 lines
640 B
Python
31 lines
640 B
Python
from sqlalchemy import URL, create_engine
|
|
from sqlalchemy.orm import declarative_base, sessionmaker
|
|
|
|
from app.config import settings
|
|
|
|
|
|
DATABASE_URL = URL.create(
|
|
"mysql+pymysql",
|
|
username=settings.db_user,
|
|
password=settings.db_password,
|
|
host=settings.db_host,
|
|
port=int(settings.db_port),
|
|
database=settings.db_name,
|
|
)
|
|
|
|
engine = create_engine(
|
|
DATABASE_URL,
|
|
pool_pre_ping=True,
|
|
future=True,
|
|
)
|
|
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, future=True)
|
|
Base = declarative_base()
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|