Skip to content

Fix adding engines to SQLAlchemyPanel from AsyncSession #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions debug_toolbar/panels/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from fastapi.dependencies.utils import solve_dependencies
from sqlalchemy import event
from sqlalchemy.engine import Connection, Engine, ExecutionContext
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Session

from debug_toolbar.panels.sql import SQLPanel
Expand Down Expand Up @@ -40,7 +41,13 @@ def after_execute(self, context: ExecutionContext, **kwargs: t.Any) -> None:
}
self.add_query(str(context.engine.url), query)

async def add_engines(self, request: Request):
async def add_engines(self, request: Request): # noqa: C901
def add_bind_to_engines(bind: t.Union[Connection, Engine]):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add a class method add_bind(self, bind: Connection | Engine]):, use from __future__ import annotations import to provide Python 3.8 support.

if isinstance(bind, Connection):
self.engines.add(bind.engine)
else:
self.engines.add(bind)

route = request["route"]

if hasattr(route, "dependant"):
Expand All @@ -55,13 +62,16 @@ async def add_engines(self, request: Request):
pass
else:
for value in solved_result[0].values():
if isinstance(value, AsyncSession):
value = getattr(value, "sync_session", None)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need getattr, value = value.sync_session is okay.

if isinstance(value, Session):
bind = value.get_bind()

if isinstance(bind, Connection):
self.engines.add(bind.engine)
binds = getattr(value, "_Session__binds", None)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need getattr, binds = value._Session__binds is okay, default value is an empty dict.
Try value.get_bind() first and look for _Session__binds in case of UnboundExecutionError exception.

if binds:
for bind in binds.values():
add_bind_to_engines(bind)
else:
self.engines.add(bind)
bind = value.get_bind()
add_bind_to_engines(bind)

async def process_request(self, request: Request) -> Response:
await self.add_engines(request)
Expand Down