Skip to content

Commit

Permalink
Fix FastAPI transaction dep
Browse files Browse the repository at this point in the history
  • Loading branch information
sayanarijit committed Feb 15, 2024
1 parent 701bf99 commit 1bfe683
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion apphelpers/db/piccolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ async def connection_pool_lifespan(engine: PostgresEngine, **kwargs):
await engine.close_connection_pool()


dbtransaction_ctx = PostgresEngine.transaction
@asynccontextmanager
async def dbtransaction_ctx(engine: PostgresEngine, allow_nested=True):
async with engine.transaction(allow_nested=allow_nested):
yield


def dbtransaction(engine: PostgresEngine, allow_nested=True):
Expand Down
12 changes: 10 additions & 2 deletions apphelpers/rest/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ async def get_user_agent(request: Request):
user_agent = Depends(get_user_agent)


def dbtransaction(engine, allow_nested=True):
async def dependency():
async with dbtransaction_ctx(engine, allow_nested=allow_nested):
yield

return Depends(dependency)


class SecureRouter(APIRoute):
sessions = None

Expand Down Expand Up @@ -295,8 +303,8 @@ def enable_multi_site(self, site_identifier: str):
self.multi_site_enabled = True
self.site_identifier = site_identifier

def setup_db_transaction(self, db=None):
self.router.dependencies.append(Depends(dbtransaction_ctx(db)))
def setup_db_transaction(self, db):
self.router.dependencies.append(dbtransaction(db))

def setup_honeybadger_monitoring(self):
api_key = settings.HONEYBADGER_API_KEY
Expand Down

0 comments on commit 1bfe683

Please sign in to comment.