refactor: port api/services/auth/api_key_auth_service.py api/services/datasource_provider_service.py api/services/saved_message_service.py api/services/web_conversation_service.py to session#32003
Conversation
Summary of ChangesHello @asukaminato0721, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request implements a significant refactoring of database session management within several core API services. The primary goal is to standardize how database sessions are acquired and managed, moving away from a global Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors several service files to use the new session_factory for creating database sessions, replacing the direct use of db.session. This is a positive change that improves session management and consistency. The changes in api_key_auth_service.py, saved_message_service.py, and web_conversation_service.py are well-implemented. However, the refactoring for datasource_provider_service.py is incomplete, as only one method has been updated. For consistency, it would be beneficial to refactor the remaining methods in that file to use session_factory as well. I've also left a couple of minor suggestions to optimize read-only queries.
There was a problem hiding this comment.
Pull request overview
This PR refactors four service files from using Flask-SQLAlchemy's db.session to pure SQLAlchemy's session_factory.create_session() pattern. This is part of a larger migration effort to transition the codebase from Flask-SQLAlchemy to pure SQLAlchemy for better flexibility and control over session management.
Changes:
- Replaced
db.sessionwithsession_factory.create_session()context managers - Added explicit transaction boundaries using
session.begin()for write operations - Removed unused
dbimport fromapi_key_auth_service.py
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| api/services/auth/api_key_auth_service.py | Refactored all methods to use session_factory; removed db import |
| api/services/datasource_provider_service.py | Refactored remove_datasource_credentials method to use session_factory |
| api/services/saved_message_service.py | Refactored all methods to use session_factory; removed db import |
| api/services/web_conversation_service.py | Refactored pin and unpin methods to use session_factory; removed db import |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if saved_message: | ||
| return |
There was a problem hiding this comment.
The early return on line 53 occurs within a session.begin() transaction context. When exiting the context manager via return, an empty transaction is committed. While not incorrect, this is inefficient. Consider restructuring to avoid early returns within the transaction block, or move the transaction begin to after the early return check.
| if not saved_message: | ||
| return |
There was a problem hiding this comment.
The early return on line 83 occurs within a session.begin() transaction context. When exiting the context manager via return, an empty transaction is committed. While not incorrect, this is inefficient. Consider restructuring to avoid early returns within the transaction block, or move the transaction begin to after the early return check.
| if pinned_conversation: | ||
| return |
There was a problem hiding this comment.
The early return on line 79 occurs within a session.begin() transaction context. When exiting the context manager via return, an empty transaction is committed. While not incorrect, this is inefficient. Consider restructuring to avoid early returns within the transaction block, or move the transaction begin to after the early return check.
| if not pinned_conversation: | ||
| return |
There was a problem hiding this comment.
The early return on line 111 occurs within a session.begin() transaction context. When exiting the context manager via return, an empty transaction is committed. While not incorrect, this is inefficient. Consider restructuring to avoid early returns within the transaction block, or move the transaction begin to after the early return check.
| with session_factory.create_session() as session: | ||
| data_source_api_key_bindings = ( | ||
| session.query(DataSourceApiKeyAuthBinding) | ||
| .where( | ||
| DataSourceApiKeyAuthBinding.tenant_id == tenant_id, | ||
| DataSourceApiKeyAuthBinding.category == category, | ||
| DataSourceApiKeyAuthBinding.provider == provider, | ||
| DataSourceApiKeyAuthBinding.disabled.is_(False), | ||
| ) | ||
| .first() | ||
| ) | ||
| .first() | ||
| ) | ||
| if not data_source_api_key_bindings: | ||
| return None | ||
| if not data_source_api_key_bindings.credentials: | ||
| return None | ||
| credentials = json.loads(data_source_api_key_bindings.credentials) | ||
| return credentials | ||
| if not data_source_api_key_bindings: | ||
| return None | ||
| if not data_source_api_key_bindings.credentials: | ||
| return None | ||
| credentials = json.loads(data_source_api_key_bindings.credentials) | ||
| return credentials |
There was a problem hiding this comment.
The data_source_api_key_bindings object accessed on lines 50-54 is detached from the session (since it's accessed after being queried within a session context that will exit). While the scalar attributes like credentials are already loaded and accessible, following the established pattern in the codebase (e.g., api/core/tools/workflow_as_tool/tool.py:226), the object should be explicitly expunged using session.expunge(data_source_api_key_bindings) before being used outside the session context. This makes it clear that the object is intended to be used as a detached object.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| SavedMessage.app_id == app_model.id, | ||
| SavedMessage.created_by_role == ("account" if isinstance(user, Account) else "end_user"), | ||
| SavedMessage.created_by == user.id, | ||
| with session_factory.create_session() as session: |
There was a problem hiding this comment.
The pagination_by_last_id method performs only read operations (querying SavedMessage), so it should not use session.begin(). The session.begin() context manager is only needed for write operations (insert, update, delete).
According to the codebase pattern (e.g., api/controllers/console/admin.py:118, api/controllers/console/admin.py:194), read-only queries should use: with session_factory.create_session() as session: without the session.begin() clause.
Important
Fixes #<issue number>.Summary
part of #24115
Screenshots
Checklist
make lintandmake type-check(backend) andcd web && npx lint-staged(frontend) to appease the lint gods