mirror of
https://github.com/langgenius/dify.git
synced 2026-07-31 17:29:37 +08:00
chore(api): consolidate chained .where() calls into single calls (#39819)
This commit is contained in:
parent
bea2f78e09
commit
908dc703ec
@ -240,9 +240,9 @@ class MCPAppApi(Resource):
|
||||
with sessionmaker(db.engine, expire_on_commit=False).begin() as session:
|
||||
return session.scalar(
|
||||
select(EndUser)
|
||||
.where(EndUser.tenant_id == tenant_id)
|
||||
.where(EndUser.session_id == mcp_server_id)
|
||||
.where(EndUser.type == EndUserType.MCP)
|
||||
.where(
|
||||
EndUser.tenant_id == tenant_id, EndUser.session_id == mcp_server_id, EndUser.type == EndUserType.MCP
|
||||
)
|
||||
.limit(1)
|
||||
)
|
||||
|
||||
|
||||
@ -322,11 +322,12 @@ def validate_dataset_token[R](view: Callable[..., R]) -> Callable[..., R]:
|
||||
raise Forbidden("Dataset api access is not enabled.")
|
||||
|
||||
tenant_account_join = db.session.execute(
|
||||
select(Tenant, TenantAccountJoin)
|
||||
.where(Tenant.id == api_token.tenant_id)
|
||||
.where(TenantAccountJoin.tenant_id == Tenant.id)
|
||||
.where(TenantAccountJoin.role.in_(["owner"]))
|
||||
.where(Tenant.status == TenantStatus.NORMAL)
|
||||
select(Tenant, TenantAccountJoin).where(
|
||||
Tenant.id == api_token.tenant_id,
|
||||
TenantAccountJoin.tenant_id == Tenant.id,
|
||||
TenantAccountJoin.role.in_(["owner"]),
|
||||
Tenant.status == TenantStatus.NORMAL,
|
||||
)
|
||||
).one_or_none() # TODO: only owner information is required, so only one is returned.
|
||||
if tenant_account_join:
|
||||
tenant, ta = tenant_account_join
|
||||
|
||||
@ -66,11 +66,7 @@ class MessageCycleManager:
|
||||
# Use SQLAlchemy 2.x style session.scalar(select(...))
|
||||
with session_factory.create_session() as session:
|
||||
message_file = session.scalar(
|
||||
select(MessageFile)
|
||||
.where(
|
||||
MessageFile.message_id == message_id,
|
||||
)
|
||||
.where(MessageFile.belongs_to == "assistant")
|
||||
select(MessageFile).where(MessageFile.message_id == message_id, MessageFile.belongs_to == "assistant")
|
||||
)
|
||||
|
||||
if message_file:
|
||||
|
||||
@ -2003,8 +2003,11 @@ class DatasetRetrieval:
|
||||
results = session.scalars(
|
||||
select(Dataset)
|
||||
.outerjoin(subquery, Dataset.id == subquery.c.dataset_id)
|
||||
.where(Dataset.tenant_id == tenant_id, Dataset.id.in_(dataset_ids))
|
||||
.where((subquery.c.available_document_count > 0) | (Dataset.provider == "external"))
|
||||
.where(
|
||||
Dataset.tenant_id == tenant_id,
|
||||
Dataset.id.in_(dataset_ids),
|
||||
(subquery.c.available_document_count > 0) | (Dataset.provider == "external"),
|
||||
)
|
||||
).all()
|
||||
|
||||
available_datasets = []
|
||||
|
||||
@ -72,10 +72,11 @@ def _load_user_from_request(request_from_flask_login: Request, session: Session)
|
||||
workspace_id = request.headers.get("X-WORKSPACE-ID")
|
||||
if workspace_id:
|
||||
tenant_account_join = session.execute(
|
||||
select(Tenant, TenantAccountJoin)
|
||||
.where(Tenant.id == workspace_id)
|
||||
.where(TenantAccountJoin.tenant_id == Tenant.id)
|
||||
.where(TenantAccountJoin.role == "owner")
|
||||
select(Tenant, TenantAccountJoin).where(
|
||||
Tenant.id == workspace_id,
|
||||
TenantAccountJoin.tenant_id == Tenant.id,
|
||||
TenantAccountJoin.role == "owner",
|
||||
)
|
||||
).one_or_none()
|
||||
if tenant_account_join:
|
||||
tenant, ta = tenant_account_join
|
||||
|
||||
@ -165,11 +165,8 @@ class Account(UserMixin, TypeBase):
|
||||
|
||||
def set_tenant_id_with_session(self, tenant_id: str, *, session: Session) -> None:
|
||||
"""Set the current tenant by id using the caller-owned session."""
|
||||
query = (
|
||||
select(Tenant, TenantAccountJoin)
|
||||
.where(Tenant.id == tenant_id)
|
||||
.where(TenantAccountJoin.tenant_id == Tenant.id)
|
||||
.where(TenantAccountJoin.account_id == self.id)
|
||||
query = select(Tenant, TenantAccountJoin).where(
|
||||
Tenant.id == tenant_id, TenantAccountJoin.tenant_id == Tenant.id, TenantAccountJoin.account_id == self.id
|
||||
)
|
||||
tenant_account_join = session.execute(query).first()
|
||||
if not tenant_account_join:
|
||||
|
||||
@ -1624,8 +1624,7 @@ class TenantService:
|
||||
select(Account, TenantAccountJoin.role)
|
||||
.select_from(Account)
|
||||
.join(TenantAccountJoin, Account.id == TenantAccountJoin.account_id)
|
||||
.where(TenantAccountJoin.tenant_id == tenant.id)
|
||||
.where(TenantAccountJoin.role == "dataset_operator")
|
||||
.where(TenantAccountJoin.tenant_id == tenant.id, TenantAccountJoin.role == "dataset_operator")
|
||||
)
|
||||
|
||||
# Initialize an empty list to store the updated accounts
|
||||
|
||||
@ -230,12 +230,12 @@ class AppAnnotationService:
|
||||
escaped_keyword = escape_like_pattern(keyword)
|
||||
stmt = (
|
||||
select(MessageAnnotation)
|
||||
.where(MessageAnnotation.app_id == app_id)
|
||||
.where(
|
||||
MessageAnnotation.app_id == app_id,
|
||||
or_(
|
||||
MessageAnnotation.question.ilike(f"%{escaped_keyword}%", escape="\\"),
|
||||
MessageAnnotation.content.ilike(f"%{escaped_keyword}%", escape="\\"),
|
||||
)
|
||||
),
|
||||
)
|
||||
.order_by(MessageAnnotation.created_at.desc(), MessageAnnotation.id.desc())
|
||||
)
|
||||
|
||||
@ -256,8 +256,7 @@ class ConversationService:
|
||||
|
||||
stmt = (
|
||||
select(ConversationVariable)
|
||||
.where(ConversationVariable.app_id == app_model.id)
|
||||
.where(ConversationVariable.conversation_id == conversation.id)
|
||||
.where(ConversationVariable.app_id == app_model.id, ConversationVariable.conversation_id == conversation.id)
|
||||
.order_by(ConversationVariable.created_at)
|
||||
)
|
||||
|
||||
@ -342,11 +341,10 @@ class ConversationService:
|
||||
conversation = cls.get_conversation(app_model, conversation_id, user, session=session)
|
||||
|
||||
# Get the existing conversation variable
|
||||
stmt = (
|
||||
select(ConversationVariable)
|
||||
.where(ConversationVariable.app_id == app_model.id)
|
||||
.where(ConversationVariable.conversation_id == conversation.id)
|
||||
.where(ConversationVariable.id == variable_id)
|
||||
stmt = select(ConversationVariable).where(
|
||||
ConversationVariable.app_id == app_model.id,
|
||||
ConversationVariable.conversation_id == conversation.id,
|
||||
ConversationVariable.id == variable_id,
|
||||
)
|
||||
|
||||
existing_variable = session.scalar(stmt)
|
||||
|
||||
@ -125,8 +125,7 @@ def test_delete_by_run_ids_empty_short_circuits(db_session_with_containers: Sess
|
||||
remaining_count = db_session_with_containers.scalar(
|
||||
select(func.count())
|
||||
.select_from(WorkflowTriggerLog)
|
||||
.where(WorkflowTriggerLog.tenant_id == tenant_id)
|
||||
.where(WorkflowTriggerLog.workflow_run_id == run_id)
|
||||
.where(WorkflowTriggerLog.tenant_id == tenant_id, WorkflowTriggerLog.workflow_run_id == run_id)
|
||||
)
|
||||
assert remaining_count == 1
|
||||
finally:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user