feat(psa): add PsaConnection model and migration 058
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -19,6 +19,7 @@ from app.models.survey_invite import SurveyInvite
|
|||||||
from app.models.ai_suggestion import AISuggestion # noqa: F401
|
from app.models.ai_suggestion import AISuggestion # noqa: F401
|
||||||
from app.models.kb_import import KBImport, KBImportNode # noqa: F401
|
from app.models.kb_import import KBImport, KBImportNode # noqa: F401
|
||||||
from app.models.script_template import ScriptCategory, ScriptTemplate, ScriptGeneration # noqa: F401
|
from app.models.script_template import ScriptCategory, ScriptTemplate, ScriptGeneration # noqa: F401
|
||||||
|
from app.models.psa_connection import PsaConnection # noqa: F401
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
|
|
||||||
# this is the Alembic Config object
|
# this is the Alembic Config object
|
||||||
|
|||||||
39
backend/alembic/versions/058_add_psa_connections.py
Normal file
39
backend/alembic/versions/058_add_psa_connections.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
"""Add psa_connections table.
|
||||||
|
|
||||||
|
Revision ID: 058
|
||||||
|
Revises: 057
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
revision = "058"
|
||||||
|
down_revision = "057"
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
op.create_table(
|
||||||
|
"psa_connections",
|
||||||
|
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
|
||||||
|
sa.Column("account_id", postgresql.UUID(as_uuid=True), nullable=False),
|
||||||
|
sa.Column("provider", sa.String(50), nullable=False),
|
||||||
|
sa.Column("display_name", sa.String(100), nullable=False),
|
||||||
|
sa.Column("site_url", sa.String(255), nullable=False),
|
||||||
|
sa.Column("company_id", sa.String(100), nullable=False),
|
||||||
|
sa.Column("credentials_encrypted", sa.Text(), nullable=False),
|
||||||
|
sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.text("true")),
|
||||||
|
sa.Column("last_validated_at", sa.DateTime(timezone=True), nullable=True),
|
||||||
|
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
||||||
|
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
|
||||||
|
sa.PrimaryKeyConstraint("id"),
|
||||||
|
sa.ForeignKeyConstraint(["account_id"], ["accounts.id"], ondelete="CASCADE"),
|
||||||
|
sa.UniqueConstraint("account_id"),
|
||||||
|
)
|
||||||
|
op.create_index("ix_psa_connections_account_id", "psa_connections", ["account_id"])
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
op.drop_index("ix_psa_connections_account_id")
|
||||||
|
op.drop_table("psa_connections")
|
||||||
@@ -36,6 +36,7 @@ from .survey_response import SurveyResponse
|
|||||||
from .survey_invite import SurveyInvite
|
from .survey_invite import SurveyInvite
|
||||||
from .kb_import import KBImport, KBImportNode
|
from .kb_import import KBImport, KBImportNode
|
||||||
from .script_template import ScriptCategory, ScriptTemplate, ScriptGeneration
|
from .script_template import ScriptCategory, ScriptTemplate, ScriptGeneration
|
||||||
|
from .psa_connection import PsaConnection
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"User",
|
"User",
|
||||||
@@ -86,4 +87,5 @@ __all__ = [
|
|||||||
"ScriptCategory",
|
"ScriptCategory",
|
||||||
"ScriptTemplate",
|
"ScriptTemplate",
|
||||||
"ScriptGeneration",
|
"ScriptGeneration",
|
||||||
|
"PsaConnection",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ if TYPE_CHECKING:
|
|||||||
from app.models.step_category import StepCategory
|
from app.models.step_category import StepCategory
|
||||||
from app.models.step_library import StepLibrary
|
from app.models.step_library import StepLibrary
|
||||||
from app.models.account_limit_override import AccountLimitOverride
|
from app.models.account_limit_override import AccountLimitOverride
|
||||||
|
from app.models.psa_connection import PsaConnection
|
||||||
|
|
||||||
|
|
||||||
class Account(Base):
|
class Account(Base):
|
||||||
@@ -53,3 +54,4 @@ class Account(Base):
|
|||||||
step_categories: Mapped[list["StepCategory"]] = relationship("StepCategory", foreign_keys="[StepCategory.account_id]", back_populates="account")
|
step_categories: Mapped[list["StepCategory"]] = relationship("StepCategory", foreign_keys="[StepCategory.account_id]", back_populates="account")
|
||||||
step_library: Mapped[list["StepLibrary"]] = relationship("StepLibrary", foreign_keys="[StepLibrary.account_id]", back_populates="account")
|
step_library: Mapped[list["StepLibrary"]] = relationship("StepLibrary", foreign_keys="[StepLibrary.account_id]", back_populates="account")
|
||||||
limit_override: Mapped[Optional["AccountLimitOverride"]] = relationship("AccountLimitOverride", back_populates="account", uselist=False)
|
limit_override: Mapped[Optional["AccountLimitOverride"]] = relationship("AccountLimitOverride", back_populates="account", uselist=False)
|
||||||
|
psa_connection: Mapped[Optional["PsaConnection"]] = relationship("PsaConnection", back_populates="account", uselist=False)
|
||||||
|
|||||||
48
backend/app/models/psa_connection.py
Normal file
48
backend/app/models/psa_connection.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
"""PSA connection model — one per account."""
|
||||||
|
import uuid
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from sqlalchemy import String, DateTime, Boolean, Text, ForeignKey
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
|
|
||||||
|
from app.core.database import Base
|
||||||
|
|
||||||
|
|
||||||
|
class PsaConnection(Base):
|
||||||
|
__tablename__ = "psa_connections"
|
||||||
|
|
||||||
|
id: Mapped[uuid.UUID] = mapped_column(
|
||||||
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||||
|
)
|
||||||
|
account_id: Mapped[uuid.UUID] = mapped_column(
|
||||||
|
UUID(as_uuid=True),
|
||||||
|
ForeignKey("accounts.id", ondelete="CASCADE"),
|
||||||
|
nullable=False,
|
||||||
|
unique=True,
|
||||||
|
index=True,
|
||||||
|
)
|
||||||
|
provider: Mapped[str] = mapped_column(String(50), nullable=False)
|
||||||
|
display_name: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||||
|
site_url: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||||
|
company_id: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||||
|
credentials_encrypted: Mapped[str] = mapped_column(Text, nullable=False)
|
||||||
|
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
||||||
|
last_validated_at: Mapped[Optional[datetime]] = mapped_column(
|
||||||
|
DateTime(timezone=True), nullable=True
|
||||||
|
)
|
||||||
|
created_at: Mapped[datetime] = mapped_column(
|
||||||
|
DateTime(timezone=True),
|
||||||
|
nullable=False,
|
||||||
|
default=lambda: datetime.now(timezone.utc),
|
||||||
|
)
|
||||||
|
updated_at: Mapped[datetime] = mapped_column(
|
||||||
|
DateTime(timezone=True),
|
||||||
|
nullable=False,
|
||||||
|
default=lambda: datetime.now(timezone.utc),
|
||||||
|
onupdate=lambda: datetime.now(timezone.utc),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Relationships
|
||||||
|
account = relationship("Account", back_populates="psa_connection")
|
||||||
Reference in New Issue
Block a user