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

Features/40 update institutions models new #60

Merged
merged 5 commits 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
6 changes: 5 additions & 1 deletion db_revisions/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def run_migrations_offline() -> None:
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
render_as_batch=True,
)

with context.begin_transaction():
Expand All @@ -90,7 +91,10 @@ def run_migrations_online() -> None:

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata, version_table_schema=target_metadata.schema
connection=connection,
target_metadata=target_metadata,
version_table_schema=target_metadata.schema,
render_as_batch=True,
)

with context.begin_transaction():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""Update Financial Institutions Table

Revision ID: 045aa502e050
Revises: "549c612bf1c9"
Create Date: 2023-11-29 11:55:10.328766

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision: str = "045aa502e050"
down_revision: Union[str, None] = "549c612bf1c9"
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") as batch_op:
batch_op.add_column(sa.Column("tax_id", sa.String(length=9), nullable=True))
batch_op.add_column(sa.Column("rssd_id", sa.Integer(), nullable=True))
batch_op.add_column(sa.Column("primary_federal_regulator_id", sa.String(length=4), nullable=True))
batch_op.add_column(sa.Column("hmda_institution_type_id", sa.String(), nullable=True))
batch_op.add_column(sa.Column("sbl_institution_type_id", sa.String(), nullable=True))
batch_op.add_column(sa.Column("hq_address_street_1", sa.String(), nullable=False))
batch_op.add_column(sa.Column("hq_address_street_2", sa.String(), nullable=True))
batch_op.add_column(sa.Column("hq_address_city", sa.String(), nullable=True))
batch_op.add_column(sa.Column("hq_address_state_code", sa.String(length=2), nullable=True))
batch_op.add_column(sa.Column("hq_address_zip", sa.String(length=5), nullable=False))
batch_op.add_column(sa.Column("parent_lei", sa.String(length=20), nullable=True))
batch_op.add_column(sa.Column("parent_legal_name", sa.String(), nullable=True))
batch_op.add_column(sa.Column("parent_rssd_id", sa.Integer(), nullable=True))
batch_op.add_column(sa.Column("top_holder_lei", sa.String(length=20), nullable=True))
batch_op.add_column(sa.Column("top_holder_legal_name", sa.String(), nullable=True))
batch_op.add_column(sa.Column("top_holder_rssd_id", sa.Integer(), nullable=True))
batch_op.create_index(
batch_op.f("ix_financial_institutions_hmda_institution_type_id"),
["hmda_institution_type_id"],
unique=False,
)
batch_op.create_index(
batch_op.f("ix_financial_institutions_hq_address_state_code"),
["hq_address_state_code"],
unique=False,
)
batch_op.create_index(
batch_op.f("ix_financial_institutions_primary_federal_regulator_id"),
["primary_federal_regulator_id"],
unique=False,
)
batch_op.create_index(
batch_op.f("ix_financial_institutions_sbl_institution_type_id"),
["sbl_institution_type_id"],
unique=False,
)
batch_op.create_unique_constraint("unique_financial_institutions_tax_id", ["tax_id"])
batch_op.create_unique_constraint("unique_financial_institutions_rssd_id", ["rssd_id"])
batch_op.create_foreign_key(
"fk_federal_regulator_financial_institutions",
"federal_regulator",
["primary_federal_regulator_id"],
["id"],
)
batch_op.create_foreign_key(
"fk_address_state_code_financial_institutions", "address_state", ["hq_address_state_code"], ["code"]
)
batch_op.create_foreign_key(
"fk_hmda_institution_type_financial_institutions",
"hmda_institution_type",
["hmda_institution_type_id"],
["id"],
)
batch_op.create_foreign_key(
"fk_sbl_institution_type_financial_institutions",
"sbl_institution_type",
["sbl_institution_type_id"],
["id"],
)


