73 lines
3.5 KiB
Python
73 lines
3.5 KiB
Python
"""ORM models for workflow definitions and immutable published versions."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
from sqlalchemy import ForeignKey, Integer, String, Text, UniqueConstraint
|
|
from sqlalchemy import text as sa_text
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from deerflow.persistence.base import Base
|
|
from deerflow.persistence.types import BeijingDateTime, PortableJSON, PortableLongText
|
|
|
|
|
|
class WorkflowDefinitionRow(Base):
|
|
"""Mutable workflow metadata + current draft canvas."""
|
|
|
|
__tablename__ = "workflow_definitions"
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
description: Mapped[str] = mapped_column(Text, default="", nullable=False)
|
|
owner_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
|
# draft | active | archived
|
|
status: Mapped[str] = mapped_column(String(32), default="draft", nullable=False, index=True)
|
|
draft_revision: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
|
# Monotonic version allocator. "1" = no version published yet; publish takes
|
|
# the value then CAS-bumps it, replacing the old MAX(version_number)+1 race.
|
|
next_version_number: Mapped[int] = mapped_column(
|
|
Integer, nullable=False, default=1, server_default=sa_text("1")
|
|
)
|
|
draft_graph_json: Mapped[str] = mapped_column(PortableLongText(), default="{}", nullable=False)
|
|
# Coze canvas document (opaque JSON text). Never interpreted by the runtime;
|
|
# the executable WorkflowGraph lives in draft_graph_json above.
|
|
draft_canvas_schema_json: Mapped[str] = mapped_column(
|
|
# Keep this as an ORM-side default only. MySQL 5.7 rejects DEFAULT on
|
|
# TEXT/LONGTEXT columns, while workflow creation always supplies this
|
|
# value explicitly as well.
|
|
PortableLongText(), default="{}", nullable=False
|
|
)
|
|
# sha256 of the canvas document the current published version was compiled
|
|
# from (audit trail only; publish always freezes the execution graph).
|
|
published_canvas_schema_hash: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=lambda: datetime.now(UTC))
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
BeijingDateTime(),
|
|
default=lambda: datetime.now(UTC),
|
|
onupdate=lambda: datetime.now(UTC),
|
|
)
|
|
|
|
|
|
class WorkflowVersionRow(Base):
|
|
"""Immutable published graph snapshot. Runs bind to version id."""
|
|
|
|
__tablename__ = "workflow_versions"
|
|
__table_args__ = (UniqueConstraint("workflow_id", "version_number", name="uq_workflow_versions_wf_num"),)
|
|
|
|
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
|
workflow_id: Mapped[str] = mapped_column(
|
|
String(64),
|
|
ForeignKey("workflow_definitions.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
version_number: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
graph_json: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
|
|
graph_hash: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
|
input_schema: Mapped[dict] = mapped_column(PortableJSON(), default=dict)
|
|
output_schema: Mapped[dict] = mapped_column(PortableJSON(), default=dict)
|
|
published_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
published_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=lambda: datetime.now(UTC))
|
|
change_note: Mapped[str] = mapped_column(Text, default="", nullable=False)
|