Skip to content

refactor: migrate services layer to sessionmaker pattern#34382

Open
xr843 wants to merge 6 commits intolanggenius:mainfrom
xr843:refactor/sessionmaker-services
Open

refactor: migrate services layer to sessionmaker pattern#34382
xr843 wants to merge 6 commits intolanggenius:mainfrom
xr843:refactor/sessionmaker-services

Conversation

@xr843
Copy link
Copy Markdown
Contributor

@xr843 xr843 commented Apr 1, 2026

Summary

Part of #24245. Migrate Session(db.engine) + manual session.commit() to sessionmaker(db.engine).begin() context manager in the services layer.

Continues the migration after #34379 (service_api + inner_api controllers).

Changes

  • Replace Session(db.engine) with sessionmaker(db.engine).begin()
  • Replace Session(db.engine).no_autoflush with sessionmaker(db.engine, autoflush=False).begin()
  • Replace Session(db.engine, autoflush=False) with sessionmaker(db.engine, autoflush=False).begin()
  • Replace Session(db.engine, expire_on_commit=False) with sessionmaker(db.engine, expire_on_commit=False).begin()
  • Replace Session(db.engine) as session, session.begin() with sessionmaker(db.engine).begin()
  • Remove manual session.commit() calls (handled automatically by the context manager)
  • Remove manual session.rollback() calls (handled automatically on exception)
  • Preserve Session import where used as type annotation

Files modified (19 files in api/services/)

  • account_service.py
  • async_workflow_service.py
  • clear_free_plan_tenant_expired_logs.py
  • credit_pool_service.py
  • dataset_service.py
  • datasource_provider_service.py
  • oauth_server.py
  • plugin/plugin_auto_upgrade_service.py
  • plugin/plugin_migration.py
  • plugin/plugin_parameter_service.py
  • plugin/plugin_permission_service.py
  • plugin/plugin_service.py
  • rag_pipeline/rag_pipeline.py
  • tools/builtin_tools_manage_service.py
  • trigger/app_trigger_service.py
  • trigger/trigger_provider_service.py
  • trigger/trigger_service.py
  • trigger/webhook_service.py
  • workflow_service.py

Closes part of #24245

Part of langgenius#24245. Replace manual Session(db.engine) + session.commit()
with sessionmaker(db.engine).begin() context manager in api/services/.
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. refactor labels Apr 1, 2026
@asukaminato0721
Copy link
Copy Markdown
Contributor

fix conflict

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-01 14:36:19.908060152 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-01 14:36:08.255981191 +0000
@@ -365,7 +365,7 @@
 ERROR No matching overload found for function `redis.client.Redis.__init__` called with arguments: (host=int | str | Unknown, port=int | str | Unknown, password=int | str | Unknown | None, db=int, ssl=bool, ssl_ca_certs=str | None, ssl_cert_reqs=Any | None, ssl_certfile=Any | None, ssl_keyfile=Any | None, socket_timeout=Literal[5], socket_connect_timeout=Literal[5], health_check_interval=Literal[30]) [no-matching-overload]
   --> schedule/queue_monitor_task.py:14:21
 ERROR Object of class `Tenant` has no attribute `role` [missing-attribute]
-    --> services/account_service.py:1131:13
+    --> services/account_service.py:1132:13
 ERROR Runtime checkable protocol `Generator` has an unsafe overlap with type `Iterable[bytes]` [unsafe-overlap]
    --> services/audio_service.py:143:41
 ERROR No matching overload found for function `flask.helpers.stream_with_context` called with arguments: (Generator[bytes]) [no-matching-overload]
@@ -399,7 +399,7 @@
 ERROR Returned type `dict[Unknown, Unknown] | None` is not assignable to declared return type `dict[str, Any]` [bad-return]
    --> services/external_knowledge_service.py:232:16
 ERROR `handled_tenant_count` was assigned in the current scope before the nonlocal declaration [unknown-name]
-  --> services/plugin/plugin_migration.py:82:34
+  --> services/plugin/plugin_migration.py:83:34
 ERROR Object of class `NoneType` has no attribute `id` [missing-attribute]
    --> services/summary_index_service.py:287:29
 ERROR Object of class `NoneType` has no attribute `summary_index_node_id` [missing-attribute]

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

Pyrefly Diff

No changes detected.

