-
Notifications
You must be signed in to change notification settings - Fork 17
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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]): | ||
if isinstance(bind, Connection): | ||
self.engines.add(bind.engine) | ||
else: | ||
self.engines.add(bind) | ||
|
||
route = request["route"] | ||
|
||
if hasattr(route, "dependant"): | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need getattr, |
||
if isinstance(value, Session): | ||
bind = value.get_bind() | ||
|
||
if isinstance(bind, Connection): | ||
self.engines.add(bind.engine) | ||
binds = getattr(value, "_Session__binds", None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need getattr, |
||
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) | ||
|
There was a problem hiding this comment.
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]):
, usefrom __future__ import annotations
import to provide Python 3.8 support.