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?
Alembic can view the status of the database and compare against the table metadata in the application, generating the “obvious” migrations based on a comparison. This is achieved using the --autogenerate option to the alembic revision command, which places so-called candidate migrations into our new migrations file.
The head will automatically revert to the most recent remaining migration. Using stamp will set the db version value to the specified revision; not alter the head revision number. Based on your question, the original answer will resolve your issue.
Then I ran the alembic upgrade head command. This command makes sure that the database is up to date with the most recent revision. As you can see alembic correctly ran the baseline revision, created the bug table according to the upgrade instruction and also added its own table alembic_version .
Alembic provides for the creation, management, and invocation of change management scripts for a relational database, using SQLAlchemy as the underlying engine. This tutorial will provide a full introduction to the theory and usage of this tool. To begin, make sure Alembic is installed as described at Installation.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With