Skip to content

Commit

Permalink
Moved the feed data for the lookup tables from csv file into src/conf… (
Browse files Browse the repository at this point in the history
#67)

…ig file. Added custom tests for alembic migration

---------

Co-authored-by: Nargis Sultani <[email protected]>
  • Loading branch information
nargis-sultani and Nargis Sultani authored Jan 4, 2024
1 parent ee4d59c commit 9004692
Show file tree
Hide file tree
Showing 19 changed files with 334 additions and 255 deletions.
Empty file added db_revisions/__init__.py
Empty file.
58 changes: 0 additions & 58 deletions db_revisions/feed/address_state.csv

This file was deleted.

8 changes: 0 additions & 8 deletions db_revisions/feed/federal_regulator.csv

This file was deleted.

19 changes: 0 additions & 19 deletions db_revisions/feed/hmda_institution_type.csv

This file was deleted.

14 changes: 0 additions & 14 deletions db_revisions/feed/sbl_institution_type.csv

This file was deleted.

18 changes: 6 additions & 12 deletions db_revisions/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from alembic import op
from sqlalchemy import engine_from_config
from sqlalchemy import MetaData, Table, engine_from_config
import sqlalchemy
from csv import DictReader
import os


def table_exists(table_name):
Expand All @@ -15,12 +13,8 @@ def table_exists(table_name):
return table_name in tables


def get_feed_data_from_file(table_name):
file_dir = os.path.dirname(os.path.realpath(__file__))
data_file_path = f"{file_dir}/feed/%s.csv" % table_name
data_file = open(data_file_path, "r")
reader = DictReader(data_file, delimiter="|")
output_list = list()
for dictionary in reader:
output_list.append(dictionary)
return output_list
def get_table_by_name(table_name):
meta = MetaData()
meta.reflect(op.get_bind())
table = Table(table_name, meta)
return table
28 changes: 0 additions & 28 deletions db_revisions/versions/26a742d97ad9_feed_federal_regulator_table.py

This file was deleted.

39 changes: 39 additions & 0 deletions db_revisions/versions/26a742d97ad9_seed_federal_regulator_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Seed Federal Regulator table
Revision ID: 26a742d97ad9
Revises: 7b6ff51002b5
Create Date: 2023-12-14 01:23:17.872728
"""
from typing import Sequence, Union
from alembic import op
from utils import get_table_by_name


# revision identifiers, used by Alembic.
revision: str = "26a742d97ad9"
down_revision: Union[str, None] = "7b6ff51002b5"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
seed_data = [
{"id": "FCA", "name": "Farm Credit Administration"},
{"id": "FDIC", "name": "Federal Deposit Insurance Corporation"},
{"id": "FHFA", "name": "Federal Housing Finance Agency"},
{"id": "FRS", "name": "Federal Reserve System"},
{"id": "NCUA", "name": "National Credit Union Administration"},
{"id": "OCC", "name": "Office of the Comptroller of the Currency"},
{"id": "OTS", "name": "Office of Thrift Supervision (only valid until July 21, 2011)"},
]

table = get_table_by_name("federal_regulator")

op.bulk_insert(table, seed_data)


def downgrade() -> None:
table = get_table_by_name("federal_regulator")

op.execute(table.delete())
28 changes: 0 additions & 28 deletions db_revisions/versions/7b6ff51002b5_feed_address_state_table.py

This file was deleted.

89 changes: 89 additions & 0 deletions db_revisions/versions/7b6ff51002b5_seed_address_state_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""Seed Address State table
Revision ID: 7b6ff51002b5
Revises: 045aa502e050
Create Date: 2023-12-14 01:21:48.325752
"""
from typing import Sequence, Union
from alembic import op
from utils import get_table_by_name


# revision identifiers, used by Alembic.
revision: str = "7b6ff51002b5"
down_revision: Union[str, None] = "045aa502e050"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
seed_data = [
{"code": "AL", "name": "Alabama"},
{"code": "AK", "name": "Alaska"},
{"code": "AZ", "name": "Arizona"},
{"code": "AR", "name": "Arkansas"},
{"code": "CA", "name": "California"},
{"code": "CO", "name": "Colorado"},
{"code": "CT", "name": "Connecticut"},
{"code": "DE", "name": "Delaware"},
{"code": "FL", "name": "Florida"},
{"code": "GA", "name": "Georgia"},
{"code": "HI", "name": "Hawaii"},
{"code": "ID", "name": "Idaho"},
{"code": "IL", "name": "Illinois"},
{"code": "IN", "name": "Indiana"},
{"code": "IA", "name": "Iowa"},
{"code": "KS", "name": "Kansas"},
{"code": "KY", "name": "Kentucky"},
{"code": "LA", "name": "Louisiana"},
{"code": "ME", "name": "Maine"},
{"code": "MD", "name": "Maryland"},
{"code": "MA", "name": "Massachusetts"},
{"code": "MI", "name": "Michigan"},
{"code": "MN", "name": "Minnesota"},
{"code": "MS", "name": "Mississippi"},
{"code": "MO", "name": "Missouri"},
{"code": "MT", "name": "Montana"},
{"code": "NE", "name": "Nebraska"},
{"code": "NV", "name": "Nevada"},
{"code": "NH", "name": "New Hampshire"},
{"code": "NJ", "name": "New Jersey"},
{"code": "NM", "name": "New Mexico"},
{"code": "NY", "name": "New York"},
{"code": "NC", "name": "North Carolina"},
{"code": "ND", "name": "North Dakota"},
{"code": "OH", "name": "Ohio"},
{"code": "OK", "name": "Oklahoma"},
{"code": "OR", "name": "Oregon"},
{"code": "PA", "name": "Pennsylvania"},
{"code": "RI", "name": "Rhode Island"},
{"code": "SC", "name": "South Carolina"},
{"code": "SD", "name": "South Dakota"},
{"code": "TN", "name": "Tennessee"},
{"code": "TX", "name": "Texas"},
{"code": "UT", "name": "Utah"},
{"code": "VT", "name": "Vermont"},
{"code": "VA", "name": "Virginia"},
{"code": "WA", "name": "Washington"},
{"code": "WV", "name": "West Virginia"},
{"code": "WI", "name": "Wisconsin"},
{"code": "WY", "name": "Wyoming"},
{"code": "DC", "name": "District of Columbia"},
{"code": "AS", "name": "American Samoa"},
{"code": "GU", "name": "Guam"},
{"code": "MP", "name": "Northern Mariana Islands"},
{"code": "PR", "name": "Puerto Rico"},
{"code": "UM", "name": "United States Minor Outlying Islands"},
{"code": "VI", "name": "Virgin Islands, U.S."},
]

table = get_table_by_name("address_state")

op.bulk_insert(table, seed_data)


def downgrade() -> None:
table = get_table_by_name("address_state")

op.execute(table.delete())

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Seed SBL Institution Type table
Revision ID: a41281b1e109
Revises: f4ff7d1aa6df
Create Date: 2023-12-14 01:24:00.120073
"""
from typing import Sequence, Union
from alembic import op
from utils import get_table_by_name

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


def upgrade() -> None:
seed_data = [
{"id": "1", "name": "Bank or savings association."},
{"id": "2", "name": "Minority depository institution."},
{"id": "3", "name": "Credit union."},
{"id": "4", "name": "Nondepository institution."},
{"id": "5", "name": "Community development financial institution (CDFI)."},
{"id": "6", "name": "Other nonprofit financial institution."},
{"id": "7", "name": "Farm Credit System institution."},
{"id": "8", "name": "Government lender."},
{"id": "9", "name": "Commercial finance company."},
{"id": "10", "name": "Equipment finance company."},
{"id": "11", "name": "Industrial loan company."},
{"id": "12", "name": "Online lender."},
{"id": "13", "name": "Other"},
]

table = get_table_by_name("sbl_institution_type")

op.bulk_insert(table, seed_data)


def downgrade() -> None:
table = get_table_by_name("sbl_institution_type")

op.execute(table.delete())
Loading

0 comments on commit 9004692

Please sign in to comment.