Search code examples
alembic

Alembic - How do I get the sql script of a single revision?


I'm trying to get the SQL script generated from an alembic revision, but when I run:

alembic upgrade head --sql

It outputs the SQL of all revisions, and because there's a breaking revision in the middle (I haven't figured out why yet), I can't get the script from that point on. Moreover, I only want the SQL script of the last revision.

Is there a parameter to get that?


Solution

  • After some time trying to figure it out, and reading the docs, you can obtain the SQL of a single or multiple specific revisions by specifying start:end in the command line:

     alembic upgrade <previous>:<current> --sql
    

    It will even output the update to the alembic_version table.

    The same works with downgrade:

     alembic downgrade <current>:<previous> --sql