"""操作执行幂等账本。""" from __future__ import annotations from typing import Optional from sqlalchemy import BigInteger, Integer, ForeignKey, Text, UniqueConstraint, VARCHAR from sqlalchemy.orm import Mapped, mapped_column from .base import Base from .types import JSONB class OperationExecutionLedger(Base): __tablename__ = "operation_execution_ledger" __table_args__ = ( UniqueConstraint( "tenant_id", "operation_code", "idempotency_key", name="uq_operation_execution_ledger_key", ), ) id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) operation_code: Mapped[str] = mapped_column(VARCHAR(64), nullable=False, index=True) project_id: Mapped[Optional[int]] = mapped_column( BigInteger, ForeignKey("projects.id"), nullable=True, index=True ) idempotency_key: Mapped[str] = mapped_column(VARCHAR(128), nullable=False) trace_id: Mapped[Optional[str]] = mapped_column(VARCHAR(64), nullable=True, index=True) status: Mapped[str] = mapped_column(VARCHAR(20), nullable=False, default="processing", index=True) payload: Mapped[Optional[dict]] = mapped_column(JSONB, nullable=True) result_payload: Mapped[Optional[dict]] = mapped_column(JSONB, nullable=True) entity_type: Mapped[Optional[str]] = mapped_column(VARCHAR(64), nullable=True, index=True) entity_id: Mapped[Optional[str]] = mapped_column(VARCHAR(128), nullable=True, index=True) error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)