195 lines
12 KiB
Python
195 lines
12 KiB
Python
"""add ai pmo kernel tables
|
|
|
|
Revision ID: add_ai_pmo_kernel_tables
|
|
Revises: add_ai_processing_audit_fields
|
|
Create Date: 2026-03-08
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
revision = "add_ai_pmo_kernel_tables"
|
|
down_revision = "add_ai_processing_audit_fields"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def _table_exists(bind, table_name: str) -> bool:
|
|
return sa.inspect(bind).has_table(table_name)
|
|
|
|
|
|
def _index_exists(bind, table_name: str, index_name: str) -> bool:
|
|
inspector = sa.inspect(bind)
|
|
if not inspector.has_table(table_name):
|
|
return False
|
|
return any(idx.get("name") == index_name for idx in inspector.get_indexes(table_name))
|
|
|
|
|
|
def _create_index_if_missing(bind, table_name: str, index_name: str, columns: list[str], *, unique: bool = False) -> None:
|
|
if not _index_exists(bind, table_name, index_name):
|
|
op.create_index(index_name, table_name, columns, unique=unique)
|
|
|
|
|
|
def upgrade() -> None:
|
|
bind = op.get_bind()
|
|
|
|
if not _table_exists(bind, "work_items"):
|
|
op.create_table(
|
|
"work_items",
|
|
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("ingest_event_id", sa.BigInteger(), sa.ForeignKey("ingest_events.id", ondelete="CASCADE"), nullable=False),
|
|
sa.Column("source_system", sa.String(length=32), nullable=False),
|
|
sa.Column("source_message_id", sa.String(length=128), nullable=False),
|
|
sa.Column("state", sa.String(length=32), nullable=False, server_default="received"),
|
|
sa.Column("intent", sa.String(length=64), nullable=True),
|
|
sa.Column("decision_level", sa.String(length=20), nullable=True),
|
|
sa.Column("project_id", sa.BigInteger(), sa.ForeignKey("projects.id"), nullable=True),
|
|
sa.Column("signal_id", sa.BigInteger(), sa.ForeignKey("signals.id"), nullable=True),
|
|
sa.Column("owner_user_id", sa.BigInteger(), sa.ForeignKey("users.id"), nullable=True),
|
|
sa.Column("dedupe_key", sa.String(length=255), nullable=True),
|
|
sa.Column("context_snapshot", sa.JSON(), nullable=True),
|
|
sa.Column("action_bundle", sa.JSON(), nullable=True),
|
|
sa.Column("execution_result", sa.JSON(), nullable=True),
|
|
sa.Column("review_summary", sa.JSON(), nullable=True),
|
|
sa.Column("reply_template_key", sa.String(length=64), nullable=True),
|
|
sa.UniqueConstraint("tenant_id", "source_system", "source_message_id", name="uq_work_items_source_message"),
|
|
sa.UniqueConstraint("tenant_id", "ingest_event_id", name="uq_work_items_ingest_event"),
|
|
)
|
|
|
|
if not _table_exists(bind, "decision_records"):
|
|
op.create_table(
|
|
"decision_records",
|
|
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("work_item_id", sa.BigInteger(), sa.ForeignKey("work_items.id", ondelete="CASCADE"), nullable=False),
|
|
sa.Column("decision_object_type", sa.String(length=40), nullable=False, server_default="portfolio"),
|
|
sa.Column("decision_object_id", sa.BigInteger(), nullable=True),
|
|
sa.Column("decision_level", sa.String(length=20), nullable=False, server_default="portfolio"),
|
|
sa.Column("context_snapshot", sa.JSON(), nullable=True),
|
|
sa.Column("rationale", sa.Text(), nullable=True),
|
|
sa.Column("confidence", sa.Float(), nullable=True),
|
|
sa.Column("risk_score", sa.Float(), nullable=True),
|
|
sa.Column("budget_impact", sa.JSON(), nullable=True),
|
|
sa.Column("resource_impact", sa.JSON(), nullable=True),
|
|
sa.Column("policy_gate_hit", sa.Boolean(), nullable=False, server_default=sa.false()),
|
|
sa.Column("policy_gate_code", sa.String(length=80), nullable=True),
|
|
sa.Column("action_bundle", sa.JSON(), nullable=True),
|
|
sa.Column("approval_requirement", sa.JSON(), nullable=True),
|
|
sa.Column("execution_result", sa.JSON(), nullable=True),
|
|
sa.Column("final_outcome", sa.String(length=32), nullable=True),
|
|
)
|
|
|
|
if not _table_exists(bind, "plan_bundles"):
|
|
op.create_table(
|
|
"plan_bundles",
|
|
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("work_item_id", sa.BigInteger(), sa.ForeignKey("work_items.id", ondelete="CASCADE"), nullable=False),
|
|
sa.Column("plan_type", sa.String(length=20), nullable=False),
|
|
sa.Column("summary", sa.Text(), nullable=True),
|
|
sa.Column("payload", sa.JSON(), nullable=True),
|
|
)
|
|
|
|
if not _table_exists(bind, "reply_receipts"):
|
|
op.create_table(
|
|
"reply_receipts",
|
|
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("work_item_id", sa.BigInteger(), sa.ForeignKey("work_items.id", ondelete="SET NULL"), nullable=True),
|
|
sa.Column("source_system", sa.String(length=32), nullable=False),
|
|
sa.Column("source_message_id", sa.String(length=128), nullable=False),
|
|
sa.Column("receipt_phase", sa.String(length=32), nullable=False),
|
|
sa.Column("template_key", sa.String(length=64), nullable=True),
|
|
sa.Column("payload", sa.JSON(), nullable=True),
|
|
sa.Column("status", sa.String(length=20), nullable=False, server_default="pending"),
|
|
sa.UniqueConstraint(
|
|
"tenant_id",
|
|
"source_system",
|
|
"source_message_id",
|
|
"receipt_phase",
|
|
name="uq_reply_receipts_source_phase",
|
|
),
|
|
)
|
|
|
|
if not _table_exists(bind, "portfolio_snapshots"):
|
|
op.create_table(
|
|
"portfolio_snapshots",
|
|
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("scope_type", sa.String(length=20), nullable=False, server_default="tenant"),
|
|
sa.Column("scope_ref", sa.String(length=64), nullable=False),
|
|
sa.Column("summary", sa.JSON(), nullable=True),
|
|
sa.Column("payload", sa.JSON(), nullable=True),
|
|
)
|
|
|
|
for table_name, columns in (
|
|
("work_items", ["tenant_id"]),
|
|
("decision_records", ["tenant_id"]),
|
|
("plan_bundles", ["tenant_id"]),
|
|
("reply_receipts", ["tenant_id"]),
|
|
("portfolio_snapshots", ["tenant_id"]),
|
|
):
|
|
_create_index_if_missing(bind, table_name, f"ix_{table_name}_tenant_id", columns)
|
|
|
|
for table_name, index_name, columns in (
|
|
("work_items", "ix_work_items_source_system", ["source_system"]),
|
|
("work_items", "ix_work_items_source_message_id", ["source_message_id"]),
|
|
("work_items", "ix_work_items_state", ["state"]),
|
|
("work_items", "ix_work_items_intent", ["intent"]),
|
|
("work_items", "ix_work_items_decision_level", ["decision_level"]),
|
|
("work_items", "ix_work_items_project_id", ["project_id"]),
|
|
("work_items", "ix_work_items_signal_id", ["signal_id"]),
|
|
("work_items", "ix_work_items_owner_user_id", ["owner_user_id"]),
|
|
("work_items", "ix_work_items_dedupe_key", ["dedupe_key"]),
|
|
("work_items", "ix_work_items_ingest_event_id", ["ingest_event_id"]),
|
|
("decision_records", "ix_decision_records_work_item_id", ["work_item_id"]),
|
|
("decision_records", "ix_decision_records_decision_object_id", ["decision_object_id"]),
|
|
("decision_records", "ix_decision_records_decision_level", ["decision_level"]),
|
|
("decision_records", "ix_decision_records_policy_gate_hit", ["policy_gate_hit"]),
|
|
("decision_records", "ix_decision_records_policy_gate_code", ["policy_gate_code"]),
|
|
("decision_records", "ix_decision_records_final_outcome", ["final_outcome"]),
|
|
("plan_bundles", "ix_plan_bundles_work_item_id", ["work_item_id"]),
|
|
("plan_bundles", "ix_plan_bundles_plan_type", ["plan_type"]),
|
|
("reply_receipts", "ix_reply_receipts_work_item_id", ["work_item_id"]),
|
|
("reply_receipts", "ix_reply_receipts_source_system", ["source_system"]),
|
|
("reply_receipts", "ix_reply_receipts_source_message_id", ["source_message_id"]),
|
|
("reply_receipts", "ix_reply_receipts_receipt_phase", ["receipt_phase"]),
|
|
("reply_receipts", "ix_reply_receipts_status", ["status"]),
|
|
("portfolio_snapshots", "ix_portfolio_snapshots_scope_type", ["scope_type"]),
|
|
("portfolio_snapshots", "ix_portfolio_snapshots_scope_ref", ["scope_ref"]),
|
|
):
|
|
_create_index_if_missing(bind, table_name, index_name, columns)
|
|
|
|
|
|
def downgrade() -> None:
|
|
bind = op.get_bind()
|
|
for table_name, indexes in (
|
|
("portfolio_snapshots", ["ix_portfolio_snapshots_scope_ref", "ix_portfolio_snapshots_scope_type", "ix_portfolio_snapshots_tenant_id"]),
|
|
("reply_receipts", ["ix_reply_receipts_status", "ix_reply_receipts_receipt_phase", "ix_reply_receipts_source_message_id", "ix_reply_receipts_source_system", "ix_reply_receipts_work_item_id", "ix_reply_receipts_tenant_id"]),
|
|
("plan_bundles", ["ix_plan_bundles_plan_type", "ix_plan_bundles_work_item_id", "ix_plan_bundles_tenant_id"]),
|
|
("decision_records", ["ix_decision_records_final_outcome", "ix_decision_records_policy_gate_code", "ix_decision_records_policy_gate_hit", "ix_decision_records_decision_level", "ix_decision_records_decision_object_id", "ix_decision_records_work_item_id", "ix_decision_records_tenant_id"]),
|
|
("work_items", ["ix_work_items_ingest_event_id", "ix_work_items_dedupe_key", "ix_work_items_owner_user_id", "ix_work_items_signal_id", "ix_work_items_project_id", "ix_work_items_decision_level", "ix_work_items_intent", "ix_work_items_state", "ix_work_items_source_message_id", "ix_work_items_source_system", "ix_work_items_tenant_id"]),
|
|
):
|
|
if not _table_exists(bind, table_name):
|
|
continue
|
|
for index_name in indexes:
|
|
if _index_exists(bind, table_name, index_name):
|
|
op.drop_index(index_name, table_name=table_name)
|
|
for table_name in ("portfolio_snapshots", "reply_receipts", "plan_bundles", "decision_records", "work_items"):
|
|
if _table_exists(bind, table_name):
|
|
op.drop_table(table_name)
|