Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

chore: port WorkflowNodeExecutionOffload to TypeBase (#35633)

Asuka Minatod7630db953d1
diff --git a/api/core/repositories/sqlalchemy_workflow_node_execution_repository.py b/api/core/repositories/sqlalchemy_workflow_node_execution_repository.py
index 2fe5f427e5d..8d3c449857c 100644
--- a/api/core/repositories/sqlalchemy_workflow_node_execution_repository.py
+++ b/api/core/repositories/sqlalchemy_workflow_node_execution_repository.py
@@ -301,14 +301,15 @@ class SQLAlchemyWorkflowNodeExecutionRepository(WorkflowNodeExecutionRepository)
user=self._user,
tenant_id=self._tenant_id,
)
+ assert self._app_id
offload = WorkflowNodeExecutionOffload(
- id=uuidv7(),
tenant_id=self._tenant_id,
app_id=self._app_id,
node_execution_id=execution_id,
type_=type_,
file_id=upload_file.id,
)
+ offload.id = str(uuidv7())
return _InputsOutputsTruncationResult(
truncated_value=truncated_values,
file=upload_file,
diff --git a/api/models/workflow.py b/api/models/workflow.py
index a53cd4248c8..5868d78b144 100644
--- a/api/models/workflow.py
+++ b/api/models/workflow.py
@@ -73,8 +73,8 @@ from .base import Base, DefaultFieldsDCMixin, TypeBase
from .engine import db
from .enums import CreatorUserRole, DraftVariableType, ExecutionOffLoadType, WorkflowRunTriggeredFrom
-# UploadFile uses TypeBase while workflow execution offload models use Base, so relationships
-# must target the class object directly instead of relying on string lookup across registries.
+# UploadFile and workflow execution offload use TypeBase, so importing the class object keeps
+# relationship joins explicit where the related execution model still uses Base.
from .model import UploadFile
from .types import EnumText, LongText, StringUUID
from .utils.file_input_compat import (
@@ -1056,9 +1056,13 @@ class WorkflowNodeExecutionModel(Base): # This model is expected to have `offlo
created_by: Mapped[str] = mapped_column(StringUUID)
finished_at: Mapped[datetime | None] = mapped_column(DateTime)
+ # WorkflowNodeExecutionOffload uses TypeBase while this model uses Base, so both the target and join
+ # must resolve lazily as class objects instead of relying on string lookup across registries.
offload_data: Mapped[list["WorkflowNodeExecutionOffload"]] = orm.relationship(
- "WorkflowNodeExecutionOffload",
- primaryjoin="WorkflowNodeExecutionModel.id == foreign(WorkflowNodeExecutionOffload.node_execution_id)",
+ lambda: WorkflowNodeExecutionOffload,
+ primaryjoin=lambda: (
+ WorkflowNodeExecutionModel.id == orm.foreign(WorkflowNodeExecutionOffload.node_execution_id)
+ ),
uselist=True,
lazy="raise",
back_populates="execution",
@@ -1196,7 +1200,7 @@ class WorkflowNodeExecutionModel(Base): # This model is expected to have `offlo
return self._load_full_content(session, offload.file_id, storage)
-class WorkflowNodeExecutionOffload(Base):
+class WorkflowNodeExecutionOffload(TypeBase):
__tablename__ = "workflow_node_execution_offload"
__table_args__ = (
# PostgreSQL 14 treats NULL values as distinct in unique constraints by default,
@@ -1215,15 +1219,7 @@ class WorkflowNodeExecutionOffload(Base):
)
_HASH_COL_SIZE = 64
- id: Mapped[str] = mapped_column(
- StringUUID,
- primary_key=True,
- default=lambda: str(uuid4()),
- )
-
- created_at: Mapped[datetime] = mapped_column(
- DateTime, default=naive_utc_now, server_default=func.current_timestamp()
- )
+ id: Mapped[str] = mapped_column(StringUUID, primary_key=True, default_factory=lambda: str(uuidv7()), init=False)
tenant_id: Mapped[str] = mapped_column(StringUUID)
app_id: Mapped[str] = mapped_column(StringUUID)
@@ -1256,8 +1252,11 @@ class WorkflowNodeExecutionOffload(Base):
foreign_keys=[node_execution_id],
lazy="raise",
uselist=False,
- primaryjoin="WorkflowNodeExecutionOffload.node_execution_id == WorkflowNodeExecutionModel.id",
+ primaryjoin=lambda: (
+ orm.foreign(WorkflowNodeExecutionOffload.node_execution_id) == WorkflowNodeExecutionModel.id
+ ),
back_populates="offload_data",
+ init=False,
)
file: Mapped[Optional["UploadFile"]] = orm.relationship(
@@ -1266,6 +1265,10 @@ class WorkflowNodeExecutionOffload(Base):
lazy="raise",
uselist=False,
primaryjoin=lambda: orm.foreign(WorkflowNodeExecutionOffload.file_id) == UploadFile.id,
+ init=False,
+ )
+ created_at: Mapped[datetime] = mapped_column(
+ DateTime, default_factory=naive_utc_now, server_default=func.current_timestamp()
)
diff --git a/api/tests/unit_tests/core/repositories/test_sqlalchemy_workflow_node_execution_repository.py b/api/tests/unit_tests/core/repositories/test_sqlalchemy_workflow_node_execution_repository.py
index 953529b4c55..dd95b4231f7 100644
--- a/api/tests/unit_tests/core/repositories/test_sqlalchemy_workflow_node_execution_repository.py
+++ b/api/tests/unit_tests/core/repositories/test_sqlalchemy_workflow_node_execution_repository.py
@@ -60,6 +60,16 @@ def _upload_file(*, key: str = "storage-key") -> UploadFile:
)
+def _offload(type_: ExecutionOffLoadType, *, file_id: str = "file-1") -> WorkflowNodeExecutionOffload:
+ return WorkflowNodeExecutionOffload(
+ tenant_id="tenant-1",
+ app_id="app-1",
+ node_execution_id="execution-1",
+ type_=type_,
+ file_id=file_id,
+ )
+
+
def _execution(
*,
execution_id: str = "execution-1",
@@ -192,12 +202,10 @@ def test_helper_functions_and_truncator_configuration(
assert _deterministic_json_dump({"b": 1, "a": 2}) == '{"a": 2, "b": 1}'
assert _find_first([], lambda _value: True) is None
assert _find_first([1, 2, 3], lambda value: value > 1) == 2
- inputs = WorkflowNodeExecutionOffload(type_=ExecutionOffLoadType.INPUTS)
- outputs = WorkflowNodeExecutionOffload(type_=ExecutionOffLoadType.OUTPUTS)
+ inputs = _offload(ExecutionOffLoadType.INPUTS)
+ outputs = _offload(ExecutionOffLoadType.OUTPUTS)
assert _find_first([inputs, outputs], _filter_by_offload_type(ExecutionOffLoadType.OUTPUTS)) is outputs
- replaced = _replace_or_append_offload(
- [inputs, outputs], WorkflowNodeExecutionOffload(type_=ExecutionOffLoadType.INPUTS)
- )
+ replaced = _replace_or_append_offload([inputs, outputs], _offload(ExecutionOffLoadType.INPUTS))
assert [item.type_ for item in replaced] == [ExecutionOffLoadType.OUTPUTS, ExecutionOffLoadType.INPUTS]
created: dict[str, int] = {}
@@ -455,7 +463,7 @@ def test_to_domain_model_loads_offloaded_storage(
)
offloads = []
for offload_type in ExecutionOffLoadType:
- offload = WorkflowNodeExecutionOffload(type_=offload_type)
+ offload = _offload(offload_type)
offload.file = _upload_file(key=offload_type.value)
offloads.append(offload)
db_model.offload_data = offloads
diff --git a/api/tests/unit_tests/core/repositories/test_workflow_node_execution_truncation.py b/api/tests/unit_tests/core/repositories/test_workflow_node_execution_truncation.py
index b444c93e5f8..ba7b5282993 100644
--- a/api/tests/unit_tests/core/repositories/test_workflow_node_execution_truncation.py
+++ b/api/tests/unit_tests/core/repositories/test_workflow_node_execution_truncation.py
@@ -121,6 +121,16 @@ def mock_user() -> Account:
return user
+def create_offload(type_: ExecutionOffLoadType) -> WorkflowNodeExecutionOffload:
+ return WorkflowNodeExecutionOffload(
+ tenant_id="test-tenant-id",
+ app_id="test-app-id",
+ node_execution_id="test-node-execution-id",
+ type_=type_,
+ file_id="test-file-id",
+ )
+
+
class TestSQLAlchemyWorkflowNodeExecutionRepositoryTruncation:
"""Test class for truncation functionality in SQLAlchemyWorkflowNodeExecutionRepository."""
@@ -178,7 +188,7 @@ class TestWorkflowNodeExecutionModelTruncatedProperties:
def test_inputs_truncated_with_offload_data(self):
"""Test inputs_truncated property when offload data exists."""
model = WorkflowNodeExecutionModel()
- offload = WorkflowNodeExecutionOffload(type_=ExecutionOffLoadType.INPUTS)
+ offload = create_offload(ExecutionOffLoadType.INPUTS)
model.offload_data = [offload]
assert model.inputs_truncated is True
@@ -190,7 +200,7 @@ class TestWorkflowNodeExecutionModelTruncatedProperties:
model = WorkflowNodeExecutionModel()
# Mock offload data with outputs file
- offload = WorkflowNodeExecutionOffload(type_=ExecutionOffLoadType.OUTPUTS)
+ offload = create_offload(ExecutionOffLoadType.OUTPUTS)
model.offload_data = [offload]
assert model.inputs_truncated is False
@@ -199,7 +209,7 @@ class TestWorkflowNodeExecutionModelTruncatedProperties:
def test_process_data_truncated_with_offload_data(self):
model = WorkflowNodeExecutionModel()
- offload = WorkflowNodeExecutionOffload(type_=ExecutionOffLoadType.PROCESS_DATA)
+ offload = create_offload(ExecutionOffLoadType.PROCESS_DATA)
model.offload_data = [offload]
assert model.process_data_truncated is True
assert model.inputs_truncated is False