Skip to content

refactor(services): migrate builtin_tools_manage_service to SQLAlchemy 2.0 select() API#34973

Merged
asukaminato0721 merged 3 commits intolanggenius:mainfrom
wdeveloper16:refactor/sqlalchemy2-builtin-tools-manage-service
Apr 12, 2026
Merged

refactor(services): migrate builtin_tools_manage_service to SQLAlchemy 2.0 select() API#34973
asukaminato0721 merged 3 commits intolanggenius:mainfrom
wdeveloper16:refactor/sqlalchemy2-builtin-tools-manage-service

Conversation

@wdeveloper16
Copy link
Copy Markdown
Contributor

@wdeveloper16 wdeveloper16 commented Apr 11, 2026

part of #22668

Files:

  • api/services/tools/builtin_tools_manage_service.py

Description:
Migrates 16 occurrences of legacy session.query() in the builtin tool provider
management service. Notable changes:

  • Bulk is_default clear via session.execute(update(...).values(is_default=False).execution_options(synchronize_session=False))
  • Boolean column filter: BuiltinToolProvider.is_default == True.is_(True)
  • Count check: session.query(...).count()session.scalar(select(func.count(...)))
  • Added delete, func, update to SQLAlchemy imports

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. refactor labels Apr 11, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@wdeveloper16 wdeveloper16 requested a review from laipz8200 as a code owner April 11, 2026 21:47
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@asukaminato0721 asukaminato0721 added this pull request to the merge queue Apr 12, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Apr 12, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Migrates BuiltinToolManageService database access from legacy session.query() usage to SQLAlchemy 2.0 select() / session.scalar(s) APIs to improve typing and align with modern ORM patterns.

Changes:

  • Replaced session.query(...).first()/count()/update()/delete() patterns with select(), session.scalar(s), session.execute(update/delete(...)), and func.count(...).
  • Updated boolean filtering to use .is_(True) and introduced bulk update with synchronize_session=False.
  • Adjusted unit tests to mock session.scalar() / session.execute() instead of session.query() chains.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
api/services/tools/builtin_tools_manage_service.py Refactors ORM queries/updates/deletes to SQLAlchemy 2.0 select() style throughout the builtin tools management service.
api/tests/unit_tests/services/tools/test_builtin_tools_manage_service.py Updates unit tests’ session mocks to match the new scalar() / execute()-based data access patterns.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -274,7 +274,7 @@ def test_returns_user_client_params_when_exists(
mock_create_enc.return_value = (mock_encrypter, MagicMock())

user_client = MagicMock(oauth_params='{"encrypted": "data"}')
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

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

In this test, ToolOAuthTenantClient.oauth_params is a property that returns a dict parsed from encrypted_oauth_params (see api/models/tools.py), but the mock sets oauth_params to a JSON string. This makes the test less representative and could hide type/usage issues. Consider setting oauth_params to a dict (or mocking encrypted_oauth_params and oauth_params consistently with the model).

Suggested change
user_client = MagicMock(oauth_params='{"encrypted": "data"}')
user_client = MagicMock(oauth_params={"encrypted": "data"})

Copilot uses AI. Check for mistakes.
Merged via the queue into langgenius:main with commit 4ef67fe Apr 12, 2026
32 checks passed
HanqingZ pushed a commit to HanqingZ/dify that referenced this pull request Apr 23, 2026
…y 2.0 select() API (langgenius#34973)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer refactor size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants