Add upstream_key_account_links table mapping generated keys to remote accounts per website/platform, surface imported_accounts on key responses, and update website sync/routers to manage the links.
60 lines
3.6 KiB
Python
60 lines
3.6 KiB
Python
from datetime import datetime, timezone
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import DateTime, ForeignKey, Index, Integer, String, Text, UniqueConstraint
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class UpstreamGeneratedKey(Base):
|
|
__tablename__ = "upstream_generated_keys"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
|
upstream_id: Mapped[int] = mapped_column(Integer, ForeignKey("upstreams.id", ondelete="CASCADE"), index=True)
|
|
group_id: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
|
|
group_name: Mapped[str] = mapped_column(String(255), default="")
|
|
key_id: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
key_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
key_value: Mapped[str] = mapped_column(Text, nullable=False)
|
|
masked_key: Mapped[str] = mapped_column(String(255), default="")
|
|
raw_json: Mapped[str] = mapped_column(Text, default="{}")
|
|
managed_prefix: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, index=True)
|
|
status: Mapped[str] = mapped_column(String(32), default="created")
|
|
error: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
imported_website_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("websites.id", ondelete="SET NULL"), nullable=True, index=True)
|
|
imported_account_id: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
imported_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
imported_target_group_id: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
imported_target_group_name: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=lambda: datetime.now(timezone.utc))
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
|
|
|
|
account_links = relationship("UpstreamKeyAccountLink", back_populates="upstream_key", cascade="all, delete-orphan")
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint("upstream_id", "group_id", "key_name", name="uq_upstream_group_key"),
|
|
Index("ix_key_upstream_name", "upstream_id", "key_name"),
|
|
)
|
|
|
|
|
|
class UpstreamKeyAccountLink(Base):
|
|
__tablename__ = "upstream_key_account_links"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
|
upstream_key_id: Mapped[int] = mapped_column(Integer, ForeignKey("upstream_generated_keys.id", ondelete="CASCADE"), index=True, nullable=False)
|
|
website_id: Mapped[int] = mapped_column(Integer, ForeignKey("websites.id", ondelete="CASCADE"), index=True, nullable=False)
|
|
target_group: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
platform: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
remote_account_id: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
status: Mapped[str] = mapped_column(String(32), default="active", nullable=False)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, default=lambda: datetime.now(timezone.utc))
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
|
|
|
|
upstream_key = relationship("UpstreamGeneratedKey", back_populates="account_links")
|
|
|
|
__table_args__ = (
|
|
UniqueConstraint("upstream_key_id", "website_id", "platform", name="uq_key_website_platform"),
|
|
UniqueConstraint("website_id", "remote_account_id", name="uq_website_remote_account"),
|
|
)
|