Skip to content

Commit

Permalink
Escape db url in alembic config env
Browse files Browse the repository at this point in the history
  • Loading branch information
conbrad committed Aug 12, 2024
1 parent 7e5d0ff commit 72d9308
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
27 changes: 9 additions & 18 deletions api/alembic/env.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" Configurations for Alembic migrations
"""
"""Configurations for Alembic migrations"""

from logging.config import fileConfig
import sqlalchemy
from alembic import context
Expand All @@ -11,7 +11,8 @@
# access to the values within the .ini file in use.
# sqlalchemy.url uses variables from .env file
config = context.config
config.set_main_option('sqlalchemy.url', DB_WRITE_STRING)
escaped_db_url = DB_WRITE_STRING.replace("%", "%%")
config.set_main_option("sqlalchemy.url", escaped_db_url)

# Interpret the config file for Python logging.
# This line sets up loggers basically.
Expand All @@ -30,20 +31,19 @@


def exclude_tables_from_config(config_):
""" There are tables (e.g. spatial_ref_sys created by postgis), that must be ignored. """
"""There are tables (e.g. spatial_ref_sys created by postgis), that must be ignored."""
tables_ = config_.get("tables", None)
if tables_ is not None:
tables = tables_.split(",")
return tables


# load tables to be excluded
exclude_tables = exclude_tables_from_config(
config.get_section('alembic:exclude'))
exclude_tables = exclude_tables_from_config(config.get_section("alembic:exclude"))


def include_object(object, name, type_, reflected, compare_to):
""" any tables not in the ignore list, are to be included """
"""any tables not in the ignore list, are to be included"""
if type_ == "table" and name in exclude_tables:
return False
else:
Expand All @@ -63,13 +63,7 @@ def run_migrations_offline():
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
include_object=include_object
)
context.configure(url=url, target_metadata=target_metadata, literal_binds=True, dialect_opts={"paramstyle": "named"}, include_object=include_object)

with context.begin_transaction():
context.run_migrations()
Expand All @@ -89,10 +83,7 @@ def run_migrations_online():
)

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

with context.begin_transaction():
context.run_migrations()
Expand Down
2 changes: 0 additions & 2 deletions api/app/db/database.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Setup database to perform CRUD transactions"""

import configparser
import logging
import urllib.parse
from typing import Generator, AsyncGenerator
Expand All @@ -11,7 +10,6 @@
from .. import config

logger = logging.getLogger(__name__)
configparser.ConfigParser(interpolation=None)
write_user = config.get("POSTGRES_WRITE_USER", "wps")
read_user = config.get("POSTGRES_READ_USER", "wpsread")
postgres_password = urllib.parse.quote(config.get("POSTGRES_PASSWORD", "wps"), safe="~()*!.'")
Expand Down

0 comments on commit 72d9308

Please sign in to comment.