Skip to content
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

Fix adding engines to SQLAlchemyPanel from AsyncSession #48

Merged
merged 3 commits into from
May 4, 2024

Conversation

IgelSchnauze
Copy link
Contributor

Hi :з We use AsyncSession in our project and faced issues with SQLAlchemyPanel which does not support AsyncSession.
Moreover, there is no support for a Session with multiple binds.
In this PR, I have attempted to fix these issues.

Copy link

codecov bot commented Mar 22, 2024

Codecov Report

Attention: Patch coverage is 71.42857% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 95.49%. Comparing base (4311ef5) to head (9139dcb).

Files Patch % Lines
debug_toolbar/panels/sqlalchemy.py 71.42% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #48      +/-   ##
==========================================
- Coverage   95.73%   95.49%   -0.25%     
==========================================
  Files          44       44              
  Lines        1078     1087       +9     
==========================================
+ Hits         1032     1038       +6     
- Misses         46       49       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mongkok mongkok self-requested a review March 24, 2024 11:03
Copy link
Owner

@mongkok mongkok left a comment

Choose a reason for hiding this comment

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

Thanks for PR!

@@ -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)
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.

@@ -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.

@lzbht
Copy link

lzbht commented Apr 17, 2024

I can't wait to use this feature! When can I use it?

@mongkok mongkok merged commit 60fb7b1 into mongkok:main May 4, 2024
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants