Do you have to go in and edit your alembic revisions generated with alembic revision --autogenerate
all the
time because it's generating a bunch of schema changes that shouldn't be there? This basically means that your
local database schema is out of date with the production database schema. It's easy to ignore this and just go
in and delete all of the irrelevant schema changes that make it into your revision, but who has the time for that?
This is actually a pretty simple problem to solve, just follow these instructions:
- Checkout the
master
branch, and make sure it's up to date:~$ git checkout master && git pull
- Get your local database up to date with the most recent revision
~$ alembic upgrade head
- Try autogenerating a revision without having made any changes to models
~$ alembic revision --autogenerate
- If the generated revision is empty, congrats! Your local schema is in the right place. Otherwise keep going...
- Upgrade your schema using your new autogenerated revision
~$ alembic upgrade head
- This part is important "Downgrade" back to the actual revision head on master, you can do this in one of two ways:
-
Reimplement the autogenerated
def downgrade()
method in your new revision to just bedef downgrade(): pass
Then run
~$ alembic downgrade -1
and delete your new revision. -
Change the alembic version number in the database to the revision hash of the previous head before your autogenerated revision:
Then delete your new revision.UPDATE alembic_versions SET version_num = '<previous_hash>';
-
After completing these steps you should be able to run ~$ alembic revision --autogenerate
and see no changes in your
revision!