state-hub/migrations/versions/e8f9a0b1c2d3_task_wait_qualifiers.py

104 lines
3.8 KiB
Python
Raw Normal View History

CUST-WP-0074-T02: task wait qualifiers in the hub read model Adds tasks.decision_id and workplan_dependencies.from_task_id (migration e8f9a0b1c2d3). A from_task_id column is needed rather than from_workplan_id plus description: the partial unique indexes key on (from_workplan_id, target, relationship_type), so two tasks in one workplan waiting on the same target could not both be indexed, and the derived wait_kind needs per-task attribution. The two existing unique indexes are narrowed to frontmatter edges (from_task_id IS NULL) and two task-origin counterparts are added; downgrade deletes task-origin rows before restoring the old indexes. POST /workplans/{id}/dependencies/ accepts from_task_id (must belong to the from workplan; a task cannot depend on itself). TaskCreate/Update/Read carry decision_id. Derived read-model fields, no new write routes (rule 5): - TaskRead.wait_kind: external | human | both | unqualified, null unless status is wait (dependency rows from this task / needs_human). - WorkplanRead.blocked_kind: human | external | none, null unless status is blocked. Human wins over external. Both come from api/services/wait_kind.py, applied on GET /tasks/, GET /tasks/{id}, GET /workplans/ and GET /workplans/{id}. The identifier-migration reference-count proof now includes from_task_id (FK count guard 22 -> 23). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Assistant: claude-code Assistant-Model: sonnet Assistant-Process: 237582@bnt-lap001 Assistant-Session: f2b3d9f1-8fb9-4b9c-bc2b-837ec5dfc826
2026-09-28 22:39:18 +02:00
"""task wait qualifiers (CUST-WP-0074-T02)
Adds ``tasks.decision_id`` (record id of the decision a human-gated wait hangs
on) and ``workplan_dependencies.from_task_id`` so that task-block
``depends_on`` edges carry the waiting task as the from side. The two existing
partial unique indexes are narrowed to frontmatter edges (``from_task_id IS
NULL``) and two task-origin counterparts are added, so two tasks in one
workplan may wait on the same target.
Revision ID: e8f9a0b1c2d3
Revises: d7e8f9a0b1c2
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
revision = "e8f9a0b1c2d3"
down_revision = "d7e8f9a0b1c2"
branch_labels = None
depends_on = None
def upgrade() -> None:
op.add_column("tasks", sa.Column("decision_id", sa.String(length=120), nullable=True))
op.add_column(
"workplan_dependencies",
sa.Column("from_task_id", postgresql.UUID(as_uuid=True), nullable=True),
)
op.create_foreign_key(
"fk_workplan_dependencies_from_task_id",
"workplan_dependencies",
"tasks",
["from_task_id"],
["id"],
ondelete="CASCADE",
onupdate="CASCADE",
)
op.create_index(
"ix_workplan_dependencies_from_task_id",
"workplan_dependencies",
["from_task_id"],
)
op.drop_index("uq_wp_dep_workplan_target", table_name="workplan_dependencies")
op.drop_index("uq_wp_dep_task_target", table_name="workplan_dependencies")
op.create_index(
"uq_wp_dep_workplan_target",
"workplan_dependencies",
["from_workplan_id", "to_workplan_id", "relationship_type"],
unique=True,
postgresql_where=sa.text("to_workplan_id IS NOT NULL AND from_task_id IS NULL"),
)
op.create_index(
"uq_wp_dep_task_target",
"workplan_dependencies",
["from_workplan_id", "to_task_id", "relationship_type"],
unique=True,
postgresql_where=sa.text("to_task_id IS NOT NULL AND from_task_id IS NULL"),
)
op.create_index(
"uq_wp_dep_from_task_workplan_target",
"workplan_dependencies",
["from_task_id", "to_workplan_id", "relationship_type"],
unique=True,
postgresql_where=sa.text("to_workplan_id IS NOT NULL AND from_task_id IS NOT NULL"),
)
op.create_index(
"uq_wp_dep_from_task_task_target",
"workplan_dependencies",
["from_task_id", "to_task_id", "relationship_type"],
unique=True,
postgresql_where=sa.text("to_task_id IS NOT NULL AND from_task_id IS NOT NULL"),
)
def downgrade() -> None:
op.drop_index("uq_wp_dep_from_task_task_target", table_name="workplan_dependencies")
op.drop_index("uq_wp_dep_from_task_workplan_target", table_name="workplan_dependencies")
op.drop_index("uq_wp_dep_task_target", table_name="workplan_dependencies")
op.drop_index("uq_wp_dep_workplan_target", table_name="workplan_dependencies")
# Task-origin rows would collide under the frontmatter-only indexes.
op.execute("DELETE FROM workplan_dependencies WHERE from_task_id IS NOT NULL")
op.create_index(
"uq_wp_dep_workplan_target",
"workplan_dependencies",
["from_workplan_id", "to_workplan_id", "relationship_type"],
unique=True,
postgresql_where=sa.text("to_workplan_id IS NOT NULL"),
)
op.create_index(
"uq_wp_dep_task_target",
"workplan_dependencies",
["from_workplan_id", "to_task_id", "relationship_type"],
unique=True,
postgresql_where=sa.text("to_task_id IS NOT NULL"),
)
op.drop_index("ix_workplan_dependencies_from_task_id", table_name="workplan_dependencies")
op.drop_constraint(
"fk_workplan_dependencies_from_task_id", "workplan_dependencies", type_="foreignkey"
)
op.drop_column("workplan_dependencies", "from_task_id")
op.drop_column("tasks", "decision_id")