You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import json
  2. from datetime import datetime
  3. import sqlalchemy as sa
  4. from sqlalchemy import DateTime, String, func
  5. from sqlalchemy.dialects.postgresql import JSONB
  6. from sqlalchemy.orm import Mapped, mapped_column
  7. from models.base import Base
  8. from .types import StringUUID
  9. class DataSourceOauthBinding(Base):
  10. __tablename__ = "data_source_oauth_bindings"
  11. __table_args__ = (
  12. sa.PrimaryKeyConstraint("id", name="source_binding_pkey"),
  13. sa.Index("source_binding_tenant_id_idx", "tenant_id"),
  14. sa.Index("source_info_idx", "source_info", postgresql_using="gin"),
  15. )
  16. id = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"))
  17. tenant_id = mapped_column(StringUUID, nullable=False)
  18. access_token: Mapped[str] = mapped_column(String(255), nullable=False)
  19. provider: Mapped[str] = mapped_column(String(255), nullable=False)
  20. source_info = mapped_column(JSONB, nullable=False)
  21. created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
  22. updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
  23. disabled: Mapped[bool | None] = mapped_column(sa.Boolean, nullable=True, server_default=sa.text("false"))
  24. class DataSourceApiKeyAuthBinding(Base):
  25. __tablename__ = "data_source_api_key_auth_bindings"
  26. __table_args__ = (
  27. sa.PrimaryKeyConstraint("id", name="data_source_api_key_auth_binding_pkey"),
  28. sa.Index("data_source_api_key_auth_binding_tenant_id_idx", "tenant_id"),
  29. sa.Index("data_source_api_key_auth_binding_provider_idx", "provider"),
  30. )
  31. id = mapped_column(StringUUID, server_default=sa.text("uuid_generate_v4()"))
  32. tenant_id = mapped_column(StringUUID, nullable=False)
  33. category: Mapped[str] = mapped_column(String(255), nullable=False)
  34. provider: Mapped[str] = mapped_column(String(255), nullable=False)
  35. credentials = mapped_column(sa.Text, nullable=True) # JSON
  36. created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
  37. updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
  38. disabled: Mapped[bool | None] = mapped_column(sa.Boolean, nullable=True, server_default=sa.text("false"))
  39. def to_dict(self):
  40. return {
  41. "id": self.id,
  42. "tenant_id": self.tenant_id,
  43. "category": self.category,
  44. "provider": self.provider,
  45. "credentials": json.loads(self.credentials),
  46. "created_at": self.created_at.timestamp(),
  47. "updated_at": self.updated_at.timestamp(),
  48. "disabled": self.disabled,
  49. }