from __future__ import annotations from datetime import datetime from typing import Any import uuid from sqlalchemy import DateTime, Index, JSON, String, Text, UniqueConstraint from sqlalchemy.orm import Mapped, mapped_column from govoplan_core.db.base import Base, TimestampMixin def new_uuid() -> str: return str(uuid.uuid4()) class FormDefinitionRevision(Base, TimestampMixin): __tablename__ = "form_definition_revisions" __table_args__ = ( UniqueConstraint( "tenant_id", "form_id", "revision", name="uq_form_definition_revision", ), Index( "ix_form_definition_current", "tenant_id", "form_id", "superseded_at", ), Index( "ix_form_definition_catalog", "tenant_id", "publication_state", "form_key", ), ) id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid) tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True) form_id: Mapped[str] = mapped_column(String(255), nullable=False, index=True) form_key: Mapped[str] = mapped_column(String(255), nullable=False, index=True) revision: Mapped[str] = mapped_column(String(255), nullable=False, index=True) previous_revision_id: Mapped[str | None] = mapped_column( String(36), nullable=True, index=True ) publication_state: Mapped[str] = mapped_column( String(30), nullable=False, index=True ) title: Mapped[str] = mapped_column(String(500), nullable=False) recorded_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), nullable=False, index=True ) superseded_at: Mapped[datetime | None] = mapped_column( DateTime(timezone=True), nullable=True, index=True ) search_text: Mapped[str] = mapped_column(Text, nullable=False) payload: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False) changed_by: Mapped[str | None] = mapped_column( String(255), nullable=True, index=True ) __all__ = ["FormDefinitionRevision"]