Skip to content

Commit

Permalink
undo revision 6dd77f09fae6
Browse files Browse the repository at this point in the history
  • Loading branch information
Nargis Sultani committed Nov 21, 2024
1 parent 6e58904 commit c3afdc6
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ def downgrade() -> None:

op.add_column(
"financial_institutions",
sa.Column(name="is_active", type_=sa.Boolean(), nullable=True, server_default=sa.true()),
sa.Column(name="is_active", type_=sa.Boolean(), server_default=sa.true()),
)
op.execute(
"UPDATE financial_institutions SET is_active = CASE lei_status_code WHEN 'ISSUED' THEN True ELSE False END"
)

with op.batch_alter_table("financial_institutions") as batch_op:
batch_op.alter_column("is_active", existing_nullable=True, nullable=False)
batch_op.create_index(
index_name=batch_op.f("ix_financial_institutions_is_active"), columns=["is_active"], unique=False
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "6dd77f09fae6"
Expand All @@ -20,24 +17,8 @@


def upgrade() -> None:
with op.batch_alter_table("financial_institutions", schema=None) as batch_op:
batch_op.alter_column("name", type_=sa.String(255), nullable=False)
batch_op.alter_column("hq_address_street_1", type_=sa.String(255), nullable=False)
batch_op.alter_column("hq_address_street_2", type_=sa.String(255), nullable=True)
batch_op.alter_column("hq_address_street_3", type_=sa.String(255), nullable=True)
batch_op.alter_column("hq_address_street_4", type_=sa.String(255), nullable=True)
batch_op.alter_column("hq_address_city", type_=sa.String(255), nullable=False)
batch_op.alter_column("parent_legal_name", type_=sa.String(255), nullable=True)
batch_op.alter_column("top_holder_legal_name", type_=sa.String(255), nullable=True)
pass


def downgrade() -> None:
with op.batch_alter_table("financial_institutions", schema=None) as batch_op:
batch_op.alter_column("name", type_=sa.String, nullable=False)
batch_op.alter_column("hq_address_street_1", type_=sa.String, nullable=False)
batch_op.alter_column("hq_address_street_2", type_=sa.String, nullable=True)
batch_op.alter_column("hq_address_street_3", type_=sa.String, nullable=True)
batch_op.alter_column("hq_address_street_4", type_=sa.String, nullable=True)
batch_op.alter_column("hq_address_city", type_=sa.String, nullable=False)
batch_op.alter_column("parent_legal_name", type_=sa.String, nullable=True)
batch_op.alter_column("top_holder_legal_name", type_=sa.String, nullable=True)
pass
35 changes: 35 additions & 0 deletions db_revisions/versions/ca39ad68af05_undo_revision_6dd77f09fae6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Undo revision 6dd77f09fae6
Revision ID: ca39ad68af05
Revises: 6613e1e2c133
Create Date: 2024-11-20 14:58:54.366468
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "ca39ad68af05"
down_revision: Union[str, None] = "6613e1e2c133"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
with op.batch_alter_table("financial_institutions", schema=None) as batch_op:
batch_op.alter_column("name", type_=sa.String(), nullable=False)
batch_op.alter_column("hq_address_street_1", type_=sa.String(), nullable=False)
batch_op.alter_column("hq_address_street_2", type_=sa.String(), nullable=True)
batch_op.alter_column("hq_address_street_3", type_=sa.String(), nullable=True)
batch_op.alter_column("hq_address_street_4", type_=sa.String(), nullable=True)
batch_op.alter_column("hq_address_city", type_=sa.String(), nullable=False)
batch_op.alter_column("parent_legal_name", type_=sa.String(), nullable=True)
batch_op.alter_column("top_holder_legal_name", type_=sa.String(), nullable=True)


def downgrade() -> None:
pass
16 changes: 8 additions & 8 deletions src/regtech_user_fi_management/entities/models/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FinancialInstitutionDao(AuditMixin, Base):
version: Mapped[int] = mapped_column(nullable=False, default=0)
__mapper_args__ = {"version_id_col": version, "version_id_generator": False}
lei: Mapped[str] = mapped_column(String(20), unique=True, index=True, primary_key=True)
name: Mapped[str] = mapped_column(String(255), index=True)
name: Mapped[str] = mapped_column(index=True)
lei_status_code: Mapped[str] = mapped_column(ForeignKey("lei_status.code"), nullable=False)
lei_status: Mapped["LeiStatusDao"] = relationship(lazy="selectin")
domains: Mapped[List["FinancialInstitutionDomainDao"]] = relationship(
Expand All @@ -54,19 +54,19 @@ class FinancialInstitutionDao(AuditMixin, Base):
hmda_institution_type_id: Mapped[str] = mapped_column(ForeignKey("hmda_institution_type.id"), nullable=True)
hmda_institution_type: Mapped["HMDAInstitutionTypeDao"] = relationship(lazy="selectin")
sbl_institution_types: Mapped[List[SblTypeMappingDao]] = relationship(lazy="selectin", cascade="all, delete-orphan")
hq_address_street_1: Mapped[str] = mapped_column(String(255))
hq_address_street_2: Mapped[str] = mapped_column(String(255), nullable=True)
hq_address_street_3: Mapped[str] = mapped_column(String(255), nullable=True)
hq_address_street_4: Mapped[str] = mapped_column(String(255), nullable=True)
hq_address_city: Mapped[str] = mapped_column(String(255))
hq_address_street_1: Mapped[str] = mapped_column()
hq_address_street_2: Mapped[str] = mapped_column(nullable=True)
hq_address_street_3: Mapped[str] = mapped_column(nullable=True)
hq_address_street_4: Mapped[str] = mapped_column(nullable=True)
hq_address_city: Mapped[str] = mapped_column()
hq_address_state_code: Mapped[str] = mapped_column(ForeignKey("address_state.code"), nullable=True)
hq_address_state: Mapped["AddressStateDao"] = relationship(lazy="selectin")
hq_address_zip: Mapped[str] = mapped_column(String(5))
parent_lei: Mapped[str] = mapped_column(String(20), nullable=True)
parent_legal_name: Mapped[str] = mapped_column(String(255), nullable=True)
parent_legal_name: Mapped[str] = mapped_column(nullable=True)
parent_rssd_id: Mapped[int] = mapped_column(nullable=True)
top_holder_lei: Mapped[str] = mapped_column(String(20), nullable=True)
top_holder_legal_name: Mapped[str] = mapped_column(String(255), nullable=True)
top_holder_legal_name: Mapped[str] = mapped_column(nullable=True)
top_holder_rssd_id: Mapped[int] = mapped_column(nullable=True)
modified_by: Mapped[str] = mapped_column()

Expand Down
14 changes: 7 additions & 7 deletions src/regtech_user_fi_management/entities/models/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ class FinancialInstitutionDto(FinancialInstitutionBase):
primary_federal_regulator_id: str | None = None
hmda_institution_type_id: str | None = None
sbl_institution_types: List[SblTypeAssociationDto | str] = []
hq_address_street_1: str = Field(max_length=255)
hq_address_street_2: str | None = Field(None, max_length=255)
hq_address_street_3: str | None = Field(None, max_length=255)
hq_address_street_4: str | None = Field(None, max_length=255)
hq_address_city: str = Field(None, max_length=255)
hq_address_street_1: str
hq_address_street_2: str | None = None
hq_address_street_3: str | None = None
hq_address_street_4: str | None = None
hq_address_city: str
hq_address_state_code: str | None = None
hq_address_zip: str
parent_lei: str | None = None
parent_legal_name: str | None = Field(None, max_length=255)
parent_legal_name: str | None = None
parent_rssd_id: int | None = None
top_holder_lei: str | None = None
top_holder_legal_name: str | None = Field(None, max_length=255)
top_holder_legal_name: str | None = None
top_holder_rssd_id: int | None = None
version: int | None = None

Expand Down
16 changes: 6 additions & 10 deletions tests/entities/repos/test_institutions_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,20 +412,16 @@ async def test_update_sbl_institution_types_inst_non_exist(
commit_spy.assert_not_called()
assert res is None

async def test_add_institution_invalid_field_length(self, transaction_session: AsyncSession):
async def test_add_institution_no_field_length_restriction(self, transaction_session: AsyncSession):

out_of_range_text = (
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
"dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
"nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis..."
)
out_of_range_text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
with pytest.raises(Exception) as e:
await repo.upsert_institution(
transaction_session,
FinancialInstitutionDto(
name="New Bank 123",
lei="NEWBANK1230000000000",
lei_status_code="ISSUED",
is_active=True,
tax_id="65-4321987",
rssd_id=6543,
primary_federal_regulator_id="FRI3",
Expand All @@ -450,7 +446,7 @@ async def test_add_institution_invalid_field_length(self, transaction_session: A
)
assert isinstance(e.value, ValidationError)

async def test_update_institution_invalid_field_length(self, transaction_session: AsyncSession):
async def test_update_institution_no_field_length_restriction(self, transaction_session: AsyncSession):
out_of_range_text = (
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
"dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
Expand All @@ -463,14 +459,14 @@ async def test_update_institution_invalid_field_length(self, transaction_session
FinancialInstitutionDto(
name="New Bank 123",
lei="NEWBANK1230000000000",
lei_status_code="ISSUED",
is_active=True,
tax_id="65-4321987",
rssd_id=6543,
primary_federal_regulator_id="FRI3",
hmda_institution_type_id="HIT3",
sbl_institution_types=[SblTypeAssociationDto(id="1")],
hq_address_street_1=out_of_range_text,
hq_address_street_2="",
hq_address_street_2=out_of_range_text,
hq_address_street_3="",
hq_address_street_4="",
hq_address_city="Test City 3",
Expand Down
34 changes: 26 additions & 8 deletions tests/migrations/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ def test_migration_to_6dd77f09fae6(alembic_runner: MigrationContext, alembic_eng

columns = inspector.get_columns("financial_institutions")

assert [c for c in columns if c["name"] == "name"][0]["type"].length == 255
assert [c for c in columns if c["name"] == "hq_address_street_1"][0]["type"].length == 255
assert [c for c in columns if c["name"] == "hq_address_street_2"][0]["type"].length == 255
assert [c for c in columns if c["name"] == "hq_address_street_3"][0]["type"].length == 255
assert [c for c in columns if c["name"] == "hq_address_street_4"][0]["type"].length == 255
assert [c for c in columns if c["name"] == "hq_address_city"][0]["type"].length == 255
assert [c for c in columns if c["name"] == "parent_legal_name"][0]["type"].length == 255
assert [c for c in columns if c["name"] == "top_holder_legal_name"][0]["type"].length == 255
assert [c for c in columns if c["name"] == "name"][0]["type"].length is None
assert [c for c in columns if c["name"] == "hq_address_street_1"][0]["type"].length is None
assert [c for c in columns if c["name"] == "hq_address_street_2"][0]["type"].length is None
assert [c for c in columns if c["name"] == "hq_address_street_3"][0]["type"].length is None
assert [c for c in columns if c["name"] == "hq_address_street_4"][0]["type"].length is None
assert [c for c in columns if c["name"] == "hq_address_city"][0]["type"].length is None
assert [c for c in columns if c["name"] == "parent_legal_name"][0]["type"].length is None
assert [c for c in columns if c["name"] == "top_holder_legal_name"][0]["type"].length is None


def test_tables_exist_migrate_up_to_6613e1e2c133(alembic_runner: MigrationContext, alembic_engine: Engine):
Expand All @@ -93,3 +93,21 @@ def test_tables_exist_migrate_up_to_6613e1e2c133(alembic_runner: MigrationContex
inspector = sqlalchemy.inspect(alembic_engine)
tables = inspector.get_table_names()
assert "lei_status" in tables


def test_migration_to_ca39ad68af05(alembic_runner: MigrationContext, alembic_engine: Engine):

alembic_runner.migrate_up_to("ca39ad68af05")

inspector = sqlalchemy.inspect(alembic_engine)

columns = inspector.get_columns("financial_institutions")

assert [c for c in columns if c["name"] == "name"][0]["type"].length is None
assert [c for c in columns if c["name"] == "hq_address_street_1"][0]["type"].length is None
assert [c for c in columns if c["name"] == "hq_address_street_2"][0]["type"].length is None
assert [c for c in columns if c["name"] == "hq_address_street_3"][0]["type"].length is None
assert [c for c in columns if c["name"] == "hq_address_street_4"][0]["type"].length is None
assert [c for c in columns if c["name"] == "hq_address_city"][0]["type"].length is None
assert [c for c in columns if c["name"] == "parent_legal_name"][0]["type"].length is None
assert [c for c in columns if c["name"] == "top_holder_legal_name"][0]["type"].length is None

0 comments on commit c3afdc6

Please sign in to comment.