Support for postgres URL from an environment variable for alembic

1,152 views
Skip to first unread message

Selena Deckelmann

unread,
Mar 25, 2015, 2:12:23 PM3/25/15
to sqlalchem...@googlegroups.com
Hi!

We are working on a cloud deployment of our application, and would love to use a URL from the environment to configure alembic's connection to Postgres. 

Are there any existing plans to make this happen? If we make a patch, would that be something you'd consider integrating upstream? 

Thanks!
-selena

Michael Bayer

unread,
Mar 25, 2015, 3:33:54 PM3/25/15
to sqlalchem...@googlegroups.com
Hi Selena -

If I’m understanding correctly, this can certainly be done now. The env.py
file within the Alembic script environment is provided there so that any
deployment-specific customizations can happen, including that the URL for
the database connection can be derived by any means desired.

So if by “the environment”, we meant for example “an environment variable”,
that’s easy. Just consult os.environ inside the env.py script:

import os

def run_migrations_online():

url = os.environ.get(“ALEMBIC_URL”, None)
if url:
connectable = create_engine(url)
else:
# this is what env.py usually does
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)

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

with context.begin_transaction():
context.run_migrations()

Other ways to get at URLs or even existing connections include through
custom command line arguments sent to the alembic runner, which can be done
with the -x flag, there’s a short example which seems to be exactly what
you’re looking for here:
http://alembic.readthedocs.org/en/latest/api.html#alembic.environment.EnvironmentContext.get_x_argument

There’s more ways than that too! If you want to run commands
programmatically and have them all on the same transaction, there’s a recipe
for that here:
http://alembic.readthedocs.org/en/latest/cookbook.html#sharing-a-connection-with-a-series-of-migration-commands-and-environments

The way that Alembic finds a database to connect to has always tried to be
as open ended as possible. Let me know that one of these methods will work
for you.



> Thanks!
> -selena
>
> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alem...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Selena Deckelmann

unread,
Mar 25, 2015, 4:07:39 PM3/25/15
to sqlalchem...@googlegroups.com
On Wed, Mar 25, 2015 at 12:33 PM, Michael Bayer <mik...@zzzcomputing.com> wrote:


Selena Deckelmann <sel...@maxipad.org> wrote:

> Hi!
>
> We are working on a cloud deployment of our application, and would love to use a URL from the environment to configure alembic's connection to Postgres.
>
> Are there any existing plans to make this happen? If we make a patch, would that be something you'd consider integrating upstream?


Hi Selena -

If I’m understanding correctly, this can certainly be done now.

Hah!  I completely missed '-x'.  Thanks so much!  That is exactly what we need.  Thanks, Mike.
  
-selena
Reply all
Reply to author
Forward
0 new messages