Update test mocks to match the refactored code that uses
sessionmaker(db.engine).begin() instead of Session(db.engine).
Remove commit/rollback assertions as they are no longer needed
with the sessionmaker.begin() context manager pattern.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@xr843 xr843 requested a review from laipz8200 as a code owner April 1, 2026 23:38
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-01 23:39:23.403012288 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-01 23:39:12.723905462 +0000
@@ -7118,9 +7118,9 @@
 ERROR Argument `dict[str, str]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.workflow_service.WorkflowService._build_human_input_node` [bad-argument-type]
     --> tests/unit_tests/services/test_workflow_service.py:2763:65
 ERROR Argument `Literal['api_key']` is not assignable to parameter `credential_type` with type `CredentialType` in function `services.tools.builtin_tools_manage_service.BuiltinToolManageService.list_builtin_provider_credentials_schema` [bad-argument-type]
-  --> tests/unit_tests/services/tools/test_builtin_tools_manage_service.py:84:89
+  --> tests/unit_tests/services/tools/test_builtin_tools_manage_service.py:83:89
 ERROR No matching overload found for function `_pytest.raises.raises` called with arguments: (tuple[type[ValueError], type[Exception]], match=Literal['not found|Provider']) [no-matching-overload]
-   --> tests/unit_tests/services/tools/test_builtin_tools_manage_service.py:315:27
+   --> tests/unit_tests/services/tools/test_builtin_tools_manage_service.py:311:27
 ERROR Object of class `Mapping` has no attribute `startswith` [missing-attribute]
    --> tests/unit_tests/services/tools/test_tools_transform_service.py:464:16
 ERROR Could not import `DatasetDocument` from `models.dataset` [missing-module-attribute]

…tern

The service was refactored from Session to sessionmaker(db.engine).begin()
context manager pattern. Update mock_session fixture to patch sessionmaker
instead of Session, and remove commit assertions since begin() auto-commits.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-02 04:33:55.599731799 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-02 04:33:44.383712936 +0000
@@ -7118,9 +7118,9 @@
 ERROR Argument `dict[str, str]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.workflow_service.WorkflowService._build_human_input_node` [bad-argument-type]
     --> tests/unit_tests/services/test_workflow_service.py:2763:65
 ERROR Argument `Literal['api_key']` is not assignable to parameter `credential_type` with type `CredentialType` in function `services.tools.builtin_tools_manage_service.BuiltinToolManageService.list_builtin_provider_credentials_schema` [bad-argument-type]
-  --> tests/unit_tests/services/tools/test_builtin_tools_manage_service.py:84:89
+  --> tests/unit_tests/services/tools/test_builtin_tools_manage_service.py:83:89
 ERROR No matching overload found for function `_pytest.raises.raises` called with arguments: (tuple[type[ValueError], type[Exception]], match=Literal['not found|Provider']) [no-matching-overload]
-   --> tests/unit_tests/services/tools/test_builtin_tools_manage_service.py:315:27
+   --> tests/unit_tests/services/tools/test_builtin_tools_manage_service.py:311:27
 ERROR Object of class `Mapping` has no attribute `startswith` [missing-attribute]
    --> tests/unit_tests/services/tools/test_tools_transform_service.py:464:16
 ERROR Could not import `DatasetDocument` from `models.dataset` [missing-module-attribute]

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-02 04:35:59.301095251 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-02 04:35:48.442073595 +0000
@@ -7118,9 +7118,9 @@
 ERROR Argument `dict[str, str]` is not assignable to parameter `node_config` with type `NodeConfigDict` in function `services.workflow_service.WorkflowService._build_human_input_node` [bad-argument-type]
     --> tests/unit_tests/services/test_workflow_service.py:2763:65
 ERROR Argument `Literal['api_key']` is not assignable to parameter `credential_type` with type `CredentialType` in function `services.tools.builtin_tools_manage_service.BuiltinToolManageService.list_builtin_provider_credentials_schema` [bad-argument-type]
-  --> tests/unit_tests/services/tools/test_builtin_tools_manage_service.py:84:89
+  --> tests/unit_tests/services/tools/test_builtin_tools_manage_service.py:83:89
 ERROR No matching overload found for function `_pytest.raises.raises` called with arguments: (tuple[type[ValueError], type[Exception]], match=Literal['not found|Provider']) [no-matching-overload]
-   --> tests/unit_tests/services/tools/test_builtin_tools_manage_service.py:315:27
+   --> tests/unit_tests/services/tools/test_builtin_tools_manage_service.py:311:27
 ERROR Object of class `Mapping` has no attribute `startswith` [missing-attribute]
    --> tests/unit_tests/services/tools/test_tools_transform_service.py:464:16
 ERROR Could not import `DatasetDocument` from `models.dataset` [missing-module-attribute]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants