Skip to content

Commit

Permalink
Merge branch 'main' into features/45_running_alembic_on_app_startup
Browse files Browse the repository at this point in the history
  • Loading branch information
nargis-sultani authored Nov 28, 2023
2 parents 5e68327 + fd0bb61 commit 23d0ca6
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 36 deletions.
15 changes: 7 additions & 8 deletions db_revisions/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from dotenv import load_dotenv

# SBL-specific configuration

ENV = os.getenv("ENV", "LOCAL")

if ENV == "LOCAL":
Expand Down Expand Up @@ -75,18 +74,18 @@ def run_migrations_online() -> None:

if connectable is None:
connectable = engine_from_config(
context.config.get_section(context.config.config_ini_section),
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 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
8 changes: 5 additions & 3 deletions db_revisions/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from alembic import op
from sqlalchemy import engine_from_config
from sqlalchemy.engine import reflection
import sqlalchemy


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)
engine = config.attributes.get("connection", None)
if engine is None:
engine = engine_from_config(config.get_section(config.config_ini_section), prefix="sqlalchemy.")
inspector = sqlalchemy.inspect(engine)
tables = inspector.get_table_names()
return table_name in tables
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def upgrade() -> None:
"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.Column("event_time", sa.DateTime(), server_default=sa.func.now(), nullable=False),
sa.ForeignKeyConstraint(
["lei"],
["financial_institutions.lei"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ 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.Column("event_time", sa.DateTime(), server_default=sa.func.now(), nullable=False),
sa.PrimaryKeyConstraint("domain"),
)
op.create_index(op.f("ix_denied_domains_domain"), "denied_domains", ["domain"], unique=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def upgrade() -> None:
"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.Column("event_time", sa.DateTime(), server_default=sa.func.now(), nullable=False),
sa.PrimaryKeyConstraint("lei"),
)
op.create_index(op.f("ix_financial_institutions_lei"), "financial_institutions", ["lei"], unique=True)
Expand Down
Loading

0 comments on commit 23d0ca6

Please sign in to comment.