Skip to content

refactor: use sessionmaker().begin() in console workspace and misc co…#34284

Merged
asukaminato0721 merged 2 commits intolanggenius:mainfrom
Desel72:refactor/use-sessionmaker-console-workspace
Mar 31, 2026
Merged

refactor: use sessionmaker().begin() in console workspace and misc co…#34284
asukaminato0721 merged 2 commits intolanggenius:mainfrom
Desel72:refactor/use-sessionmaker-console-workspace

Conversation

@Desel72
Copy link
Copy Markdown
Contributor

@Desel72 Desel72 commented Mar 30, 2026

Summary

Part of #24245.

Refactor remaining console controllers (workspace, apikey, explore) to use sessionmaker().begin() instead of Session() with explicit commit() or session.begin(), following SQLAlchemy best practices.

Files changed

  • api/controllers/console/apikey.py
  • api/controllers/console/explore/conversation.py
  • api/controllers/console/workspace/__init__.py
  • api/controllers/console/workspace/account.py
  • api/controllers/console/workspace/trigger_providers.py
  • api/controllers/console/workspace/tool_providers.py

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. refactor labels Mar 30, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

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

Refactors several console controllers to use sessionmaker(...).begin() context blocks for transaction framing, aligning controller DB session handling with SQLAlchemy 2.x session best practices.

Changes:

  • Replaced with Session(db.engine) as session: (and explicit commit() / session.begin()) with with sessionmaker(db.engine).begin() as session: in targeted endpoints.
  • Removed now-redundant explicit session.commit() calls where begin() handles commit/rollback automatically.
  • Updated imports from Session to sessionmaker across the affected modules.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
api/controllers/console/apikey.py Uses sessionmaker(...).begin() for _get_resource() session scope.
api/controllers/console/explore/conversation.py Uses sessionmaker(...).begin() for conversation pagination query.
api/controllers/console/workspace/init.py Uses sessionmaker(...).begin() for plugin permission lookup in decorator.
api/controllers/console/workspace/account.py Uses sessionmaker(...).begin() for account lookup during change-email flow.
api/controllers/console/workspace/trigger_providers.py Uses sessionmaker(...).begin() for deletion transaction and removes explicit commit.
api/controllers/console/workspace/tool_providers.py Uses sessionmaker(...).begin() across MCP provider CRUD/auth flows.
Comments suppressed due to low confidence (2)

api/controllers/console/workspace/account.py:570

  • AccountService.get_account_by_email_with_case_fallback() returns an ORM Account. Wrapping this read in sessionmaker(...).begin() will commit on exit and (with SQLAlchemy defaults) expire the instance; accessing account.email after the context can raise DetachedInstanceError. Consider either (a) using a non-committing session context for this read, or (b) extracting the needed scalar fields (email) inside the session before leaving, or (c) configuring the sessionmaker with expire_on_commit=False for this block.
            with sessionmaker(db.engine).begin() as session:
                account = AccountService.get_account_by_email_with_case_fallback(args.email, session=session)
            if account is None:
                raise AccountNotFound()
            email_for_sending = account.email
            user_email = account.email

api/controllers/console/workspace/init.py:39

  • The early return view(*args, **kwargs) runs while inside sessionmaker(...).begin(), so the entire view executes inside a DB transaction and the commit happens only after the view returns. This can unintentionally hold locks/transactions open for the full request. Consider limiting the session/transaction scope to just the permission query (compute an allow/deny decision, exit the context, then call view).
            with sessionmaker(db.engine).begin() as session:
                permission = (
                    session.query(TenantPluginPermission)
                    .where(
                        TenantPluginPermission.tenant_id == tenant_id,
                    )
                    .first()
                )

                if not permission:
                    # no permission set, allow access for everyone
                    return view(*args, **kwargs)


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

@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

1 similar comment
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@Desel72 Desel72 force-pushed the refactor/use-sessionmaker-console-workspace branch from 8fcdaad to 3744caa Compare March 31, 2026 14:02
@Desel72 Desel72 requested a review from laipz8200 as a code owner March 31, 2026 14:02
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 31, 2026
@asukaminato0721 asukaminato0721 added this pull request to the merge queue Mar 31, 2026
Merged via the queue into langgenius:main with commit dbdbb09 Mar 31, 2026
27 checks passed
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:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants