Skip to content

Instantly share code, notes, and snippets.

@azrosen92
Last active April 26, 2019 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azrosen92/facd21d5aa81dafb5730ea5607c8a05b to your computer and use it in GitHub Desktop.
Save azrosen92/facd21d5aa81dafb5730ea5607c8a05b to your computer and use it in GitHub Desktop.
Have a bunch of extra stuff in your automatically generated alembic revisions?

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:

  1. Checkout the master branch, and make sure it's up to date: ~$ git checkout master && git pull
  2. Get your local database up to date with the most recent revision ~$ alembic upgrade head
  3. Try autogenerating a revision without having made any changes to models ~$ alembic revision --autogenerate
  4. If the generated revision is empty, congrats! Your local schema is in the right place. Otherwise keep going...
  5. Upgrade your schema using your new autogenerated revision ~$ alembic upgrade head
  6. 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 be

      def 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:

    UPDATE alembic_versions SET version_num = '<previous_hash>';
    Then delete your new revision.

After completing these steps you should be able to run ~$ alembic revision --autogenerate and see no changes in your revision!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment