Skip to content

Instantly share code, notes, and snippets.

@coliver
Created February 21, 2018 20:31
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coliver/8eac61b716ab1621ddc7a8b0540d10e5 to your computer and use it in GitHub Desktop.
Save coliver/8eac61b716ab1621ddc7a8b0540d10e5 to your computer and use it in GitHub Desktop.
How to fix a schema.rb conflict after a rebase

This is blatently summarized from here: http://dgmstuart.github.io/blog/2017/04/12/how-to-recover-from-rails-database-schema-conflicts-when-rebasing/

I wanted a more succeinct version available to me.

Don't

  • Try to manually merge it

Do

On your feature branch

  • Checkout and pull and ensure the database agrees with the schema.
  • Run rake db:migrate:status and ensure everything is 'up'
    • If anything is marked 'down' run rake db:migrate
    • If anything is marked *** NO FILE *** your db had a migration run against it outside of the current branch.
      • You can switch to the branch with the migration and rollback there.
  • Run your rebase: git pull --rebase origin master
  • At the first conflict, unstage the schema diff from your new branch:
git reset HEAD db/schema.rb
  • Discard the schema diff from your new branch:
git checkout db/schema.rb
  • Rebuild the schema:
rake db:migrate
  • Check the resulting schema diff against the original diff on your branch (e.g. by looking at the commit on the pull request) - it should be identical

    • …with one exception: if the migration you’re adding was created before any of the migrations on develop, then the version in the schema file won’t change. This is expected: that version should always relate to the most recently generated migration.

That’s it! You can now continue your rebase.

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