代码提交

This commit is contained in:
董宏凯 committed 2026-08-30 01:25:20 +08:00
1 parent 4ac7bc21cd
commit 17cf849361
110 files changed
+17007 -518

No files matched your search

@@ -0,0 +1,64 @@
"""Create workflow_definitions / workflow_versions tables.
Revision ID: 20260828_01
Revises: 20260825_01
Create Date: 2026-08-28
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20260828_01"
down_revision = "20260825_01"
branch_labels = None
depends_on = None
def upgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if not inspector.has_table("workflow_definitions"):
op.create_table(
"workflow_definitions",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column("description", sa.Text(), nullable=False, server_default=""),
sa.Column("owner_id", sa.String(length=64), nullable=False),
sa.Column("status", sa.String(length=32), nullable=False, server_default="draft"),
sa.Column("draft_revision", sa.Integer(), nullable=False, server_default="0"),
sa.Column("draft_graph_json", sa.Text(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_workflow_definitions_owner_id", "workflow_definitions", ["owner_id"])
op.create_index("ix_workflow_definitions_status", "workflow_definitions", ["status"])
if not inspector.has_table("workflow_versions"):
op.create_table(
"workflow_versions",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("workflow_id", sa.String(length=64), nullable=False),
sa.Column("version_number", sa.Integer(), nullable=False),
sa.Column("graph_json", sa.Text(), nullable=False),
sa.Column("graph_hash", sa.String(length=64), nullable=False),
sa.Column("input_schema", sa.Text(), nullable=True),
sa.Column("output_schema", sa.Text(), nullable=True),
sa.Column("published_by", sa.String(length=64), nullable=True),
sa.Column("published_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("change_note", sa.Text(), nullable=False, server_default=""),
sa.ForeignKeyConstraint(["workflow_id"], ["workflow_definitions.id"], ondelete="CASCADE"),
sa.UniqueConstraint("workflow_id", "version_number", name="uq_workflow_versions_wf_num"),
)
op.create_index("ix_workflow_versions_workflow_id", "workflow_versions", ["workflow_id"])
op.create_index("ix_workflow_versions_graph_hash", "workflow_versions", ["graph_hash"])
def downgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if inspector.has_table("workflow_versions"):
op.drop_table("workflow_versions")
if inspector.has_table("workflow_definitions"):
op.drop_table("workflow_definitions")
@@ -0,0 +1,127 @@
"""Create workflow run / node-run / event / artifact tables.
Revision ID: 20260828_02
Revises: 20260828_01
Create Date: 2026-08-28
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20260828_02"
down_revision = "20260828_01"
branch_labels = None
depends_on = None
def upgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if not inspector.has_table("workflow_runs"):
op.create_table(
"workflow_runs",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("workflow_id", sa.String(length=64), nullable=False),
sa.Column("workflow_version_id", sa.String(length=64), nullable=False),
sa.Column("status", sa.String(length=32), nullable=False, server_default="queued"),
sa.Column("owner_id", sa.String(length=64), nullable=False),
sa.Column("idempotency_key", sa.String(length=128), nullable=True),
sa.Column("input_json", sa.Text(), nullable=True),
sa.Column("output_json", sa.Text(), nullable=True),
sa.Column("error_json", sa.Text(), nullable=True),
sa.Column("context_json", sa.Text(), nullable=True),
sa.Column("pending_input_json", sa.Text(), nullable=True),
sa.Column("resume_token", sa.String(length=64), nullable=True),
sa.Column("lease_owner", sa.String(length=64), nullable=True),
sa.Column("lease_until", sa.DateTime(timezone=True), nullable=True),
sa.Column("heartbeat_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("attempt", sa.Integer(), nullable=False, server_default="0"),
sa.Column("max_attempts", sa.Integer(), nullable=False, server_default="3"),
sa.Column("version", sa.Integer(), nullable=False, server_default="0"),
sa.Column("retry_of_run_id", sa.String(length=64), nullable=True),
sa.Column("started_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("finished_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint("idempotency_key", name="uq_workflow_runs_idempotency"),
)
op.create_index("ix_workflow_runs_workflow_id", "workflow_runs", ["workflow_id"])
op.create_index("ix_workflow_runs_workflow_version_id", "workflow_runs", ["workflow_version_id"])
op.create_index("ix_workflow_runs_owner_id", "workflow_runs", ["owner_id"])
op.create_index("ix_workflow_runs_status", "workflow_runs", ["status"])
op.create_index("ix_workflow_runs_resume_token", "workflow_runs", ["resume_token"])
if not inspector.has_table("workflow_node_runs"):
op.create_table(
"workflow_node_runs",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("run_id", sa.String(length=64), nullable=False),
sa.Column("node_id", sa.String(length=128), nullable=False),
sa.Column("node_type", sa.String(length=32), nullable=False),
sa.Column("status", sa.String(length=32), nullable=False, server_default="queued"),
sa.Column("attempt", sa.Integer(), nullable=False, server_default="1"),
sa.Column("iteration", sa.Integer(), nullable=False, server_default="0"),
sa.Column("is_current", sa.Boolean(), nullable=False, server_default=sa.text("1")),
sa.Column("input_snapshot", sa.Text(), nullable=True),
sa.Column("output_snapshot", sa.Text(), nullable=True),
sa.Column("error_json", sa.Text(), nullable=True),
sa.Column("parent_node_run_id", sa.String(length=64), nullable=True),
sa.Column("duration_ms", sa.Integer(), nullable=True),
sa.Column("started_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("finished_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint(
"run_id", "node_id", "attempt", "iteration", name="uq_workflow_node_runs_key"
),
)
op.create_index("ix_workflow_node_runs_run_id", "workflow_node_runs", ["run_id"])
op.create_index("ix_workflow_node_runs_status", "workflow_node_runs", ["status"])
if not inspector.has_table("workflow_run_events"):
op.create_table(
"workflow_run_events",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column("run_id", sa.String(length=64), nullable=False),
sa.Column("workflow_id", sa.String(length=64), nullable=False),
sa.Column("version_id", sa.String(length=64), nullable=False),
sa.Column("seq", sa.Integer(), nullable=False),
sa.Column("event_type", sa.String(length=64), nullable=False),
sa.Column("node_id", sa.String(length=128), nullable=True),
sa.Column("node_run_id", sa.String(length=64), nullable=True),
sa.Column("payload_json", sa.Text(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint("run_id", "seq", name="uq_workflow_run_events_run_seq"),
)
op.create_index("ix_workflow_run_events_run_id", "workflow_run_events", ["run_id"])
op.create_index("ix_workflow_run_events_run_seq", "workflow_run_events", ["run_id", "seq"])
if not inspector.has_table("workflow_run_artifacts"):
op.create_table(
"workflow_run_artifacts",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("run_id", sa.String(length=64), nullable=False),
sa.Column("node_id", sa.String(length=128), nullable=False),
sa.Column("name", sa.String(length=512), nullable=False),
sa.Column("mime_type", sa.String(length=191), nullable=False),
sa.Column("path", sa.Text(), nullable=False),
sa.Column("size_bytes", sa.Integer(), nullable=True),
sa.Column("preview", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
)
op.create_index("ix_workflow_run_artifacts_run_id", "workflow_run_artifacts", ["run_id"])
def downgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
for table in (
"workflow_run_artifacts",
"workflow_run_events",
"workflow_node_runs",
"workflow_runs",
):
if inspector.has_table(table):
op.drop_table(table)
@@ -0,0 +1,49 @@
"""Create workflow_data_sources table.
Revision ID: 20260828_03
Revises: 20260828_02
Create Date: 2026-08-28
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20260828_03"
down_revision = "20260828_02"
branch_labels = None
depends_on = None
def upgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if inspector.has_table("workflow_data_sources"):
return
op.create_table(
"workflow_data_sources",
sa.Column("id", sa.String(length=64), primary_key=True),
sa.Column("name", sa.String(length=191), nullable=False),
sa.Column("description", sa.String(length=512), nullable=False, server_default=""),
sa.Column("kind", sa.String(length=32), nullable=False, server_default="sql"),
sa.Column("driver", sa.String(length=64), nullable=False, server_default=""),
sa.Column("secret_ciphertext", sa.Text(), nullable=False),
sa.Column("secret_encrypted", sa.Boolean(), nullable=False, server_default=sa.text("1")),
sa.Column("masked_target", sa.String(length=512), nullable=False, server_default=""),
sa.Column("max_rows", sa.Integer(), nullable=False, server_default="1000"),
sa.Column("allowed_tables", sa.Text(), nullable=True),
sa.Column("enabled", sa.Boolean(), nullable=False, server_default=sa.text("1")),
sa.Column("owner_id", sa.String(length=64), nullable=True),
sa.Column("created_by", sa.String(length=64), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
sa.UniqueConstraint("name", name="uq_workflow_data_sources_name"),
)
op.create_index("ix_workflow_data_sources_owner_id", "workflow_data_sources", ["owner_id"])
def downgrade() -> None:
bind = op.get_bind()
if sa.inspect(bind).has_table("workflow_data_sources"):
op.drop_table("workflow_data_sources")
@@ -0,0 +1,66 @@
"""Add workflow counter columns: version allocator + event seq counter.
Revision ID: 20260828_04
Revises: 20260828_03
Create Date: 2026-08-28
Functional-robustness remediation (audit items P1-02 / P1-03):
* ``workflow_definitions.next_version_number`` — publish allocates the version
number by CAS-bumping this counter instead of ``MAX(version_number) + 1``,
so two concurrent publishes get adjacent distinct numbers instead of a
unique-constraint 500.
* ``workflow_runs.next_event_seq`` — the event log draws each event's ``seq``
from this counter in the insert's own transaction instead of ``MAX(seq)+1``.
Both columns carry a ``server_default`` so existing databases get them via the
startup ``_ensure_orm_columns_sync`` sweep even without running Alembic; rows
keep the default (1 / 0) and the first write self-heals from the existing
maximum on unique-conflict.
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20260828_04"
down_revision = "20260828_03"
branch_labels = None
depends_on = None
def upgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if inspector.has_table("workflow_definitions"):
existing = {c["name"] for c in inspector.get_columns("workflow_definitions")}
if "next_version_number" not in existing:
op.add_column(
"workflow_definitions",
sa.Column("next_version_number", sa.Integer(), nullable=False, server_default="1"),
)
if inspector.has_table("workflow_runs"):
existing = {c["name"] for c in inspector.get_columns("workflow_runs")}
if "next_event_seq" not in existing:
op.add_column(
"workflow_runs",
sa.Column("next_event_seq", sa.Integer(), nullable=False, server_default="0"),
)
def downgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if inspector.has_table("workflow_definitions"):
existing = {c["name"] for c in inspector.get_columns("workflow_definitions")}
if "next_version_number" in existing:
op.drop_column("workflow_definitions", "next_version_number")
if inspector.has_table("workflow_runs"):
existing = {c["name"] for c in inspector.get_columns("workflow_runs")}
if "next_event_seq" in existing:
op.drop_column("workflow_runs", "next_event_seq")
@@ -0,0 +1,50 @@
"""Add non-secret HTTP metadata to workflow data sources.
Revision ID: 20260829_01
Revises: 20260828_04
Create Date: 2026-08-29
HTTP request headers remain in the encrypted secret blob. The endpoint and
method allowlist are intentionally separate non-secret fields so the canvas
can show a reusable interface resource without exposing credentials.
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20260829_01"
down_revision = "20260828_04"
branch_labels = None
depends_on = None
def upgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if not inspector.has_table("workflow_data_sources"):
return
existing = {column["name"] for column in inspector.get_columns("workflow_data_sources")}
if "base_url" not in existing:
op.add_column(
"workflow_data_sources",
sa.Column("base_url", sa.String(length=2048), nullable=False, server_default=""),
)
if "allowed_methods" not in existing:
op.add_column(
"workflow_data_sources",
sa.Column("allowed_methods", sa.Text(), nullable=True),
)
def downgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if not inspector.has_table("workflow_data_sources"):
return
existing = {column["name"] for column in inspector.get_columns("workflow_data_sources")}
if "allowed_methods" in existing:
op.drop_column("workflow_data_sources", "allowed_methods")
if "base_url" in existing:
op.drop_column("workflow_data_sources", "base_url")
@@ -0,0 +1,51 @@
"""Add Coze canvas draft column to workflow_definitions.
Revision ID: 20260829_02
Revises: 20260829_01
Create Date: 2026-08-29
The canvas document (positions, Coze form internals) is stored separately from
the executable WorkflowGraph so neither representation can impersonate the
other. ``published_canvas_schema_hash`` records which canvas the current
published version was compiled from, for audit only.
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision = "20260829_02"
down_revision = "20260829_01"
branch_labels = None
depends_on = None
def upgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if not inspector.has_table("workflow_definitions"):
return
existing = {column["name"] for column in inspector.get_columns("workflow_definitions")}
if "draft_canvas_schema_json" not in existing:
op.add_column(
"workflow_definitions",
sa.Column("draft_canvas_schema_json", sa.Text(), nullable=False, server_default="'{}'"),
)
if "published_canvas_schema_hash" not in existing:
op.add_column(
"workflow_definitions",
sa.Column("published_canvas_schema_hash", sa.String(length=64), nullable=True),
)
def downgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if not inspector.has_table("workflow_definitions"):
return
existing = {column["name"] for column in inspector.get_columns("workflow_definitions")}
if "published_canvas_schema_hash" in existing:
op.drop_column("workflow_definitions", "published_canvas_schema_hash")
if "draft_canvas_schema_json" in existing:
op.drop_column("workflow_definitions", "draft_canvas_schema_json")