Run alembic upgrade header inside a python function

736 views
Skip to first unread message

Dorian Hoxha

unread,
Jan 5, 2015, 6:19:58 AM1/5/15
to sqlalchem...@googlegroups.com
Hi list(),

I'm currently using 1 db / customer. That means in development I test the migration with 1 db and on production I want to run the 'upgrade' command on each database.

So I just want something like:

def upgrade(connection details or metadata, or metadata + engine)


I looked at the env.py:run_migrations_online(), but what does it do, migrate OR upgrade ?

Thank you

Michael Bayer

unread,
Jan 5, 2015, 11:39:42 AM1/5/15
to sqlalchem...@googlegroups.com
your env.py script makes all the determinations as to when the migrations
run and on what database connection. For a small number of databases, I will
often place different sections within alembic.ini and run alembic upgrade
passing the —name argument:

[customer_one]
sqlalchemy.url = driver://user:pass@localhost/dbname


[customer_two]
sqlalchemy.url = driver://user:pass@localhost/dbname


alembic upgrade head —name customer_one


if you want to do something more dynamic you can call upon
run_migrations_online() multiple times within the env.py script, see the
example in the “multidb” template. run_migrations_online() runs whatever the
current command is from the outside, it’s passed in as a function.

>



> Thank you
>
> --
> 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.

Dorian Hoxha

unread,
Jan 5, 2015, 1:22:00 PM1/5/15
to sqlalchem...@googlegroups.com
Thanks Michael,

What I ended up doing is, in the end of env.py:


for c in get_clients():
    # generate metadata for this client
    # generate engine for this client

    if context.is_offline_mode():
        run_migrations_offline(metadata)
    else:
        run_migrations_online(metadata, engine)
Reply all
Reply to author
Forward
0 new messages