61 lines
1.5 KiB
Python
61 lines
1.5 KiB
Python
|
|
"""SQLAlchemy 引擎与会话。"""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from contextlib import contextmanager
|
||
|
|
from typing import Generator
|
||
|
|
|
||
|
|
from sqlalchemy import create_engine, event, text
|
||
|
|
from sqlalchemy.orm import Session, declarative_base, sessionmaker
|
||
|
|
|
||
|
|
from as_platform.config import DATABASE_URL, IS_POSTGRES, IS_SQLITE
|
||
|
|
|
||
|
|
connect_args: dict = {}
|
||
|
|
engine_kwargs: dict = {"future": True}
|
||
|
|
|
||
|
|
if IS_SQLITE:
|
||
|
|
connect_args["check_same_thread"] = False
|
||
|
|
elif IS_POSTGRES:
|
||
|
|
engine_kwargs.update(pool_pre_ping=True, pool_size=5, max_overflow=10)
|
||
|
|
|
||
|
|
engine = create_engine(DATABASE_URL, connect_args=connect_args, **engine_kwargs)
|
||
|
|
SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False, future=True)
|
||
|
|
Base = declarative_base()
|
||
|
|
|
||
|
|
|
||
|
|
@event.listens_for(engine, "connect")
|
||
|
|
def _on_connect(dbapi_conn, _):
|
||
|
|
if IS_SQLITE:
|
||
|
|
cursor = dbapi_conn.cursor()
|
||
|
|
cursor.execute("PRAGMA foreign_keys=ON")
|
||
|
|
cursor.close()
|
||
|
|
|
||
|
|
|
||
|
|
def check_connection() -> bool:
|
||
|
|
try:
|
||
|
|
with engine.connect() as conn:
|
||
|
|
conn.execute(text("SELECT 1"))
|
||
|
|
return True
|
||
|
|
except Exception:
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
@contextmanager
|
||
|
|
def session_scope() -> Generator[Session, None, None]:
|
||
|
|
db = SessionLocal()
|
||
|
|
try:
|
||
|
|
yield db
|
||
|
|
db.commit()
|
||
|
|
except Exception:
|
||
|
|
db.rollback()
|
||
|
|
raise
|
||
|
|
finally:
|
||
|
|
db.close()
|
||
|
|
|
||
|
|
|
||
|
|
def get_db() -> Generator[Session, None, None]:
|
||
|
|
db = SessionLocal()
|
||
|
|
try:
|
||
|
|
yield db
|
||
|
|
finally:
|
||
|
|
db.close()
|