Skip to content

Commit

Permalink
[Issue #3860] Create opportunity_version Table (#3917)
Browse files Browse the repository at this point in the history
## Summary
Fixes #{[3860](#3860)}

### Time to review: __5 mins__

## Changes proposed
Added OpportunityVersion Model
Added OpportunityVersionFactory
Migration Script to create table

---------

Co-authored-by: nava-platform-bot <[email protected]>
  • Loading branch information
babebe and nava-platform-bot authored Feb 21, 2025
1 parent 8dcddc9 commit f586071
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""add opportunity version
Revision ID: 20590d71bc20
Revises: d5f73aa58acb
Create Date: 2025-02-20 17:46:56.068111
"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "20590d71bc20"
down_revision = "d5f73aa58acb"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"opportunity_version",
sa.Column("opportunity_version_id", sa.UUID(), nullable=False),
sa.Column("opportunity_id", sa.BigInteger(), nullable=False),
sa.Column("opportunity_data", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column(
"created_at",
sa.TIMESTAMP(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.Column(
"updated_at",
sa.TIMESTAMP(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.ForeignKeyConstraint(
["opportunity_id"],
["api.opportunity.opportunity_id"],
name=op.f("opportunity_version_opportunity_id_opportunity_fkey"),
),
sa.PrimaryKeyConstraint(
"opportunity_version_id", "opportunity_id", name=op.f("opportunity_version_pkey")
),
schema="api",
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("opportunity_version", schema="api")
# ### end Alembic commands ###
17 changes: 17 additions & 0 deletions api/src/db/models/opportunity_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import uuid
from datetime import date
from typing import TYPE_CHECKING

from sqlalchemy import BigInteger, ForeignKey, UniqueConstraint
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.ext.associationproxy import AssociationProxy, association_proxy
from sqlalchemy.orm import Mapped, mapped_column, relationship

Expand Down Expand Up @@ -456,3 +458,18 @@ class OpportunityChangeAudit(ApiSchemaTable, TimestampMixin):
BigInteger, ForeignKey(Opportunity.opportunity_id), primary_key=True, index=True
)
opportunity: Mapped[Opportunity] = relationship(Opportunity)


class OpportunityVersion(ApiSchemaTable, TimestampMixin):
__tablename__ = "opportunity_version"

opportunity_version_id: Mapped[uuid.UUID] = mapped_column(
UUID, primary_key=True, default=uuid.uuid4
)

opportunity_id: Mapped[int] = mapped_column(
BigInteger, ForeignKey(Opportunity.opportunity_id), primary_key=True
)
opportunity: Mapped[Opportunity] = relationship(Opportunity)

opportunity_data: Mapped[dict] = mapped_column(JSONB)
12 changes: 12 additions & 0 deletions api/tests/src/db/models/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -1900,3 +1900,15 @@ class Meta:
form_id = factory.LazyAttribute(lambda o: o.application_form.form_id)

is_required = False


class OpportunityVersionFactory(BaseFactory):
class Meta:
model = opportunity_models.OpportunityVersion

opportunity_version_id = Generators.UuidObj

opportunity = factory.SubFactory(OpportunityFactory)
opportunity_id = factory.LazyAttribute(lambda o: o.opportunity.opportunity_id)

opportunity_data = factory.LazyAttribute(lambda o: o.opportunity_data)
Binary file modified documentation/api/database/erds/api-schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f586071

Please sign in to comment.