def downgrade() -> None:
op.drop_constraint(
constraint_name="fk_federal_regulator_financial_institutions", table_name="financial_institutions"
)
op.drop_constraint(constraint_name="fk_address_state_financial_institutions", table_name="financial_institutions")
op.drop_constraint(
constraint_name="fk_hmda_institution_type_financial_institutions", table_name="financial_institutions"
)
op.drop_constraint(
constraint_name="fk_sbl_institution_type_financial_institutions", table_name="financial_institutions"
)
op.drop_column("financial_institutions", "top_holder_rssd_id")
op.drop_column("financial_institutions", "top_holder_legal_name")
op.drop_column("financial_institutions", "top_holder_lei")
op.drop_column("financial_institutions", "parent_rssd_id")
op.drop_column("financial_institutions", "parent_legal_name")
op.drop_column("financial_institutions", "parent_lei")
op.drop_column("financial_institutions", "hq_address_zip")
op.drop_column("financial_institutions", "hq_address_state_code")
op.drop_column("financial_institutions", "hq_address_city")
op.drop_column("financial_institutions", "hq_address_street_2")
op.drop_column("financial_institutions", "hq_address_street_1")
op.drop_column("financial_institutions", "sbl_institution_type_id")
op.drop_column("financial_institutions", "hmda_institution_type_id")
op.drop_column("financial_institutions", "primary_federal_regulator_id")
op.drop_column("financial_institutions", "rssd_id")
op.drop_column("financial_institutions", "tax_id")
37 changes: 37 additions & 0 deletions db_revisions/versions/1f6c33f20a2e_create_address_state_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Create Address State Table

Revision ID: 1f6c33f20a2e
Revises: "20e0d51d8be9"
Create Date: 2023-11-29 12:03:41.737864

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

from db_revisions.utils import table_exists


