-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa0c23b
commit 3e0320a
Showing
8 changed files
with
195 additions
and
9 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
db_revisions/versions/329c70502325_240131_fi_history_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""240131 fi history table | ||
Revision ID: 329c70502325 | ||
Revises: 3f893e52d05c | ||
Create Date: 2024-01-31 10:23:01.081439 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '329c70502325' | ||
down_revision: Union[str, None] = '3f893e52d05c' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.create_table( | ||
"financial_institutions_history", | ||
sa.Column("version", sa.Integer(), nullable=False), | ||
sa.Column("lei", sa.String(), nullable=False), | ||
sa.Column("name", sa.String()), | ||
sa.Column("is_active", sa.Boolean()), | ||
sa.Column("tax_id", sa.String(9)), | ||
sa.Column("rssd_id", sa.Integer()), | ||
sa.Column("primary_federal_regulator_id", sa.String(4)), | ||
sa.Column("hmda_institution_type_id", sa.String()), | ||
sa.Column("hq_address_street_1", sa.String()), | ||
sa.Column("hq_address_street_2", sa.String()), | ||
sa.Column("hq_address_city", sa.String()), | ||
sa.Column("hq_address_state_code", sa.String(2)), | ||
sa.Column("hq_address_zip", sa.String(5)), | ||
sa.Column("parent_lei", sa.String(20)), | ||
sa.Column("parent_legal_name", sa.String()), | ||
sa.Column("parent_rssd_id", sa.Integer()), | ||
sa.Column("top_holder_lei", sa.String(20)), | ||
sa.Column("top_holder_legal_name", sa.String()), | ||
sa.Column("top_holder_rssd_id", sa.Integer()), | ||
sa.Column("event_time", sa.DateTime(), server_default=sa.func.now(), nullable=False), | ||
sa.Column("modified_by", sa.String()), | ||
sa.Column("changeset", sa.JSON), | ||
sa.PrimaryKeyConstraint("lei", "version") | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_table("financial_institutions_history") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""240130 add version | ||
Revision ID: 3f893e52d05c | ||
Revises: 6826f05140cd | ||
Create Date: 2024-01-30 14:37:47.652233 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '3f893e52d05c' | ||
down_revision: Union[str, None] = '6826f05140cd' | ||
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("version", type_=sa.Integer(), nullable=False, server_default=sa.text("1"))) | ||
batch_op.add_column(sa.Column("modified_by", sa.String())) | ||
with op.batch_alter_table("fi_to_type_mapping") as batch_op: | ||
batch_op.add_column(sa.Column("version", type_=sa.Integer(), nullable=False, server_default=sa.text("1"))) | ||
batch_op.add_column(sa.Column("modified_by", sa.String())) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_column("financial_institutions", "version") | ||
op.drop_column("financial_institutions", "modified_by") | ||
op.drop_column("fi_to_type_mapping", "version") | ||
op.drop_column("fi_to_type_mapping", "modified_by") |
35 changes: 35 additions & 0 deletions
35
db_revisions/versions/8106d83ff594_240131_fi_type_association_history_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""240131 fi type association history table | ||
Revision ID: 8106d83ff594 | ||
Revises: 329c70502325 | ||
Create Date: 2024-01-31 10:23:21.163572 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '8106d83ff594' | ||
down_revision: Union[str, None] = '329c70502325' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
def upgrade() -> None: | ||
op.create_table( | ||
"fi_to_type_mapping_history", | ||
sa.Column("version", sa.Integer()), | ||
sa.Column("fi_id", sa.String(), nullable=False), | ||
sa.Column("type_id", sa.String(), nullable=False), | ||
sa.Column("details", sa.String()), | ||
sa.Column("modified_by", sa.String()), | ||
sa.Column("event_time", sa.DateTime(), server_default=sa.func.now(), nullable=False), | ||
sa.Column("changeset", sa.JSON), | ||
sa.PrimaryKeyConstraint("fi_id", "type_id", "version") | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_table("fi_to_type_mapping_history") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from sqlalchemy import Connection, Table, event, inspect | ||
from sqlalchemy.orm import Mapper | ||
|
||
from .models.dao import Base, FinancialInstitutionDao | ||
from entities.engine.engine import engine | ||
|
||
|
||
async def setup_fi_dao_listeners(): | ||
async with engine.begin() as connection: | ||
fi_history, mapping_history = await connection.run_sync( | ||
lambda conn: ( | ||
Table("financial_institutions_history", Base.metadata, autoload_with=conn), | ||
Table("fi_to_type_mapping_history", Base.metadata, autoload_with=conn), | ||
) | ||
) | ||
|
||
def insert_history( | ||
mapper: Mapper[FinancialInstitutionDao], connection: Connection, target: FinancialInstitutionDao | ||
): | ||
new_version = target.version + 1 if target.version else 1 | ||
changes = {} | ||
state = inspect(target) | ||
for attr in state.attrs: | ||
if attr.key == "event_time": | ||
continue | ||
attr_hist = attr.load_history() | ||
if not attr_hist.has_changes(): | ||
continue | ||
if attr.key == "sbl_institution_types": | ||
old_types = [o.as_dict() for o in attr_hist.deleted] | ||
new_types = [{**n.as_dict(), "version": new_version} for n in attr_hist.added] | ||
changes[attr.key] = {"old": old_types, "new": new_types} | ||
else: | ||
changes[attr.key] = {"old": attr_hist.deleted, "new": attr_hist.added} | ||
if changes: | ||
target.version = new_version | ||
for t in target.sbl_institution_types: | ||
t.version = new_version | ||
hist = target.__dict__.copy() | ||
hist.pop("event_time", None) | ||
history_columns = fi_history.columns.keys() | ||
for key in hist.copy(): | ||
if key not in history_columns: | ||
del hist[key] | ||
hist["changeset"] = changes | ||
types = [t.as_db_dict() for t in target.sbl_institution_types] | ||
connection.execute(fi_history.insert().values(hist)) | ||
connection.execute(mapping_history.insert().values(types)) | ||
|
||
event.listen(FinancialInstitutionDao, "before_insert", insert_history) | ||
event.listen(FinancialInstitutionDao, "before_update", insert_history) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters