Skip to content

Commit

Permalink
Make OpportunityAuditLog.user_id nullable and Opportunity.company_id …
Browse files Browse the repository at this point in the history
…unique
  • Loading branch information
shri committed Sep 3, 2024
1 parent 11ae3d6 commit b9cf6d7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# type: ignore
"""Make opportunity_audit_log.user_id nullable and opportunity.company_id unique
Revision ID: 7d7f3e31dfbe
Revises: db999dc8a9fd
Create Date: 2024-09-03 10:33:11.676562+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 = '7d7f3e31dfbe'
down_revision = 'db999dc8a9fd'
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.create_unique_constraint(batch_op.f('uq_opportunity_company_id'), ['company_id'])

with op.batch_alter_table('opportunity_audit_log', schema=None) as batch_op:
batch_op.alter_column('user_id',
existing_type=sa.UUID(),
nullable=True)

# ### 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_audit_log', schema=None) as batch_op:
batch_op.alter_column('user_id',
existing_type=sa.UUID(),
nullable=False)

with op.batch_alter_table('opportunity', schema=None) as batch_op:
batch_op.drop_constraint(batch_op.f('uq_opportunity_company_id'), type_='unique')

# ### 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!"""
5 changes: 2 additions & 3 deletions src/app/db/models/opportunity.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ class OpportunityAuditLog(UUIDAuditBase):
__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)
user_id: Mapped[UUID] = mapped_column(ForeignKey("user_account.id"), nullable=True)
tenant_id: Mapped[UUID] = mapped_column(ForeignKey("tenant.id"), nullable=False)
opportunity_id: Mapped[UUID] = mapped_column(ForeignKey("opportunity.id"), nullable=False, index=True)
# -----------
# ORM Relationships
# ------------
user: Mapped[User] = relationship(
innerjoin=True,
lazy="joined",
)

Expand All @@ -68,7 +67,7 @@ class Opportunity(UUIDAuditBase, SlugKey):
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)
company_id: Mapped[UUID] = mapped_column(ForeignKey("company.id"), nullable=True, unique=True)
# -----------
# ORM Relationships
# ------------
Expand Down

0 comments on commit b9cf6d7

Please sign in to comment.