Skip to content

Commit

Permalink
made schema configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Nargis Sultani committed Oct 22, 2023
1 parent c6b2045 commit f034fd4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 13 deletions.
39 changes: 26 additions & 13 deletions db_revisions/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context

from src.entities.models import Base

# this is the Alembic Config object, which provides
Expand All @@ -16,17 +15,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 = 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 @@ -40,11 +28,31 @@
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 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.
Expand Down Expand Up @@ -84,7 +92,12 @@ def run_migrations_online() -> None:
)

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

with context.begin_transaction():
context.run_migrations()
Expand Down
60 changes: 60 additions & 0 deletions db_revisions/versions/5ef8007003c6_create_a_baseline_migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Create a baseline migrations
Revision ID: 5ef8007003c6
Revises:
Create Date: 2023-10-22 02:43:08.751445
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "5ef8007003c6"
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"],
["public.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")

0 comments on commit f034fd4

Please sign in to comment.