From c3afdc6dd8f3ce39aaf77539cf7a90381a08f61e Mon Sep 17 00:00:00 2001 From: Nargis Sultani Date: Thu, 21 Nov 2024 01:19:43 -0500 Subject: [PATCH] undo revision 6dd77f09fae6 --- ...replace_is_active_with_lei_status_in_fi.py | 3 +- ...dd_char_limit_to_financial_institution_.py | 23 ++---------- ...ca39ad68af05_undo_revision_6dd77f09fae6.py | 35 +++++++++++++++++++ .../entities/models/dao.py | 16 ++++----- .../entities/models/dto.py | 14 ++++---- .../entities/repos/test_institutions_repo.py | 16 ++++----- tests/migrations/test_migrations.py | 34 +++++++++++++----- 7 files changed, 85 insertions(+), 56 deletions(-) create mode 100644 db_revisions/versions/ca39ad68af05_undo_revision_6dd77f09fae6.py diff --git a/db_revisions/versions/6613e1e2c133_replace_is_active_with_lei_status_in_fi.py b/db_revisions/versions/6613e1e2c133_replace_is_active_with_lei_status_in_fi.py index 0c5fa72..0df8fff 100644 --- a/db_revisions/versions/6613e1e2c133_replace_is_active_with_lei_status_in_fi.py +++ b/db_revisions/versions/6613e1e2c133_replace_is_active_with_lei_status_in_fi.py @@ -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 ) diff --git a/db_revisions/versions/6dd77f09fae6_add_char_limit_to_financial_institution_.py b/db_revisions/versions/6dd77f09fae6_add_char_limit_to_financial_institution_.py index 8988edc..b015812 100644 --- a/db_revisions/versions/6dd77f09fae6_add_char_limit_to_financial_institution_.py +++ b/db_revisions/versions/6dd77f09fae6_add_char_limit_to_financial_institution_.py @@ -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" @@ -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 diff --git a/db_revisions/versions/ca39ad68af05_undo_revision_6dd77f09fae6.py b/db_revisions/versions/ca39ad68af05_undo_revision_6dd77f09fae6.py new file mode 100644 index 0000000..f1874d9 --- /dev/null +++ b/db_revisions/versions/ca39ad68af05_undo_revision_6dd77f09fae6.py @@ -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 diff --git a/src/regtech_user_fi_management/entities/models/dao.py b/src/regtech_user_fi_management/entities/models/dao.py index 8365b29..d0261f6 100644 --- a/src/regtech_user_fi_management/entities/models/dao.py +++ b/src/regtech_user_fi_management/entities/models/dao.py @@ -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( @@ -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() diff --git a/src/regtech_user_fi_management/entities/models/dto.py b/src/regtech_user_fi_management/entities/models/dto.py index 98e450a..7bb9651 100644 --- a/src/regtech_user_fi_management/entities/models/dto.py +++ b/src/regtech_user_fi_management/entities/models/dto.py @@ -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 diff --git a/tests/entities/repos/test_institutions_repo.py b/tests/entities/repos/test_institutions_repo.py index 3b85b64..eb91743 100644 --- a/tests/entities/repos/test_institutions_repo.py +++ b/tests/entities/repos/test_institutions_repo.py @@ -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", @@ -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, " @@ -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", diff --git a/tests/migrations/test_migrations.py b/tests/migrations/test_migrations.py index af95fba..1628723 100644 --- a/tests/migrations/test_migrations.py +++ b/tests/migrations/test_migrations.py @@ -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): @@ -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