diff --git a/src/app/db/migrations/versions/2024-08-13_add_tenant_id_to_opportunity__46acf6e88bda.py b/src/app/db/migrations/versions/2024-08-13_add_tenant_id_to_opportunity__46acf6e88bda.py new file mode 100644 index 00000000..e4eae278 --- /dev/null +++ b/src/app/db/migrations/versions/2024-08-13_add_tenant_id_to_opportunity__46acf6e88bda.py @@ -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!""" diff --git a/src/app/db/models/opportunity.py b/src/app/db/models/opportunity.py index ea6e8c62..dc03d685 100644 --- a/src/app/db/models/opportunity.py +++ b/src/app/db/models/opportunity.py @@ -24,6 +24,7 @@ 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( @@ -31,15 +32,15 @@ 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) @@ -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)