Skip to content

refactor: pass db.session explicitly in billing and credential_permission services (#37403) - #37716

Closed
EvanYao826 wants to merge 2 commits into
langgenius:mainfrom
EvanYao826:fix/db-session-injection-37403
Closed

EvanYao826 wants to merge 2 commits into
langgenius:mainfrom
EvanYao826:fix/db-session-injection-37403

Conversation

@EvanYao826

Copy link
Copy Markdown
Contributor

Summary

Migrate db.session usage to explicit session parameter in 2 service files, following the pattern established in #37402.

Changes

api/services/billing_service.py:

  • is_tenant_owner_or_admin(): Added session: Session as first parameter
  • Replaced db.session.scalar → session.scalar
  • Removed from extensions.ext_database import db

api/services/credential_permission_service.py:

  • get_partial_member_list(): Added session: Session as first parameter
  • Replaced db.session.scalars → session.scalars
  • Removed from extensions.ext_database import db

Callers updated (3 files):

  • api/controllers/console/billing/billing.py — 2 call sites
  • api/controllers/console/workspace/model_providers.py — 1 call site
  • api/services/tools/builtin_tools_manage_service.py — 1 call site

Tests updated (3 files):

  • api/tests/unit_tests/services/test_billing_service.py
  • api/tests/unit_tests/services/test_credential_permission_service.py
  • api/tests/test_containers_integration_tests/services/test_billing_service.py

Pattern

# Before
def is_tenant_owner_or_admin(user):
    account = db.session.scalar(select(Account)...)

# After
def is_tenant_owner_or_admin(session: Session, user):
    account = session.scalar(select(Account)...)

All 101 tests pass. Closes #37403 (partial).

Migrate db.session to explicit session parameter in:
- billing_service.py (is_tenant_owner_or_admin)
- credential_permission_service.py (get_partial_member_list)
- agent_app_sandbox_service.py (no db.session usage found, skipped)

Follows pattern from langgenius#37402.
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. refactor labels Jun 22, 2026
@github-actions

github-actions Bot commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-06-30 02:20:49.935207310 +0000
+++ /tmp/pyrefly_pr.txt	2026-06-30 02:20:36.335177373 +0000
@@ -1,3 +1,9 @@
+ERROR Argument `scoped_session[flask_sqlalchemy.session.Session]` is not assignable to parameter `session` with type `sqlalchemy.orm.session.Session` in function `services.billing_service.BillingService.is_tenant_owner_or_admin` [bad-argument-type]
+  --> controllers/console/billing/billing.py:54:49
+ERROR Argument `scoped_session[flask_sqlalchemy.session.Session]` is not assignable to parameter `session` with type `sqlalchemy.orm.session.Session` in function `services.billing_service.BillingService.is_tenant_owner_or_admin` [bad-argument-type]
+  --> controllers/console/billing/billing.py:68:49
+ERROR Argument `scoped_session[flask_sqlalchemy.session.Session]` is not assignable to parameter `session` with type `sqlalchemy.orm.session.Session` in function `services.billing_service.BillingService.is_tenant_owner_or_admin` [bad-argument-type]
+   --> controllers/console/workspace/model_providers.py:356:49
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `dify_vdb_alibabacloud_mysql.alibabacloud_mysql_vector.AlibabaCloudMySQLVectorFactory.init_vector` [bad-argument-type]
   --> providers/vdb/vdb-alibabacloud-mysql/tests/unit_tests/test_alibabacloud_mysql_factory.py:42:38
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `dify_vdb_alibabacloud_mysql.alibabacloud_mysql_vector.AlibabaCloudMySQLVectorFactory.init_vector` [bad-argument-type]
@@ -460,6 +466,8 @@
    --> providers/vdb/vdb-weaviate/tests/unit_tests/test_weaviate_vector.py:891:42
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `dify_vdb_weaviate.weaviate_vector.WeaviateVectorFactory.init_vector` [bad-argument-type]
    --> providers/vdb/vdb-weaviate/tests/unit_tests/test_weaviate_vector.py:920:42
+ERROR Argument `scoped_session[flask_sqlalchemy.session.Session]` is not assignable to parameter `session` with type `sqlalchemy.orm.session.Session` in function `services.credential_permission_service.CredentialPermissionService.get_partial_member_list` [bad-argument-type]
+   --> services/tools/builtin_tools_manage_service.py:430:29
 ERROR Object of class `FromClause` has no attribute `metadata` [missing-attribute]
   --> tests/helpers/legacy_model_type_migration.py:64:16
 ERROR Object of class `FromClause` has no attribute `metadata` [missing-attribute]
@@ -6949,14 +6957,56 @@
    --> tests/unit_tests/services/test_async_workflow_service.py:289:26
 ERROR Argument `SimpleNamespace` is not assignable to parameter `user` with type `Account | EndUser` in function `services.async_workflow_service.AsyncWorkflowService.reinvoke_trigger` [bad-argument-type]
    --> tests/unit_tests/services/test_async_workflow_service.py:322:22
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1120:1
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1120:3
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1120:5
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1120:7
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1120:8
+ERROR Parse error: Unexpected indentation [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1121:1
+ERROR Could not find name `mock_db_session` [unknown-name]
+    --> tests/unit_tests/services/test_billing_service.py:1121:9
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1122:1
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1122:3
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1122:5
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1122:7
+ERROR Could not find name `upstream` [unknown-name]
+    --> tests/unit_tests/services/test_billing_service.py:1122:9
+ERROR Could not find name `main` [unknown-name]
+    --> tests/unit_tests/services/test_billing_service.py:1122:18
+ERROR Parse error: Unexpected indentation [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1124:1
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1138:1
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1138:3
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1138:5
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1138:7
+ERROR Could not find name `HEAD` [unknown-name]
+    --> tests/unit_tests/services/test_billing_service.py:1138:9
+ERROR Parse error: Unexpected indentation [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1140:1
+ERROR Parse error: Expected a statement [parse-error]
+    --> tests/unit_tests/services/test_billing_service.py:1174:1
 ERROR Argument `BillingInfo` is not assignable to parameter `result` with type `dict[Unknown, Unknown]` in function `TestBillingServiceSubscriptionInfoDataType._assert_billing_info_types` [bad-argument-type]
-    --> tests/unit_tests/services/test_billing_service.py:1856:41
+    --> tests/unit_tests/services/test_billing_service.py:1859:41
 ERROR Argument `BillingInfo` is not assignable to parameter `result` with type `dict[Unknown, Unknown]` in function `TestBillingServiceSubscriptionInfoDataType._assert_billing_info_types` [bad-argument-type]
-    --> tests/unit_tests/services/test_billing_service.py:1865:41
+    --> tests/unit_tests/services/test_billing_service.py:1868:41
 ERROR Argument `BillingInfo` is not assignable to parameter `result` with type `dict[Unknown, Unknown]` in function `TestBillingServiceSubscriptionInfoDataType._assert_billing_info_types` [bad-argument-type]
-    --> tests/unit_tests/services/test_billing_service.py:1878:41
+    --> tests/unit_tests/services/test_billing_service.py:1881:41
 ERROR Argument `BillingInfo` is not assignable to parameter `result` with type `dict[Unknown, Unknown]` in function `TestBillingServiceSubscriptionInfoDataType._assert_billing_info_types` [bad-argument-type]
-    --> tests/unit_tests/services/test_billing_service.py:1889:41
+    --> tests/unit_tests/services/test_billing_service.py:1892:41
 ERROR Argument `FakeRepo` is not assignable to parameter `workflow_run_repo` with type `APIWorkflowRunRepository | None` in function `services.retention.workflow_run.clear_free_plan_expired_workflow_run_logs.WorkflowRunCleanup.__init__` [bad-argument-type]
    --> tests/unit_tests/services/test_clear_free_plan_expired_workflow_run_logs.py:126:49
 ERROR Class member `FailingRepo.delete_runs_with_related_by_ids` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
@@ -6970,9 +7020,9 @@
 ERROR Class member `FixedDateTime.now` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
    --> tests/unit_tests/services/test_clear_free_plan_tenant_expired_logs.py:483:13
 ERROR Argument `SimpleNamespace` is not assignable to parameter `user` with type `Account` in function `services.credential_permission_service.CredentialPermissionService.apply_visibility_filter` [bad-argument-type]
-  --> tests/unit_tests/services/test_credential_permission_service.py:82:18
+  --> tests/unit_tests/services/test_credential_permission_service.py:80:18
 ERROR Argument `SimpleNamespace` is not assignable to parameter `user` with type `Account` in function `services.credential_permission_service.CredentialPermissionService.apply_visibility_filter` [bad-argument-type]
-  --> tests/unit_tests/services/test_credential_permission_service.py:99:18
+  --> tests/unit_tests/services/test_credential_permission_service.py:97:18
 ERROR Object of class `FromClause` has no attribute `create` [missing-attribute]
   --> tests/unit_tests/services/test_credit_pool_service.py:24:5
 ERROR Object of class `FromClause` has no attribute `insert` [missing-attribute]

@github-actions

github-actions Bot commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 51.50% 51.49% -0.00%
Strict coverage 51.01% 51.01% -0.00%
Typed symbols 30,849 30,849 0
Untyped symbols 29,332 29,335 +3
Modules 2931 2931 0

@asukaminato0721 asukaminato0721 self-assigned this Jun 22, 2026
@asukaminato0721

Copy link
Copy Markdown
Contributor

fix ci

@crazywoola

Copy link
Copy Markdown
Member

Thank you for your contribution and the effort you’ve put into this PR. It currently has merge conflicts with main, so we’re closing it as part of our PR queue cleanup. If you’d like to continue, please update your branch against the latest main, resolve the conflicts, and reopen this PR or submit a new one. We appreciate your help improving Dify and would be happy to revisit the updated contribution.

@crazywoola crazywoola closed this Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

make db.session pass from parameter.

3 participants