Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue 649] Rename opportunity table to use our naming schema #767

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""rename opportunity table prior to real usage

Revision ID: dec1422eee27
Revises: fdd9312633d8
Create Date: 2023-11-27 14:43:04.227044

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

# revision identifiers, used by Alembic.
revision = "dec1422eee27"
down_revision = "fdd9312633d8"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"opportunity",
sa.Column("opportunity_id", sa.Integer(), nullable=False),
sa.Column("opportunity_number", sa.Text(), nullable=True),
sa.Column("opportunity_title", sa.Text(), nullable=True),
sa.Column("agency", sa.Text(), nullable=True),
sa.Column("category", sa.Text(), nullable=True),
sa.Column("is_draft", sa.Boolean(), 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.PrimaryKeyConstraint("opportunity_id", name=op.f("opportunity_pkey")),
)
op.drop_table("topportunity")
# ### end Alembic commands ###


def downgrade():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to clean this up and just remove the migration that created the t* tables, and reorder the order of the upgrades? Not sure the creation / deletion needs to stay in the history for something we never used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but not until later. We need these delete migrations to delete the tables that exist already in our dev/prod environments (that, or we would need to manually delete the tables).

# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"topportunity",
sa.Column("opportunity_id", sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column("oppnumber", sa.TEXT(), autoincrement=False, nullable=True),
sa.Column("opptitle", sa.TEXT(), autoincrement=False, nullable=True),
sa.Column("owningagency", sa.TEXT(), autoincrement=False, nullable=True),
sa.Column("oppcategory", sa.TEXT(), autoincrement=False, nullable=True),
sa.Column("is_draft", sa.BOOLEAN(), autoincrement=False, nullable=False),
sa.Column(
"created_at",
postgresql.TIMESTAMP(timezone=True),
server_default=sa.text("now()"),
autoincrement=False,
nullable=False,
),
sa.Column(
"updated_at",
postgresql.TIMESTAMP(timezone=True),
server_default=sa.text("now()"),
autoincrement=False,
nullable=False,
),
sa.PrimaryKeyConstraint("opportunity_id", name="topportunity_pkey"),
)
op.drop_table("opportunity")
# ### end Alembic commands ###
21 changes: 7 additions & 14 deletions api/src/db/models/opportunity_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@


class Opportunity(Base, TimestampMixin):
# NOTE: Keeping all names lower-case versions of the Oracle DBs naming
# to follow the usual convention of Postgres.
# names are automatically lowercased in all queries unless you quote them.
# Once we've setup the replicated DB, we can adjust the naming here if needed.
__tablename__ = "opportunity"

__tablename__ = "topportunity"
opportunity_id: Mapped[int] = mapped_column(primary_key=True)

opportunity_id: Mapped[int] = mapped_column("opportunity_id", primary_key=True)
opportunity_number: Mapped[str | None]
opportunity_title: Mapped[str | None]

opportunity_number: Mapped[str | None] = mapped_column("oppnumber")
opportunity_title: Mapped[str | None] = mapped_column("opptitle")
agency: Mapped[str | None]

agency: Mapped[str | None] = mapped_column("owningagency")
category: Mapped[OpportunityCategory | None] = mapped_column(StrEnumColumn(OpportunityCategory))

category: Mapped[OpportunityCategory | None] = mapped_column(
"oppcategory", StrEnumColumn(OpportunityCategory)
)

is_draft: Mapped[bool] = mapped_column("is_draft")
is_draft: Mapped[bool]
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_str_enum_column_conversion(db_session, enable_factory_create, category,
# Verify what we stored in the DB is the string itself
raw_db_value = db_session.execute(
text(
f"select oppcategory from {Opportunity.get_table_name()} where opportunity_id={opportunity.opportunity_id}" # nosec
f"select category from {Opportunity.get_table_name()} where opportunity_id={opportunity.opportunity_id}" # nosec
)
).scalar()
assert raw_db_value == db_value
Expand Down