Skip to content

Commit

Permalink
Make tenant_id and company_id unique instead of just company_id
Browse files Browse the repository at this point in the history
  • Loading branch information
shri committed Sep 4, 2024
1 parent f4dd8ea commit a22261d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# type: ignore
"""Make tenant_id and company_id unique key instead of just company_id
Revision ID: 50dc3def711b
Revises: 7d7f3e31dfbe
Create Date: 2024-09-04 12:39:46.166508+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 = '50dc3def711b'
down_revision = '7d7f3e31dfbe'
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', schema=None) as batch_op:
batch_op.drop_constraint('uq_opportunity_company_id', type_='unique')
batch_op.create_unique_constraint(batch_op.f('uq_opportunity_tenant_id'), ['tenant_id', 'company_id'])

# ### 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', schema=None) as batch_op:
batch_op.drop_constraint(batch_op.f('uq_opportunity_tenant_id'), type_='unique')
batch_op.create_unique_constraint('uq_opportunity_company_id', ['company_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!"""
9 changes: 6 additions & 3 deletions src/app/db/models/opportunity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Any, Final, TYPE_CHECKING

from advanced_alchemy.base import SlugKey, UUIDAuditBase, orm_registry
from sqlalchemy import String, Text, ForeignKey, Index, Column, Table
from sqlalchemy import String, Text, ForeignKey, Index, Column, Table, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.dialects.postgresql import JSONB

Expand Down Expand Up @@ -59,15 +59,18 @@ 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"),
UniqueConstraint("tenant_id", "company_id"),
)
name: Mapped[str] = mapped_column(nullable=False, 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)
company_id: Mapped[UUID] = mapped_column(ForeignKey("company.id"), nullable=True, unique=True)
company_id: Mapped[UUID] = mapped_column(ForeignKey("company.id"), nullable=True)
# -----------
# ORM Relationships
# ------------
Expand Down

0 comments on commit a22261d

Please sign in to comment.