Skip to content

Commit

Permalink
Merge branch 'main' into 68-update-institutions-table-and-daos-instit…
Browse files Browse the repository at this point in the history
…ution-type-fields
  • Loading branch information
jcadam14 committed Jan 4, 2024
2 parents 3de994a + 3bef614 commit e10aa57
Show file tree
Hide file tree
Showing 31 changed files with 531 additions and 348 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ $ docker compose up -d pg keycloak
```
![Docker](images/sbl_project_svcs.png)

---
## Resetting DB and Seeding mock data
On app startup, alembic creates all the tables and seeds the lookup tables.
Running the below script with the 'reset' argument will reset the db:
db_revisions/dev_setup.sh reset
Passing the 'reset-then-seed' argument to the script will reset the db and then seed the lookup tables:
db_revisions/dev_setup.sh reset-then-seed

---
## Running the app
Once the [Dependencies](#dependencies), and [Pre-requisites](#pre-requisites) have been satisfied:
Expand Down
Empty file added db_revisions/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions db_revisions/dev_setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

if [[ $1 == "" ]]
then
echo "No arguments passed!"
echo "The argument must be either reset or reset-then-seed"
exit 0
fi

ACTION=$1

if [ $ACTION == "reset" ]
then
#If only need to reset db
poetry run alembic downgrade base
elif [ $ACTION == "reset-then-seed" ]
then
#First reset the db
poetry run alembic downgrade base
#Then upgrade it to head
poetry run alembic upgrade head
fi
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.

23 changes: 11 additions & 12 deletions db_revisions/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import Any, List, Dict
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 +14,12 @@ 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


def get_indices_from_collection(data: List[Dict[Any, Any]], key: str) -> List[Any]:
return [e[key] for e in data]
28 changes: 0 additions & 28 deletions db_revisions/versions/26a742d97ad9_feed_federal_regulator_table.py

This file was deleted.

41 changes: 41 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,41 @@
"""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 db_revisions.utils import get_table_by_name, get_indices_from_collection


# 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

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)"},
]


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

op.bulk_insert(table, seed_data)


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

ids = get_indices_from_collection(seed_data, "id")

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

This file was deleted.

91 changes: 91 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,91 @@
"""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 db_revisions.utils import get_table_by_name, get_indices_from_collection


# 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

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."},
]


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

op.bulk_insert(table, seed_data)


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

codes = get_indices_from_collection(seed_data, "code")

op.execute(table.delete().where(table.c.code.in_(codes)))

This file was deleted.

Loading

0 comments on commit e10aa57

Please sign in to comment.