"""`audit_trail`: bitácora append-only de toda mutación (entidad, acción, antes/después).""" from datetime import datetime from sqlalchemy import JSON, DateTime, Index, Integer, String, Text from sqlalchemy.orm import Mapped, mapped_column from app.models import Base, ColumnasBaseMixin, TenantMixin, ahora_utc class AuditTrail(ColumnasBaseMixin, TenantMixin, Base): __tablename__ = "audit_trail" __table_args__ = (Index("ix_audit_trail_entidad", "tenant_id", "entidad", "entidad_id"),) entidad: Mapped[str] = mapped_column(String(64), nullable=False) entidad_id: Mapped[int | None] = mapped_column(Integer, nullable=True) accion: Mapped[str] = mapped_column(String(128), nullable=False) antes: Mapped[dict | None] = mapped_column(JSON, nullable=True) despues: Mapped[dict | None] = mapped_column(JSON, nullable=True) usuario: Mapped[str] = mapped_column(String(64), nullable=False) fecha: Mapped[datetime] = mapped_column( DateTime(timezone=True), nullable=False, default=ahora_utc ) motivo: Mapped[str | None] = mapped_column(Text, nullable=True)