Search code examples
pythonalembic

alembic post migration commands


Due to some quirks in our database we need to reassign table owners post creation. Currently we leverage Alembic - has anyone got a simple way to create some sort of post hook that runs certain SQL commands after a migration?


Solution

  • It sounds like what you're asking for can be resolved by editing the env.py file of alembic.. From the docs (emphasis mine):

    env.py - The env.py script is part of the generated environment so that the way migrations run is entirely customizable. The exact specifics of how to connect are here, as well as the specifics of how the migration environment are invoked. The script can be modified so that multiple engines can be operated upon, custom arguments can be passed into the migration environment, application-specific libraries and models can be loaded in and made available.

    I bet you could get the behavior you want by adding a call in run_migrations_online().


    (This is assuming you want to run the post_hook after EACH migration. If it was migration specific, you can update the upgrade() function in the generated migration file)