Reputation: 154545
I created a migration with alembic revision --autogenerate
, applied it to my development database with alembic upgrade head
, and then realised it wasn't quite what I wanted.
How can I revert the migration so that I can tweak it and try again?
Upvotes: 130
Views: 137613
Reputation: 1225
Yes, in "online" case please downgrade, but there is one more trick, just remove unwanted version file while offline to bind "head" to "base" back. For example, occasionally I've created revision, and then remove it before alembic upgrade
Upvotes: 0
Reputation: 4573
be careful using alembic
consider next revisions:
aaa <-- head
bbb
ccc
ddd <-- base
if you execute alembic upgrade head
you are going to aaa
revision.
if you execute alembic downgrade base
you are going to ddd
revision.
if you execute several times alembic downgrade -1
eventually you can go from aaa
to ddd
revision. Idem to alembic upgrade +1
if you only want to iterate between aaa
and bbb
revisions I recommend to use:
for upgrade:
alembic upgrade head
for downgrade
alembic downgrade head-1
Upvotes: 8
Reputation: 742
Just a note for the answer of Mark Amery:
If you want to run to downgrade() of a version, you will need to run alembic downgrade the-version-before-it
, which mean it will revert to the version after the version that you want to downgrade.
For example, you run alembic history
and get the below list:
And assume you want to revert to cdd99ec41968
, then you will have to run command:
alembic downgrade 15972effcbd3
Which is the version before the version that we want to revert.
Hope it's clear enough.
Upvotes: 8
Reputation: 154545
Assuming that you only want to go back one revision, use alembic downgrade
with a relative migration identifier of -1:
alembic downgrade -1
This will run the downgrade()
method of your latest revision and update the alembic_version
table to indicate the revision you're now at.
If you need to go back multiple migrations, run
alembic history
to view a list of all the migrations in your project (from newest to oldest), then copy and paste the identifier of the migration you want to go back to:
alembic downgrade 8ac14e223d1e
There's currently no command to delete migrations from your versions directory, so if you want to completely wipe away all trace of your bad migration, you'll need to delete the version file (like 4c009570237e_add_widget_table.py
) manually.
Upvotes: 238