From 72d93081fb4b23b08d8bb366e0c7dfccb46d3035 Mon Sep 17 00:00:00 2001 From: Conor Brady Date: Mon, 12 Aug 2024 16:17:10 -0700 Subject: [PATCH] Escape db url in alembic config env --- api/alembic/env.py | 27 +++++++++------------------ api/app/db/database.py | 2 -- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/api/alembic/env.py b/api/alembic/env.py index 838f8c4c2..d92f6a561 100644 --- a/api/alembic/env.py +++ b/api/alembic/env.py @@ -1,5 +1,5 @@ -""" Configurations for Alembic migrations -""" +"""Configurations for Alembic migrations""" + from logging.config import fileConfig import sqlalchemy from alembic import context @@ -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. @@ -30,7 +31,7 @@ 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(",") @@ -38,12 +39,11 @@ def exclude_tables_from_config(config_): # 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: @@ -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() @@ -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() diff --git a/api/app/db/database.py b/api/app/db/database.py index dc374d897..d5f69ea8f 100644 --- a/api/app/db/database.py +++ b/api/app/db/database.py @@ -1,6 +1,5 @@ """Setup database to perform CRUD transactions""" -import configparser import logging import urllib.parse from typing import Generator, AsyncGenerator @@ -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="~()*!.'")