38 lines
905 B
Python
38 lines
905 B
Python
from __future__ import annotations
|
|
|
|
from contextlib import contextmanager
|
|
from typing import Iterator
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import declarative_base, sessionmaker, Session
|
|
|
|
from .config import settings
|
|
|
|
|
|
SYNC_DATABASE_URL = settings.sync_database_url or settings.database_url
|
|
|
|
engine = create_engine(SYNC_DATABASE_URL, future=True)
|
|
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, expire_on_commit=False)
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
def init_db() -> None:
|
|
"""Create all tables in the database."""
|
|
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
|
|
@contextmanager
|
|
def db_session() -> Iterator[Session]:
|
|
"""Provide a transactional scope around a series of operations."""
|
|
|
|
session: Session = SessionLocal()
|
|
try:
|
|
yield session
|
|
except Exception:
|
|
session.rollback()
|
|
raise
|
|
finally:
|
|
session.close()
|