Skip to content

Commit

Permalink
Added Alembic files and changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nargis Sultani committed Dec 8, 2023
1 parent f619a59 commit 2957ad6
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 1 deletion.
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,119 @@
"""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


from db_revisions.utils import table_exists


# 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:
if table_exists("financial_institutions"):
op.add_column("financial_institutions", sa.Column("tax_id", sa.String(length=9), nullable=True))
op.add_column("financial_institutions", sa.Column("rssd_id", sa.Integer(), nullable=True))
op.create_unique_constraint("unique_association", "financial_institutions", ["tax_id", "rssd_id"])
op.add_column(
"financial_institutions", sa.Column("primary_federal_regulator_id", sa.String(length=4), nullable=True)
)
op.add_column("financial_institutions", sa.Column("hmda_institution_type_id", sa.String(), nullable=True))
op.add_column("financial_institutions", sa.Column("sbl_institution_type_id", sa.String(), nullable=True))
op.add_column("financial_institutions", sa.Column("hq_address_street_1", sa.String(), nullable=False))
op.add_column("financial_institutions", sa.Column("hq_address_street_2", sa.String(), nullable=True))
op.add_column("financial_institutions", sa.Column("hq_address_city", sa.String(), nullable=True))
op.add_column("financial_institutions", sa.Column("hq_address_state", sa.String(length=2), nullable=True))
op.add_column("financial_institutions", sa.Column("hq_address_zip", sa.String(length=5), nullable=False))
op.add_column("financial_institutions", sa.Column("parent_lei", sa.String(length=20), nullable=True))
op.add_column("financial_institutions", sa.Column("parent_legal_name", sa.String(), nullable=True))
op.add_column("financial_institutions", sa.Column("parent_rssd_id", sa.Integer(), nullable=True))
op.add_column("financial_institutions", sa.Column("top_holder_lei", sa.String(length=20), nullable=True))
op.add_column("financial_institutions", sa.Column("top_holder_legal_name", sa.String(), nullable=True))
op.add_column("financial_institutions", sa.Column("top_holder_rssd_id", sa.Integer(), nullable=True))
op.create_index(
op.f("ix_financial_institutions_hmda_institution_type_id"),
"financial_institutions",
["hmda_institution_type_id"],
unique=False,
)
op.create_index(
op.f("ix_financial_institutions_hq_address_state"),
"financial_institutions",
["hq_address_state"],
unique=False,
)
op.create_index(
op.f("ix_financial_institutions_primary_federal_regulator_id"),
"financial_institutions",
["primary_federal_regulator_id"],
unique=False,
)
op.create_index(
op.f("ix_financial_institutions_sbl_institution_type_id"),
"financial_institutions",
["sbl_institution_type_id"],
unique=False,
)
with op.batch_alter_table("financial_institutions") as batch_op:
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_financial_institutions", "address_state", ["hq_address_state"], ["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")
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")
4 changes: 4 additions & 0 deletions tests/migrations/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ def test_tables_exist_after_migration(alembic_runner: MigrationContext, alembic_
assert "denied_domains" in tables
assert "financial_institutions" in tables
assert "financial_institution_domains" in tables
assert "address_state" in tables
assert "federal_regulator" in tables
assert "hmda_institution_type" in tables
assert "sbl_institution_type" in tables

0 comments on commit 2957ad6

Please sign in to comment.