Files
deerflow-code/offline-backend-20260512/backend/packages/harness/deerflow/persistence/workflows/model.py
T
董宏凯 ac92f29db1 chore(persistence): 增加 Workflow Studio 表重建开关与迁移兼容修正
提交内容:为数据库生命周期增加按白名单清单重建 Workflow Studio 表的受限流程,限定仅 MySQL 且检查当前连接数据库名称一致,避免误删非目标 schema。

启动流程中新增 DEER_FLOW_REBUILD_WORKFLOW_SCHEMA 环境变量入口与持久层重建 API,并在 lifespan 里按需执行;导出到 persistence 包,便于后续调用与测试复用。

迁移与模型修复:将 workflow 相关大文本字段切到 MySQL LONGTEXT,不再给 TEXT/LONGTEXT 设置 server_default;新增 draft_canvas_schema_json 的迁移回填与非空约束变更,规避 MySQL 5.7 对默认值限制。

测试覆盖:补充 workflow 定义 ddl 长文本断言与 schema 重建保护性测试(仅允许清单表、数据库名不匹配直接拒绝),并把新测试文件加入版本追踪。
2026-08-31 16:45:06 +08:00

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)