"""add operation execution ledger Revision ID: add_operation_execution_ledger Revises: feishu_outbox_idempotency Create Date: 2026-03-06 """ from __future__ import annotations from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. revision = "add_operation_execution_ledger" down_revision = "feishu_outbox_idempotency" 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, "operation_execution_ledger"): op.create_table( "operation_execution_ledger", 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("operation_code", sa.String(length=64), nullable=False), sa.Column("project_id", sa.BigInteger(), nullable=True), sa.Column("idempotency_key", sa.String(length=128), nullable=False), sa.Column("trace_id", sa.String(length=64), nullable=True), sa.Column("status", sa.String(length=20), nullable=False, server_default="processing"), sa.Column("payload", sa.JSON(), nullable=True), sa.Column("result_payload", sa.JSON(), nullable=True), sa.Column("entity_type", sa.String(length=64), nullable=True), sa.Column("entity_id", sa.String(length=128), nullable=True), sa.Column("error_message", sa.Text(), nullable=True), sa.ForeignKeyConstraint(["project_id"], ["projects.id"]), sa.UniqueConstraint( "tenant_id", "operation_code", "idempotency_key", name="uq_operation_execution_ledger_key", ), ) if _table_exists(bind, "operation_execution_ledger"): _create_index_if_not_exists( bind, index_name="ix_operation_execution_ledger_tenant_id", table_name="operation_execution_ledger", columns=["tenant_id"], ) _create_index_if_not_exists( bind, index_name="ix_operation_execution_ledger_operation_code", table_name="operation_execution_ledger", columns=["operation_code"], ) _create_index_if_not_exists( bind, index_name="ix_operation_execution_ledger_project_id", table_name="operation_execution_ledger", columns=["project_id"], ) _create_index_if_not_exists( bind, index_name="ix_operation_execution_ledger_trace_id", table_name="operation_execution_ledger", columns=["trace_id"], ) _create_index_if_not_exists( bind, index_name="ix_operation_execution_ledger_status", table_name="operation_execution_ledger", columns=["status"], ) _create_index_if_not_exists( bind, index_name="ix_operation_execution_ledger_entity_type", table_name="operation_execution_ledger", columns=["entity_type"], ) _create_index_if_not_exists( bind, index_name="ix_operation_execution_ledger_entity_id", table_name="operation_execution_ledger", columns=["entity_id"], ) def downgrade() -> None: bind = op.get_bind() if not _table_exists(bind, "operation_execution_ledger"): return _drop_index_if_exists(bind, "ix_operation_execution_ledger_entity_id", "operation_execution_ledger") _drop_index_if_exists(bind, "ix_operation_execution_ledger_entity_type", "operation_execution_ledger") _drop_index_if_exists(bind, "ix_operation_execution_ledger_status", "operation_execution_ledger") _drop_index_if_exists(bind, "ix_operation_execution_ledger_trace_id", "operation_execution_ledger") _drop_index_if_exists(bind, "ix_operation_execution_ledger_project_id", "operation_execution_ledger") _drop_index_if_exists(bind, "ix_operation_execution_ledger_operation_code", "operation_execution_ledger") _drop_index_if_exists(bind, "ix_operation_execution_ledger_tenant_id", "operation_execution_ledger") op.drop_table("operation_execution_ledger")