"""Connector configuration and uploaded table rows (RLS forced; migration 0010). ``connector_configs.config`` holds NON-secret settings only (spreadsheet id, Nango connection id, columns, webhook mapping, HTTP action template); ``secret_ref`` names the secret (e.g. the Nango secret key) resolved by the gateway at execution time. """ from __future__ import annotations import uuid from datetime import datetime from typing import Any from sqlalchemy import ( Boolean, CheckConstraint, DateTime, ForeignKey, String, UniqueConstraint, func, ) from sqlalchemy.dialects.postgresql import JSONB, UUID from sqlalchemy.orm import Mapped, mapped_column from core.db.base import Base, TenantMixin, TimestampMixin, new_id KINDS = ("google_sheets", "table", "webhook", "http_action") class ConnectorConfig(TenantMixin, TimestampMixin, Base): __tablename__ = "connector_configs" __table_args__ = ( UniqueConstraint("tenant_id", "name", name="uq_connector_configs_tenant_name"), CheckConstraint( "kind IN ('google_sheets', 'table', 'webhook', 'http_action')", name="kind" ), ) id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id) kind: Mapped[str] = mapped_column(String(20), nullable=False) name: Mapped[str] = mapped_column(String(40), nullable=False) config: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False, default=dict) secret_ref: Mapped[str | None] = mapped_column(String(120), nullable=True) enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True) updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False ) class ConnectorRow(TenantMixin, TimestampMixin, Base): """One row of an uploaded CSV/Excel table, addressed by its key column value.""" __tablename__ = "connector_rows" __table_args__ = ( UniqueConstraint("connector_id", "row_key", name="uq_connector_rows_connector_key"), ) id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=new_id) connector_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("connector_configs.id", ondelete="CASCADE"), nullable=False, ) row_key: Mapped[str] = mapped_column(String(200), nullable=False) data: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False, default=dict) updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False )