Spaces:
Paused
Paused
File size: 778 Bytes
ec17af3 | 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 | from sqlalchemy import create_engine
from sqlalchemy.engine.url import make_url
from sqlalchemy.orm import declarative_base, sessionmaker
from app.config import settings
normalized_db_url = settings.database_url
if normalized_db_url.startswith("postgres://"):
normalized_db_url = normalized_db_url.replace("postgres://", "postgresql://", 1)
url = make_url(normalized_db_url)
connect_args = {}
if url.drivername.startswith("sqlite"):
connect_args = {"check_same_thread": False}
engine = create_engine(normalized_db_url, pool_pre_ping=True, connect_args=connect_args)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
|