# revision identifiers, used by Alembic.
revision: str = "1f6c33f20a2e"
down_revision: Union[str, None] = "20e0d51d8be9"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
if not table_exists("address_state"):
op.create_table(
"address_state",
sa.Column("code", sa.String(length=2), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("event_time", sa.DateTime(), server_default=sa.func.now(), nullable=False),
sa.PrimaryKeyConstraint("code"),
sa.UniqueConstraint("name"),
)
op.create_index(op.f("ix_address_state_code"), "address_state", ["code"], unique=False)


def downgrade() -> None:
op.drop_table("address_state")
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Create Federal Regulator Table

Revision ID: 549c612bf1c9
Revises: "8b1ba6a3275b"
Create Date: 2023-11-29 12:09:20.012400

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

from db_revisions.utils import table_exists


# revision identifiers, used by Alembic.
revision: str = "549c612bf1c9"
down_revision: Union[str, None] = "8b1ba6a3275b"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
if not table_exists("federal_regulator"):
op.create_table(
"federal_regulator",
sa.Column("id", sa.String(length=4), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("event_time", sa.DateTime(), server_default=sa.func.now(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("name"),
)


def downgrade() -> None:
op.drop_table("federal_regulator")
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Create SBL Institution Type Table

Revision ID: 56ef0b5cd2d4
Revises: "1f6c33f20a2e"
Create Date: 2023-11-29 12:20:05.593826

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

from db_revisions.utils import table_exists


# revision identifiers, used by Alembic.
revision: str = "56ef0b5cd2d4"
down_revision: Union[str, None] = "1f6c33f20a2e"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
if not table_exists("sbl_institution_type"):
op.create_table(
"sbl_institution_type",
sa.Column("id", sa.String(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("event_time", sa.DateTime(), server_default=sa.func.now(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("name"),
)
op.create_index(op.f("ix_sbl_institution_type_id"), "sbl_institution_type", ["id"], unique=False)


def downgrade() -> None:
op.drop_table("sbl_institution_type")
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Create HMDA Institution Type Table

Revision ID: 8b1ba6a3275b
Revises: "56ef0b5cd2d4"
Create Date: 2023-11-29 12:14:16.694281

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

from db_revisions.utils import table_exists


# revision identifiers, used by Alembic.
revision: str = "8b1ba6a3275b"
down_revision: Union[str, None] = "56ef0b5cd2d4"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
if not table_exists("hmda_institution_type"):
op.create_table(
"hmda_institution_type",
sa.Column("id", sa.String(), nullable=False),
sa.Column("name", sa.String(), nullable=False),
sa.Column("event_time", sa.DateTime(), server_default=sa.func.now(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("name"),
)
op.create_index(op.f("ix_hmda_institution_type_id"), "hmda_institution_type", ["id"], unique=False)


def downgrade() -> None:
op.drop_table("hmda_institution_type")
16 changes: 16 additions & 0 deletions src/entities/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,25 @@
"DeniedDomainDto",
"UserProfile",
"AuthenticatedUser",
"FederalRegulatorDao",
"HMDAInstitutionTypeDao",
"SBLInstitutionTypeDao",
"AddressStateDao",
"FederalRegulatorDto",
"HMDAInstitutionTypeDto",
"SBLInstitutionTypeDto",
"AddressStateDto",
]

from .dao import (
Base,
FinancialInstitutionDao,
FinancialInstitutionDomainDao,
DeniedDomainDao,
FederalRegulatorDao,
HMDAInstitutionTypeDao,
SBLInstitutionTypeDao,
AddressStateDao,
)
from .dto import (
FinancialInstitutionDto,
Expand All @@ -28,4 +40,8 @@
DeniedDomainDto,
UserProfile,
AuthenticatedUser,
FederalRegulatorDto,
HMDAInstitutionTypeDto,
SBLInstitutionTypeDto,
AddressStateDto,
)
46 changes: 45 additions & 1 deletion src/entities/models/dao.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime
from typing import List
from sqlalchemy import ForeignKey, func
from sqlalchemy import ForeignKey, func, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.ext.asyncio import AsyncAttrs
Expand All @@ -21,6 +21,26 @@ class FinancialInstitutionDao(AuditMixin, Base):
domains: Mapped[List["FinancialInstitutionDomainDao"]] = relationship(
"FinancialInstitutionDomainDao", back_populates="fi"
)
tax_id: Mapped[str] = mapped_column(String(9), unique=True)
rssd_id: Mapped[int] = mapped_column(unique=True)
primary_federal_regulator_id: Mapped[str] = mapped_column(ForeignKey("federal_regulator.id"))
primary_federal_regulator: Mapped["FederalRegulatorDao"] = relationship(lazy="selectin")
hmda_institution_type_id: Mapped[str] = mapped_column(ForeignKey("hmda_institution_type.id"))
hmda_institution_type: Mapped["HMDAInstitutionTypeDao"] = relationship(lazy="selectin")
sbl_institution_type_id: Mapped[str] = mapped_column(ForeignKey("sbl_institution_type.id"))
sbl_institution_type: Mapped["SBLInstitutionTypeDao"] = relationship(lazy="selectin")
hq_address_street_1: Mapped[str] = mapped_column(nullable=False)
hq_address_street_2: Mapped[str]
hq_address_city: Mapped[str]
hq_address_state_code: Mapped[str] = mapped_column(ForeignKey("address_state.code"))
lchen-2101 marked this conversation as resolved.
Show resolved Hide resolved
hq_address_state: Mapped["AddressStateDao"] = relationship(lazy="selectin")
hq_address_zip: Mapped[str] = mapped_column(String(5), nullable=False)
parent_lei: Mapped[str] = mapped_column(String(20))
parent_legal_name: Mapped[str]
parent_rssd_id: Mapped[int]
top_holder_lei: Mapped[str] = mapped_column(String(20))
top_holder_legal_name: Mapped[str]
top_holder_rssd_id: Mapped[int]


class FinancialInstitutionDomainDao(AuditMixin, Base):
Expand All @@ -33,3 +53,27 @@ class FinancialInstitutionDomainDao(AuditMixin, Base):
class DeniedDomainDao(AuditMixin, Base):
__tablename__ = "denied_domains"
domain: Mapped[str] = mapped_column(index=True, primary_key=True)


class FederalRegulatorDao(AuditMixin, Base):
__tablename__ = "federal_regulator"
id: Mapped[str] = mapped_column(String(4), index=True, primary_key=True, unique=True)
name: Mapped[str] = mapped_column(unique=True, nullable=False)


class HMDAInstitutionTypeDao(AuditMixin, Base):
__tablename__ = "hmda_institution_type"
id: Mapped[str] = mapped_column(index=True, primary_key=True, unique=True)
name: Mapped[str] = mapped_column(unique=True)


class SBLInstitutionTypeDao(AuditMixin, Base):
__tablename__ = "sbl_institution_type"
id: Mapped[str] = mapped_column(index=True, primary_key=True, unique=True)
name: Mapped[str] = mapped_column(unique=True, nullable=False)


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)
Loading
Loading