22

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?

3 Answers 3

32

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
0

alembic upgrade <revision_name> --sql

0

Found this question when I was trying to get the sql script downgrade to the end.

(env)# alembic downgrade -1 --sql
ERROR:alembic.util.messaging:downgrade with --sql requires <fromrev>:<torev>
  FAILED: downgrade with --sql requires <fromrev>:<torev>

The case is quite rare. Maybe someone will be useful. If you need to get a SQL and only 1 revision, then you can do this:

alembic downgrade 6a68657325e7:-1 --sql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.