Skip to content

Commit

Permalink
Add tenant_id to opportunity association tables
Browse files Browse the repository at this point in the history
  • Loading branch information
shri committed Aug 13, 2024
1 parent cee6ed9 commit e078a3b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# type: ignore
"""Add tenant_id to opportunity association tables
Revision ID: 46acf6e88bda
Revises: 992fa69e02cb
Create Date: 2024-08-13 09:43:48.474750+00:00
"""
from __future__ import annotations

import warnings
from typing import TYPE_CHECKING

import sqlalchemy as sa
from alembic import op
from advanced_alchemy.types import EncryptedString, EncryptedText, GUID, ORA_JSONB, DateTimeUTC
from sqlalchemy import Text # noqa: F401

if TYPE_CHECKING:
from collections.abc import Sequence

__all__ = ["downgrade", "upgrade", "schema_upgrades", "schema_downgrades", "data_upgrades", "data_downgrades"]

sa.GUID = GUID
sa.DateTimeUTC = DateTimeUTC
sa.ORA_JSONB = ORA_JSONB
sa.EncryptedString = EncryptedString
sa.EncryptedText = EncryptedText

# revision identifiers, used by Alembic.
revision = '46acf6e88bda'
down_revision = '992fa69e02cb'
branch_labels = None
depends_on = None


def upgrade() -> None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
with op.get_context().autocommit_block():
schema_upgrades()
data_upgrades()

def downgrade() -> None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=UserWarning)
with op.get_context().autocommit_block():
data_downgrades()
schema_downgrades()

def schema_upgrades() -> None:
"""schema upgrade migrations go here."""
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('opportunity_job_post_relation', schema=None) as batch_op:
batch_op.add_column(sa.Column('tenant_id', sa.GUID(length=16), nullable=False))
batch_op.create_foreign_key(batch_op.f('fk_opportunity_job_post_relation_tenant_id_tenant'), 'tenant', ['tenant_id'], ['id'], ondelete='CASCADE')

with op.batch_alter_table('opportunity_person_relation', schema=None) as batch_op:
batch_op.add_column(sa.Column('tenant_id', sa.GUID(length=16), nullable=False))
batch_op.create_foreign_key(batch_op.f('fk_opportunity_person_relation_tenant_id_tenant'), 'tenant', ['tenant_id'], ['id'], ondelete='CASCADE')

# ### end Alembic commands ###

def schema_downgrades() -> None:
"""schema downgrade migrations go here."""
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('opportunity_person_relation', schema=None) as batch_op:
batch_op.drop_constraint(batch_op.f('fk_opportunity_person_relation_tenant_id_tenant'), type_='foreignkey')
batch_op.drop_column('tenant_id')

with op.batch_alter_table('opportunity_job_post_relation', schema=None) as batch_op:
batch_op.drop_constraint(batch_op.f('fk_opportunity_job_post_relation_tenant_id_tenant'), type_='foreignkey')
batch_op.drop_column('tenant_id')

# ### end Alembic commands ###

def data_upgrades() -> None:
"""Add any optional data upgrade migrations here!"""

def data_downgrades() -> None:
"""Add any optional data downgrade migrations here!"""
15 changes: 8 additions & 7 deletions src/app/db/models/opportunity.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@
orm_registry.metadata,
Column("opportunity_id", ForeignKey("opportunity.id", ondelete="CASCADE"), primary_key=True),
Column("person_id", ForeignKey("person.id", ondelete="CASCADE"), primary_key=True),
Column("tenant_id", ForeignKey("tenant.id", ondelete="CASCADE"), primary_key=True),
)

opportunity_job_post_relation: Final[Table] = Table(
"opportunity_job_post_relation",
orm_registry.metadata,
Column("opportunity_id", ForeignKey("opportunity.id", ondelete="CASCADE"), primary_key=True),
Column("job_post_id", ForeignKey("job_post.id", ondelete="CASCADE"), primary_key=True),
Column("tenant_id", ForeignKey("tenant.id", ondelete="CASCADE"), primary_key=True),
)


class OpportunityAuditLog(UUIDAuditBase):
"""An audit log for opportunity."""

__tablename__ = "opportunity_audit_log"
__table_args__ = (
Index('ix_opportunity_audit_log_opportunity_id_tenant_id', 'opportunity_id', 'tenant_id'),
)
__table_args__ = (Index("ix_opportunity_audit_log_opportunity_id_tenant_id", "opportunity_id", "tenant_id"),)
operation: Mapped[str] = mapped_column(nullable=False)
diff: Mapped[dict[str, Any] | None] = mapped_column(JSONB, nullable=True, default=None)
user_id: Mapped[UUID] = mapped_column(ForeignKey("user_account.id"), nullable=False)
Expand All @@ -59,11 +60,11 @@ class Opportunity(UUIDAuditBase, SlugKey):

__tablename__ = "opportunity"
__pii_columns__ = {}
__table_args__ = (
Index('ix_opportunity_id_tenant_id', 'id', 'tenant_id'),
)
__table_args__ = (Index("ix_opportunity_id_tenant_id", "id", "tenant_id"),)
name: Mapped[str] = mapped_column(nullable=False, index=True)
stage: Mapped[OpportunityStage] = mapped_column(OpportunityStageType, nullable=False, default="identified", index=True)
stage: Mapped[OpportunityStage] = mapped_column(
OpportunityStageType, nullable=False, default="identified", index=True
)
notes: Mapped[str] = mapped_column(Text, nullable=False, default="")
tenant_id: Mapped[UUID] = mapped_column(ForeignKey("tenant.id"), nullable=False, index=True)
owner_id: Mapped[UUID] = mapped_column(ForeignKey("user_account.id"), nullable=True, default=None)
Expand Down

0 comments on commit e078a3b

Please sign in to comment.