Alembic Load Config From Env Vars

Today, while trying to integrate the python library Alembic into a micro-service, I ran into the issue of needing to load the database connection string from a few environmental variables. Needless to say I went ahead and did some googling and compiled this solution:

def get_url():
    """Custom method to get config from the environmental variables.

    Based on: http://allan-simon.github.io/blog/posts/python-alembic-with-environment-variables/
    """
    return "postgres://{}:{}@{}/{}".format(
        os.getenv("DB_USER", "username"),
        os.getenv("DB_PASSWORD", "password"),
        os.getenv("DB_HOST", "database_host"),
        os.getenv("DB_NAME", "database_name"),
    )

Then inside the run_migrations_offline method I swapped out:

url = config.get_main_option("sqlalchemy.url")

for

url = get_url()

and

connectable = engine_from_config(
    config.get_section(config.config_ini_section),
    prefix="sqlalchemy.",
    poolclass=pool.NullPool,
)

for

connectable = create_engine(get_url())

Don't forget the import:

from sqlalchemy import create_engine

Enjoy!


Posted on 2019-02-25