115 lines
5.0 KiB
Python
115 lines
5.0 KiB
Python
"""add ai orchestration runs
|
|
|
|
Revision ID: add_ai_orchestration_runs
|
|
Revises: add_signal_source_orchestration_tables
|
|
Create Date: 2026-03-07
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
revision = "add_ai_orchestration_runs"
|
|
down_revision = "add_signal_source_orchestration_tables"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def _table_exists(bind, table_name: str) -> bool:
|
|
inspector = sa.inspect(bind)
|
|
return inspector.has_table(table_name)
|
|
|
|
|
|
def _index_exists(bind, table_name: str, index_name: str) -> bool:
|
|
inspector = sa.inspect(bind)
|
|
indexes = inspector.get_indexes(table_name)
|
|
return any(idx.get("name") == index_name for idx in indexes)
|
|
|
|
|
|
def _create_index_if_not_exists(
|
|
bind,
|
|
*,
|
|
index_name: str,
|
|
table_name: str,
|
|
columns: list[str],
|
|
unique: bool = False,
|
|
) -> None:
|
|
if _index_exists(bind, table_name, index_name):
|
|
return
|
|
op.create_index(index_name, table_name, columns, unique=unique)
|
|
|
|
|
|
def _drop_index_if_exists(bind, index_name: str, table_name: str) -> None:
|
|
if _index_exists(bind, table_name, index_name):
|
|
op.drop_index(index_name, table_name=table_name)
|
|
|
|
|
|
def upgrade() -> None:
|
|
bind = op.get_bind()
|
|
|
|
if not _table_exists(bind, "ai_orchestration_runs"):
|
|
op.create_table(
|
|
"ai_orchestration_runs",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
sa.Column("tenant_id", sa.BigInteger(), nullable=False, server_default="1"),
|
|
sa.Column("created_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now()),
|
|
sa.Column("updated_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now(), onupdate=sa.func.now()),
|
|
sa.Column("trace_id", sa.String(length=64), nullable=False),
|
|
sa.Column("run_type", sa.String(length=40), nullable=False),
|
|
sa.Column("project_id", sa.BigInteger(), nullable=False),
|
|
sa.Column("signal_id", sa.BigInteger(), nullable=True),
|
|
sa.Column("response_mode", sa.String(length=16), nullable=False, server_default="fast"),
|
|
sa.Column("status", sa.String(length=20), nullable=False, server_default="queued"),
|
|
sa.Column("planner_status", sa.String(length=20), nullable=True),
|
|
sa.Column("openclaw_status", sa.String(length=20), nullable=True),
|
|
sa.Column("worker_total", sa.Integer(), nullable=False, server_default="0"),
|
|
sa.Column("worker_completed", sa.Integer(), nullable=False, server_default="0"),
|
|
sa.Column("worker_failed", sa.Integer(), nullable=False, server_default="0"),
|
|
sa.Column("summary", sa.Text(), nullable=True),
|
|
sa.Column("planner_diagnostics", sa.JSON(), nullable=True),
|
|
sa.Column("result_payload", sa.JSON(), nullable=True),
|
|
sa.Column("created_by_user_id", sa.BigInteger(), nullable=True),
|
|
sa.Column("started_at", sa.TIMESTAMP(), nullable=True),
|
|
sa.Column("completed_at", sa.TIMESTAMP(), nullable=True),
|
|
sa.ForeignKeyConstraint(["project_id"], ["projects.id"]),
|
|
sa.ForeignKeyConstraint(["signal_id"], ["signals.id"]),
|
|
sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"]),
|
|
sa.UniqueConstraint("tenant_id", "trace_id", name="uq_ai_orchestration_run_trace_id"),
|
|
)
|
|
|
|
if _table_exists(bind, "ai_orchestration_runs"):
|
|
for index_name, columns in (
|
|
("ix_ai_orchestration_runs_tenant_id", ["tenant_id"]),
|
|
("ix_ai_orchestration_runs_trace_id", ["trace_id"]),
|
|
("ix_ai_orchestration_runs_run_type", ["run_type"]),
|
|
("ix_ai_orchestration_runs_project_id", ["project_id"]),
|
|
("ix_ai_orchestration_runs_signal_id", ["signal_id"]),
|
|
("ix_ai_orchestration_runs_response_mode", ["response_mode"]),
|
|
("ix_ai_orchestration_runs_status", ["status"]),
|
|
("ix_ai_orchestration_runs_planner_status", ["planner_status"]),
|
|
("ix_ai_orchestration_runs_openclaw_status", ["openclaw_status"]),
|
|
("ix_ai_orchestration_runs_created_by_user_id", ["created_by_user_id"]),
|
|
):
|
|
_create_index_if_not_exists(bind, index_name=index_name, table_name="ai_orchestration_runs", columns=columns)
|
|
|
|
|
|
def downgrade() -> None:
|
|
bind = op.get_bind()
|
|
if _table_exists(bind, "ai_orchestration_runs"):
|
|
for index_name in (
|
|
"ix_ai_orchestration_runs_created_by_user_id",
|
|
"ix_ai_orchestration_runs_openclaw_status",
|
|
"ix_ai_orchestration_runs_planner_status",
|
|
"ix_ai_orchestration_runs_status",
|
|
"ix_ai_orchestration_runs_response_mode",
|
|
"ix_ai_orchestration_runs_signal_id",
|
|
"ix_ai_orchestration_runs_project_id",
|
|
"ix_ai_orchestration_runs_run_type",
|
|
"ix_ai_orchestration_runs_trace_id",
|
|
"ix_ai_orchestration_runs_tenant_id",
|
|
):
|
|
_drop_index_if_exists(bind, index_name, "ai_orchestration_runs")
|
|
op.drop_table("ai_orchestration_runs")
|