refactor(api_workflow_node_execution_repository): Refactors workflow run query for clarity

Signed-off-by: -LAN- <laipz8200@outlook.com>
pull/22581/head
-LAN- 11 months ago
parent 83efc16c3c
commit 9f1e998b8b
No known key found for this signature in database
GPG Key ID: 6BA0D108DED011FF

@ -96,7 +96,7 @@ class DifyAPIWorkflowNodeExecutionRepository(WorkflowNodeExecutionRepository, Pr
Tenant filtering is optional for cases where the execution ID is globally unique. Tenant filtering is optional for cases where the execution ID is globally unique.
When `tenant_id` is None, it's the caller's responsibility to ensure proper data isolation between tenants. When `tenant_id` is None, it's the caller's responsibility to ensure proper data isolation between tenants.
If the `execution_id` comes from untrusted sources (e.g., retrieved from an API request), the caller should If the `execution_id` comes from untrusted sources (e.g., retrieved from an API request), the caller should
set `tenant_id` to prevent horizontal privilege escalation. set `tenant_id` to prevent horizontal privilege escalation.
Args: Args:

@ -25,7 +25,7 @@ from datetime import datetime
from typing import Optional, cast from typing import Optional, cast
from sqlalchemy import delete, select from sqlalchemy import delete, select
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import Session, sessionmaker
from libs.infinite_scroll_pagination import InfiniteScrollPagination from libs.infinite_scroll_pagination import InfiniteScrollPagination
from models.workflow import WorkflowRun from models.workflow import WorkflowRun
@ -45,7 +45,7 @@ class DifyAPISQLAlchemyWorkflowRunRepository:
session_maker: SQLAlchemy sessionmaker instance for database connections session_maker: SQLAlchemy sessionmaker instance for database connections
""" """
def __init__(self, session_maker: sessionmaker) -> None: def __init__(self, session_maker: sessionmaker[Session]) -> None:
""" """
Initialize the repository with a sessionmaker. Initialize the repository with a sessionmaker.
@ -86,17 +86,13 @@ class DifyAPISQLAlchemyWorkflowRunRepository:
raise ValueError("Last workflow run not exists") raise ValueError("Last workflow run not exists")
# Get records created before the last run's timestamp # Get records created before the last run's timestamp
workflow_runs = session.scalars( base_stmt = base_stmt.where(
base_stmt.where( WorkflowRun.created_at < last_workflow_run.created_at,
WorkflowRun.created_at < last_workflow_run.created_at, WorkflowRun.id != last_workflow_run.id,
WorkflowRun.id != last_workflow_run.id, )
)
.order_by(WorkflowRun.created_at.desc()) # First page - get most recent records
.limit(limit) workflow_runs = session.scalars(base_stmt.order_by(WorkflowRun.created_at.desc()).limit(limit)).all()
).all()
else:
# First page - get most recent records
workflow_runs = session.scalars(base_stmt.order_by(WorkflowRun.created_at.desc()).limit(limit)).all()
# Check if there are more records for pagination # Check if there are more records for pagination
has_more = False has_more = False

Loading…
Cancel
Save