Skip to content

Commit

Permalink
replaced is_active with lei_status_code (#258)
Browse files Browse the repository at this point in the history
closes [#250
](#250)
closes [#256
](#256)

---------

Co-authored-by: Nargis Sultani <[email protected]>
Co-authored-by: jcadam14 <[email protected]>
  • Loading branch information
3 people authored Nov 21, 2024
1 parent bfefaee commit eccffa3
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"""Replace is_active with lei_status in financial_institutions table
Revision ID: 6613e1e2c133
Revises: 6dd77f09fae6
Create Date: 2024-11-13 00:43:53.489086
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

from db_revisions.utils import table_exists, get_table_by_name, get_indices_from_collection


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

lei_status_seed_data = [
{"code": "ISSUED", "name": "Issued", "can_file": True},
{"code": "LAPSED", "name": "Lapsed", "can_file": False},
{"code": "RETIRED", "name": "Retired", "can_file": False},
]


def upgrade() -> None:

# Creating lei_status table
if not table_exists("lei_status"):
op.create_table(
"lei_status",
sa.Column("code", sa.String(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("can_file", sa.Boolean(), nullable=False),
sa.Column("event_time", sa.DateTime(), server_default=sa.func.now(), nullable=False),
sa.PrimaryKeyConstraint("code"),
sa.UniqueConstraint("name"),
)

# Seeding lei_status table
lei_status_table = get_table_by_name("lei_status")
op.bulk_insert(lei_status_table, lei_status_seed_data)

# Removing is_active from and adding lei_status_code to financial_institutions table
op.add_column("financial_institutions", sa.Column("lei_status_code", sa.String(), nullable=True))

op.execute(
"UPDATE financial_institutions SET lei_status_code = CASE is_active WHEN True THEN 'ISSUED' ELSE 'LAPSED' END"
)

with op.batch_alter_table("financial_institutions") as batch_op:
batch_op.alter_column("lei_status_code", existing_nullable=True, nullable=False)
batch_op.create_index(
index_name=batch_op.f("ix_financial_institutions_lei_status_code"),
columns=["lei_status_code"],
unique=False,
)
batch_op.create_foreign_key(
"fk_lei_status_financial_institutions",
"lei_status",
["lei_status_code"],
["code"],
)

batch_op.drop_index(index_name="ix_financial_institutions_is_active")
batch_op.drop_column("is_active")

# Removing is_active from and adding lei_status_code to financial_institutions_history table
op.add_column("financial_institutions_history", sa.Column("lei_status_code", sa.String()))


def downgrade() -> None:

op.drop_column("financial_institutions_history", "lei_status_code")

op.add_column(
"financial_institutions",
sa.Column(name="is_active", type_=sa.Boolean(), nullable=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
)

batch_op.drop_constraint(constraint_name="fk_lei_status_financial_institutions")
batch_op.drop_index("ix_financial_institutions_lei_status_code")
batch_op.drop_column("lei_status_code")

lei_status_table = get_table_by_name("lei_status")
codes = get_indices_from_collection(lei_status_seed_data, "code")
op.execute(lei_status_table.delete().where(lei_status_table.c.code.in_(codes)))

op.drop_table("lei_status")
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
26 changes: 17 additions & 9 deletions src/regtech_user_fi_management/entities/models/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ 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)
is_active: Mapped[bool] = mapped_column(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(
"FinancialInstitutionDomainDao", back_populates="fi", lazy="selectin"
)
Expand All @@ -53,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 Expand Up @@ -105,3 +106,10 @@ class AddressStateDao(AuditMixin, Base):
__tablename__ = "address_state"
code: Mapped[str] = mapped_column(String(2), index=True, primary_key=True, unique=True)
name: Mapped[str] = mapped_column(unique=True, nullable=False)


class LeiStatusDao(AuditMixin, Base):
__tablename__ = "lei_status"
code: Mapped[str] = mapped_column(index=True, primary_key=True, unique=True)
name: Mapped[str] = mapped_column(unique=True, nullable=False)
can_file: Mapped[bool] = mapped_column(index=True)
27 changes: 18 additions & 9 deletions src/regtech_user_fi_management/entities/models/dto.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from regtech_user_fi_management.config import regex_configs

from typing import Generic, List, Set, Sequence
from pydantic import BaseModel, model_validator, Field
from pydantic import BaseModel, model_validator
from typing import TypeVar

T = TypeVar("T")
Expand Down Expand Up @@ -30,7 +30,7 @@ class Config:
class FinancialInstitutionBase(BaseModel):
lei: str
name: str
is_active: bool
lei_status_code: str


class SblTypeAssociationDto(BaseModel):
Expand Down 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 Expand Up @@ -160,3 +160,12 @@ class FinancialInstitutionWithRelationsDto(FinancialInstitutionDto):

class FinancialInstitutionAssociationDto(FinancialInstitutionWithRelationsDto):
approved: bool


class LeiStatusDto(BaseModel):
code: str
name: str
can_file: bool

class Config:
from_attributes = True
4 changes: 3 additions & 1 deletion tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
HMDAInstitutionTypeDao,
SBLInstitutionTypeDao,
SblTypeMappingDao,
LeiStatusDao,
)


Expand Down Expand Up @@ -62,7 +63,8 @@ def get_institutions_mock(mocker: MockerFixture, authed_user_mock: Mock) -> Mock
FinancialInstitutionDao(
name="Test Bank 123",
lei="TESTBANK123000000000",
is_active=True,
lei_status_code="ISSUED",
lei_status=LeiStatusDao(code="ISSUED", name="Issued", can_file=True),
domains=[FinancialInstitutionDomainDao(domain="test.bank", lei="TESTBANK123")],
tax_id="12-3456789",
rssd_id=1234,
Expand Down
Loading

0 comments on commit eccffa3

Please sign in to comment.