from __future__ import annotations import uuid from typing import Any from sqlalchemy import Index, Integer, JSON, String, UniqueConstraint from sqlalchemy.orm import Mapped, mapped_column from govoplan_core.db.base import Base, TimestampMixin class PolicyOverride(Base, TimestampMixin): __tablename__ = "policy_overrides" __table_args__ = ( UniqueConstraint( "policy_family", "target_key", "scope_key", name="uq_policy_override_family_target_scope", ), Index( "ix_policy_overrides_resolution", "policy_family", "target_key", "tenant_id", "scope_type", "scope_id", ), ) id: Mapped[str] = mapped_column( String(36), primary_key=True, default=lambda: str(uuid.uuid4()), ) policy_family: Mapped[str] = mapped_column(String(40), nullable=False) target_key: Mapped[str] = mapped_column(String(120), nullable=False) tenant_id: Mapped[str | None] = mapped_column(String(36), nullable=True, index=True) scope_type: Mapped[str] = mapped_column(String(20), nullable=False) scope_id: Mapped[str | None] = mapped_column(String(255), nullable=True, index=True) scope_key: Mapped[str] = mapped_column(String(320), nullable=False) policy: Mapped[Any] = mapped_column(JSON, default=dict, nullable=False) revision: Mapped[int] = mapped_column(Integer, default=1, nullable=False) created_by: Mapped[str | None] = mapped_column( String(255), nullable=True, index=True ) updated_by: Mapped[str | None] = mapped_column( String(255), nullable=True, index=True ) __all__ = ["PolicyOverride"]