refactor: migrate services layer to sessionmaker pattern#34382
Open
xr843 wants to merge 6 commits intolanggenius:mainfrom
Open
refactor: migrate services layer to sessionmaker pattern#34382xr843 wants to merge 6 commits intolanggenius:mainfrom
xr843 wants to merge 6 commits intolanggenius:mainfrom
Conversation
Part of langgenius#24245. Replace manual Session(db.engine) + session.commit() with sessionmaker(db.engine).begin() context manager in api/services/.
Contributor
|
fix conflict |
Contributor
Pyrefly Diffbase → 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]
|
Contributor
Pyrefly DiffNo 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]>
Contributor
Pyrefly Diffbase → 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]>
Contributor
Pyrefly Diffbase → 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]
|
Contributor
Pyrefly Diffbase → 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]
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Part of #24245. Migrate
Session(db.engine)+ manualsession.commit()tosessionmaker(db.engine).begin()context manager in the services layer.Continues the migration after #34379 (service_api + inner_api controllers).
Changes
Session(db.engine)withsessionmaker(db.engine).begin()Session(db.engine).no_autoflushwithsessionmaker(db.engine, autoflush=False).begin()Session(db.engine, autoflush=False)withsessionmaker(db.engine, autoflush=False).begin()Session(db.engine, expire_on_commit=False)withsessionmaker(db.engine, expire_on_commit=False).begin()Session(db.engine) as session, session.begin()withsessionmaker(db.engine).begin()session.commit()calls (handled automatically by the context manager)session.rollback()calls (handled automatically on exception)Sessionimport where used as type annotationFiles modified (19 files in
api/services/)account_service.pyasync_workflow_service.pyclear_free_plan_tenant_expired_logs.pycredit_pool_service.pydataset_service.pydatasource_provider_service.pyoauth_server.pyplugin/plugin_auto_upgrade_service.pyplugin/plugin_migration.pyplugin/plugin_parameter_service.pyplugin/plugin_permission_service.pyplugin/plugin_service.pyrag_pipeline/rag_pipeline.pytools/builtin_tools_manage_service.pytrigger/app_trigger_service.pytrigger/trigger_provider_service.pytrigger/trigger_service.pytrigger/webhook_service.pyworkflow_service.pyCloses part of #24245