From f034fd47c75f660503c1990e7ae855f2e087b7c6 Mon Sep 17 00:00:00 2001 From: Nargis Sultani Date: Sun, 22 Oct 2023 03:28:56 -0400 Subject: [PATCH] made schema configurable --- db_revisions/env.py | 39 ++++++++---- ...f8007003c6_create_a_baseline_migrations.py | 60 +++++++++++++++++++ 2 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 db_revisions/versions/5ef8007003c6_create_a_baseline_migrations.py diff --git a/db_revisions/env.py b/db_revisions/env.py index ca3bdf0..88d71fc 100644 --- a/db_revisions/env.py +++ b/db_revisions/env.py @@ -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 @@ -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") @@ -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. @@ -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() diff --git a/db_revisions/versions/5ef8007003c6_create_a_baseline_migrations.py b/db_revisions/versions/5ef8007003c6_create_a_baseline_migrations.py new file mode 100644 index 0000000..77c2bc0 --- /dev/null +++ b/db_revisions/versions/5ef8007003c6_create_a_baseline_migrations.py @@ -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")