forked from sr2/cloud-api
feat: db dependency upgrade
No longer using .begin() and context manager. This means commits must be explicit (already were) but also, it allows for sanity checks within routes after commits.
This commit is contained in:
parent
83a24a91f4
commit
7b3ee9d5fa
1 changed files with 9 additions and 6 deletions
|
|
@ -17,13 +17,16 @@ engine = create_engine(SQLALCHEMY_DATABASE_URI.get_secret_value())
|
|||
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
|
||||
def get_db():
|
||||
with SessionLocal.begin() as db:
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.rollback() # Anything not explicitly commited is rolled back
|
||||
db.close()
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
except:
|
||||
db.rollback()
|
||||
raise
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
db_dependency = Annotated[Session, Depends(get_db)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue