mirror of
https://github.com/maxdorninger/MediaManager.git
synced 2026-04-18 03:54:03 +02:00
small fix
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import logging
|
||||
from contextvars import ContextVar
|
||||
from typing import Annotated, Any, Generator
|
||||
|
||||
from fastapi import Depends
|
||||
from sqlmodel import SQLModel, Session, create_engine
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session, declarative_base, sessionmaker
|
||||
|
||||
from database.config import DbConfig
|
||||
|
||||
@@ -13,15 +15,28 @@ db_url = "postgresql+psycopg" + "://" + config.USER + ":" + config.PASSWORD + "@
|
||||
config.PORT) + "/" + config.DBNAME
|
||||
|
||||
engine = create_engine(db_url, echo=False)
|
||||
Base = declarative_base()
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
|
||||
|
||||
def init_db() -> None:
|
||||
SQLModel.metadata.create_all(engine)
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
|
||||
def get_session() -> Generator[Session, Any, None]:
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
log.critical(f"error occurred: {e}")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
db_session: ContextVar[Session] = ContextVar('db_session')
|
||||
|
||||
|
||||
DbSessionDependency = Annotated[Session, Depends(get_session)]
|
||||
|
||||
Reference in New Issue
Block a user