refactor(api): migrate tools, account, workflow and plugin services to SQLAlchemy 2.0 (#34966)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
wdeveloper16
2026-04-12 03:45:27 +02:00
committed by GitHub
parent 440602f52a
commit 0841b4c663
9 changed files with 111 additions and 102 deletions

View File

@@ -19,7 +19,7 @@ from graphon.variables.segments import (
)
from graphon.variables.types import SegmentType
from graphon.variables.utils import dumps_with_segments
from sqlalchemy import Engine, orm, select
from sqlalchemy import Engine, delete, orm, select
from sqlalchemy.dialects.mysql import insert as mysql_insert
from sqlalchemy.dialects.postgresql import insert as pg_insert
from sqlalchemy.orm import Session, sessionmaker
@@ -222,11 +222,10 @@ class WorkflowDraftVariableService:
)
def get_variable(self, variable_id: str) -> WorkflowDraftVariable | None:
return (
self._session.query(WorkflowDraftVariable)
return self._session.scalar(
select(WorkflowDraftVariable)
.options(orm.selectinload(WorkflowDraftVariable.variable_file))
.where(WorkflowDraftVariable.id == variable_id)
.first()
)
def get_draft_variables_by_selectors(
@@ -254,20 +253,21 @@ class WorkflowDraftVariableService:
# Alternatively, a `SELECT` statement could be constructed for each selector and
# combined using `UNION` to fetch all rows.
# Benchmarking indicates that both approaches yield comparable performance.
query = (
self._session.query(WorkflowDraftVariable)
.options(
orm.selectinload(WorkflowDraftVariable.variable_file).selectinload(
WorkflowDraftVariableFile.upload_file
return list(
self._session.scalars(
select(WorkflowDraftVariable)
.options(
orm.selectinload(WorkflowDraftVariable.variable_file).selectinload(
WorkflowDraftVariableFile.upload_file
)
)
.where(
WorkflowDraftVariable.app_id == app_id,
WorkflowDraftVariable.user_id == user_id,
or_(*ors),
)
)
.where(
WorkflowDraftVariable.app_id == app_id,
WorkflowDraftVariable.user_id == user_id,
or_(*ors),
)
)
return query.all()
def list_variables_without_values(
self, app_id: str, page: int, limit: int, user_id: str
@@ -277,18 +277,21 @@ class WorkflowDraftVariableService:
WorkflowDraftVariable.user_id == user_id,
]
total = None
query = self._session.query(WorkflowDraftVariable).where(*criteria)
base_stmt = select(WorkflowDraftVariable).where(*criteria)
if page == 1:
total = query.count()
variables = (
# Do not load the `value` field
query.options(
orm.defer(WorkflowDraftVariable.value, raiseload=True),
from sqlalchemy import func as sa_func
total = self._session.scalar(select(sa_func.count()).select_from(base_stmt.subquery()))
variables = list(
self._session.scalars(
# Do not load the `value` field
base_stmt.options(
orm.defer(WorkflowDraftVariable.value, raiseload=True),
)
.order_by(WorkflowDraftVariable.created_at.desc())
.limit(limit)
.offset((page - 1) * limit)
)
.order_by(WorkflowDraftVariable.created_at.desc())
.limit(limit)
.offset((page - 1) * limit)
.all()
)
return WorkflowDraftVariableList(variables=variables, total=total)
@@ -299,11 +302,13 @@ class WorkflowDraftVariableService:
WorkflowDraftVariable.node_id == node_id,
WorkflowDraftVariable.user_id == user_id,
]
query = self._session.query(WorkflowDraftVariable).where(*criteria)
variables = (
query.options(orm.selectinload(WorkflowDraftVariable.variable_file))
.order_by(WorkflowDraftVariable.created_at.desc())
.all()
variables = list(
self._session.scalars(
select(WorkflowDraftVariable)
.options(orm.selectinload(WorkflowDraftVariable.variable_file))
.where(*criteria)
.order_by(WorkflowDraftVariable.created_at.desc())
)
)
return WorkflowDraftVariableList(variables=variables)
@@ -326,8 +331,8 @@ class WorkflowDraftVariableService:
return self._get_variable(app_id, node_id, name, user_id=user_id)
def _get_variable(self, app_id: str, node_id: str, name: str, user_id: str) -> WorkflowDraftVariable | None:
return (
self._session.query(WorkflowDraftVariable)
return self._session.scalar(
select(WorkflowDraftVariable)
.options(orm.selectinload(WorkflowDraftVariable.variable_file))
.where(
WorkflowDraftVariable.app_id == app_id,
@@ -335,7 +340,6 @@ class WorkflowDraftVariableService:
WorkflowDraftVariable.name == name,
WorkflowDraftVariable.user_id == user_id,
)
.first()
)
def update_variable(
@@ -488,20 +492,20 @@ class WorkflowDraftVariableService:
self._session.delete(variable)
def delete_user_workflow_variables(self, app_id: str, user_id: str):
(
self._session.query(WorkflowDraftVariable)
self._session.execute(
delete(WorkflowDraftVariable)
.where(
WorkflowDraftVariable.app_id == app_id,
WorkflowDraftVariable.user_id == user_id,
)
.delete(synchronize_session=False)
.execution_options(synchronize_session=False)
)
def delete_app_workflow_variables(self, app_id: str):
(
self._session.query(WorkflowDraftVariable)
self._session.execute(
delete(WorkflowDraftVariable)
.where(WorkflowDraftVariable.app_id == app_id)
.delete(synchronize_session=False)
.execution_options(synchronize_session=False)
)
def delete_workflow_draft_variable_file(self, deletions: list[DraftVarFileDeletion]):
@@ -540,14 +544,14 @@ class WorkflowDraftVariableService:
return self._delete_node_variables(app_id, node_id, user_id=user_id)
def _delete_node_variables(self, app_id: str, node_id: str, user_id: str):
(
self._session.query(WorkflowDraftVariable)
self._session.execute(
delete(WorkflowDraftVariable)
.where(
WorkflowDraftVariable.app_id == app_id,
WorkflowDraftVariable.node_id == node_id,
WorkflowDraftVariable.user_id == user_id,
)
.delete(synchronize_session=False)
.execution_options(synchronize_session=False)
)
def _get_conversation_id_from_draft_variable(self, app_id: str, user_id: str) -> str | None:
@@ -588,13 +592,11 @@ class WorkflowDraftVariableService:
conv_id = self._get_conversation_id_from_draft_variable(workflow.app_id, account_id)
if conv_id is not None:
conversation = (
self._session.query(Conversation)
.where(
conversation = self._session.scalar(
select(Conversation).where(
Conversation.id == conv_id,
Conversation.app_id == workflow.app_id,
)
.first()
)
# Only return the conversation ID if it exists and is valid (has a correspond conversation record in DB).
if conversation is not None: