Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket 37 - move init.sql from sbl-project repo to user-fi-management #43

Merged
merged 22 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions db_revisions/env.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
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 import models
from entities.models import Base

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand All @@ -18,17 +14,6 @@
if config.config_file_name is not None:
fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = models.Base.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.

# this specific to SBL configuration

ENV = os.getenv("ENV", "LOCAL")
Expand All @@ -42,11 +27,24 @@
INST_DB_PWD = os.environ.get("INST_DB_PWD")
INST_DB_HOST = os.environ.get("INST_DB_HOST")
INST_DB_NAME = os.environ.get("INST_DB_NAME")
INST_DB_SCHEMA = os.environ.get("INST_DB_SCHEMA")
INST_CONN = f"postgresql://{INST_DB_USER}:{INST_DB_PWD}@{INST_DB_HOST}/{INST_DB_NAME}"
config.set_main_option("sqlalchemy.url", INST_CONN)

# end specific SBL configuration

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel

target_metadata = Base.metadata
target_metadata.schema = INST_DB_SCHEMA

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode. This generates the SQL script without executing on the database.
Expand Down Expand Up @@ -79,17 +77,23 @@ def run_migrations_online() -> None:
and associate a connection with the context.

"""
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
connectable = context.config.attributes.get("connection", None)

if connectable is None:
connectable = engine_from_config(
context.config.get_section(context.config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata, version_table_schema=target_metadata.schema
)

with context.begin_transaction():
context.run_migrations()
with context.begin_transaction():
context.run_migrations()


if context.is_offline_mode():
Expand Down
2 changes: 1 addition & 1 deletion db_revisions/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def upgrade() -> None:


def downgrade() -> None:
${downgrades if downgrades else "pass"}
${downgrades if downgrades else "pass"}
11 changes: 11 additions & 0 deletions db_revisions/utils.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""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 = ["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")
2 changes: 0 additions & 2 deletions db_revisions/versions/README

This file was deleted.

34 changes: 34 additions & 0 deletions db_revisions/versions/a98b11074c54_create_denied_domains_table.py
Original file line number Diff line number Diff line change
@@ -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")
Original file line number Diff line number Diff line change
@@ -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")
Loading
Loading