25 lines
695 B
Python
25 lines
695 B
Python
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker, declarative_base, Session
|
|
from app.core.config import settings
|
|
import os
|
|
|
|
connect_args = {}
|
|
if settings.DATABASE_URL.startswith("sqlite"):
|
|
connect_args["check_same_thread"] = False
|
|
db_path = settings.DATABASE_URL.replace("sqlite:///", "")
|
|
parent = os.path.dirname(db_path)
|
|
if parent:
|
|
os.makedirs(parent, exist_ok=True)
|
|
|
|
engine = create_engine(settings.DATABASE_URL, connect_args=connect_args)
|
|
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
|
Base = declarative_base()
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|