diff --git a/db_revisions/env.py b/db_revisions/env.py index c09d478..1a14900 100644 --- a/db_revisions/env.py +++ b/db_revisions/env.py @@ -1,8 +1,7 @@ import os from dotenv import load_dotenv from logging.config import fileConfig -from sqlalchemy import engine_from_config -from sqlalchemy import pool +from sqlalchemy import engine_from_config, pool from alembic import context from entities.models import Base @@ -47,13 +46,6 @@ # ... etc. -def include_object(object, name, type_, reflected, compare_to): - if type_ == "table" and name == "alembic_version": - return False - else: - return True - - def run_migrations_offline() -> None: """Run migrations in 'offline' mode. This generates the SQL script without executing on the database. @@ -97,10 +89,7 @@ def run_migrations_online() -> None: with connectable.connect() as connection: context.configure( - connection=connection, - target_metadata=target_metadata, - version_table_schema=target_metadata.schema, - include_object=include_object, + connection=connection, target_metadata=target_metadata, version_table_schema=target_metadata.schema ) with context.begin_transaction(): diff --git a/db_revisions/utils.py b/db_revisions/utils.py new file mode 100644 index 0000000..a8c9d57 --- /dev/null +++ b/db_revisions/utils.py @@ -0,0 +1,11 @@ +from alembic import op +from sqlalchemy import engine_from_config +from sqlalchemy.engine import reflection + + +def table_exists(table_name): + config = op.get_context().config + engine = engine_from_config(config.get_section(config.config_ini_section), prefix="sqlalchemy.") + inspector = reflection.Inspector.from_engine(engine) + tables = inspector.get_table_names() + return table_name in tables diff --git a/db_revisions/versions/20e0d51d8be9_create_financial_institution_domains_.py b/db_revisions/versions/20e0d51d8be9_create_financial_institution_domains_.py new file mode 100644 index 0000000..a865a2d --- /dev/null +++ b/db_revisions/versions/20e0d51d8be9_create_financial_institution_domains_.py @@ -0,0 +1,44 @@ +"""Create financial_institution_domains table + +Revision ID: 20e0d51d8be9 +Revises: f76c5004993f +Create Date: 2023-11-02 11:37:52.487064 + +""" +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 = "20e0d51d8be9" +down_revision: Union[str, None] = "f76c5004993f" +branch_labels: Union[str, Sequence[str], None] = None +# depends_on: Union[str, Sequence[str], None] = None +depends_on = ["f76c5004993f"] + + +def upgrade() -> None: + if not table_exists("financial_institution_domains"): + op.create_table( + "financial_institution_domains", + sa.Column("domain", sa.String(), nullable=False), + sa.Column("lei", sa.String(), nullable=False), + sa.Column("event_time", sa.DateTime(), server_default=sa.text("now()"), nullable=False), + sa.ForeignKeyConstraint( + ["lei"], + ["financial_institutions.lei"], + ), + sa.PrimaryKeyConstraint("domain", "lei"), + ) + op.create_index( + op.f("ix_financial_institution_domains_domain"), "financial_institution_domains", ["domain"], unique=False + ) + op.create_index( + op.f("ix_financial_institution_domains_lei"), "financial_institution_domains", ["lei"], unique=False + ) + + +def downgrade() -> None: + op.drop_table("financial_institution_domains") diff --git a/db_revisions/versions/a98b11074c54_create_denied_domains_table.py b/db_revisions/versions/a98b11074c54_create_denied_domains_table.py new file mode 100644 index 0000000..dbc2bae --- /dev/null +++ b/db_revisions/versions/a98b11074c54_create_denied_domains_table.py @@ -0,0 +1,34 @@ +"""Create denied_domains table + +Revision ID: a98b11074c54 +Revises: +Create Date: 2023-11-02 11:31:54.882727 + +""" +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 = "a98b11074c54" +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + if not table_exists("denied_domains"): + op.create_table( + "denied_domains", + sa.Column("domain", sa.String(), nullable=False), + sa.Column("event_time", sa.DateTime(), server_default=sa.text("now()"), nullable=False), + sa.PrimaryKeyConstraint("domain"), + ) + op.create_index(op.f("ix_denied_domains_domain"), "denied_domains", ["domain"], unique=False) + + +def downgrade() -> None: + op.drop_table("denied_domains") diff --git a/db_revisions/versions/aa098560725b_create_a_baseline_migration.py b/db_revisions/versions/aa098560725b_create_a_baseline_migration.py deleted file mode 100644 index dee0a63..0000000 --- a/db_revisions/versions/aa098560725b_create_a_baseline_migration.py +++ /dev/null @@ -1,60 +0,0 @@ -"""Create a baseline migration - -Revision ID: aa098560725b -Revises: -Create Date: 2023-10-25 10:07:38.214272 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision: str = "aa098560725b" -down_revision: Union[str, None] = None -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - op.create_table( - "denied_domains", - sa.Column("domain", sa.String(), nullable=False), - sa.Column("event_time", sa.DateTime(), server_default=sa.text("now()"), nullable=False), - sa.PrimaryKeyConstraint("domain"), - ) - op.create_index(op.f("ix_denied_domains_domain"), "denied_domains", ["domain"], unique=False) - op.create_table( - "financial_institutions", - sa.Column("lei", sa.String(), nullable=False), - sa.Column("name", sa.String(), nullable=False), - sa.Column("event_time", sa.DateTime(), server_default=sa.text("now()"), nullable=False), - sa.PrimaryKeyConstraint("lei"), - ) - op.create_index(op.f("ix_financial_institutions_lei"), "financial_institutions", ["lei"], unique=True) - op.create_index(op.f("ix_financial_institutions_name"), "financial_institutions", ["name"], unique=False) - op.create_table( - "financial_institution_domains", - sa.Column("domain", sa.String(), nullable=False), - sa.Column("lei", sa.String(), nullable=False), - sa.Column("event_time", sa.DateTime(), server_default=sa.text("now()"), nullable=False), - sa.ForeignKeyConstraint( - ["lei"], - ["financial_institutions.lei"], - ), - sa.PrimaryKeyConstraint("domain", "lei"), - ) - op.create_index( - op.f("ix_financial_institution_domains_domain"), "financial_institution_domains", ["domain"], unique=False - ) - op.create_index( - op.f("ix_financial_institution_domains_lei"), "financial_institution_domains", ["lei"], unique=False - ) - - -def downgrade() -> None: - op.drop_table("financial_institution_domains") - op.drop_table("financial_institutions") - op.drop_table("denied_domains") diff --git a/db_revisions/versions/f76c5004993f_create_financial_institutions_table.py b/db_revisions/versions/f76c5004993f_create_financial_institutions_table.py new file mode 100644 index 0000000..a77f031 --- /dev/null +++ b/db_revisions/versions/f76c5004993f_create_financial_institutions_table.py @@ -0,0 +1,36 @@ +"""Create financial_institutions table + +Revision ID: f76c5004993f +Revises: a98b11074c54 +Create Date: 2023-11-02 11:34:43.808166 + +""" +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 = "f76c5004993f" +down_revision: Union[str, None] = "a98b11074c54" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + if not table_exists("financial_institutions"): + op.create_table( + "financial_institutions", + sa.Column("lei", sa.String(), nullable=False), + sa.Column("name", sa.String(), nullable=False), + sa.Column("event_time", sa.DateTime(), server_default=sa.text("now()"), nullable=False), + sa.PrimaryKeyConstraint("lei"), + ) + op.create_index(op.f("ix_financial_institutions_lei"), "financial_institutions", ["lei"], unique=True) + op.create_index(op.f("ix_financial_institutions_name"), "financial_institutions", ["name"], unique=False) + + +def downgrade() -> None: + op.drop_table("financial_institutions") diff --git a/pyproject.toml b/pyproject.toml index fd78129..a27ebee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,6 @@ python-jose = "^3.3.0" requests = "^2.31.0" asyncpg = "^0.27.0" alembic = "^1.12.0" -pytest-alembic = "0.10.7" [tool.poetry.group.dev.dependencies] diff --git a/src/entities/models/dto.py b/src/entities/models/dto.py index 82fc5df..d78fd6c 100644 --- a/src/entities/models/dto.py +++ b/src/entities/models/dto.py @@ -73,7 +73,7 @@ def from_claim(cls, claims: Dict[str, Any]) -> "AuthenticatedUser": ) @classmethod - def parse_institutions(cls, institutions: Optional[List[str]] = None) -> List[str]: + def parse_institutions(cls, institutions: List[str] | None) -> List[str]: """ Parse out the list of institutions returned by Keycloak