Managing conflict in schema.rb created by Git operation

Ruby on-RailsGitMigration

Ruby on-Rails Problem Overview


I created a migration, ran rake db:migrate, which bumped my db/schema.rb version number. Then I did a git fetch origin master and saw that there were changes from my team members. So I did a git stash and a git rebase FETCH_HEAD, followed by a git stash pop. This resulted in a conflict in db/schema.rb over the version number.

Upstream>>>
ActiveRecord::Schema.define(:version => 20110930179257) do
===========
ActiveRecord::Schema.define(:version => 20110930161932) do
<<<Stashed

I think the appropriate fix would be to manually increment the version number to something higher than the upstream.

Is this sensible, or bad news?

Thanks, Max

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

If your current database has the correct schema, you should:

  • Run pending migrations (if any)

     rake db:migrate
    
  • Overwrite your schema.rb from your current database schema

     rake db:schema:dump
    
  • And commit

Solution 2 - Ruby on-Rails

When I find myself with this conflict, I simply migrate the database. Whether there are pending migrations or not, the conflict will be corrected.

Solution 3 - Ruby on-Rails

tldr

Accept the Upstream version and run rake db:migrate as you'd normally do.

why is that the way to go

Don't worry about the migrations you've created (which are below Upstream version 20110930179257). ActiveRecord uses a table schema_migrations where it puts all of the migrations that have been run. If your migrations aren't on the list but in db/migrate directory, then ActiveRecord will run them.

Here's the table so you can visualise it better: schema_migrations table

It's tempting to think that it's actually this line: ActiveRecord::Schema.define(:version => 20110930179257) that defines latest migration run, so no migrations with version below it are going to be run. This is fortunately not true. Rails will run any migrations that are in db/migrate folder and not yet in the schema_migrations table.

Solution 4 - Ruby on-Rails

According to this answer, a conflict is guaranteed. The user has to to manually merge, and set the version as the higher of the two.

Solution 5 - Ruby on-Rails

Here's what I do when merging master into my feature branch fails over conflicts in db/schema.rb:

$ git merge --abort
$ git checkout master
$ rake db:drop db:create db:migrate
$ git checkout -- db/schema.rb
$ git checkout my_feature_branch
$ rake db:migrate
$ git add db/schema.rb
$ git commit -m 'Updated schema'
$ git merge master

Solution 6 - Ruby on-Rails

~/bin/update-schema-rb:

#!/usr/bin/env bash

git co master
bin/rake db:reset db:seed
git co -
bin/rake db:migrate

Solution 7 - Ruby on-Rails

An option is to have a manual version script which is additive only. There you want conflicts. Schema is something that will bite you hard if you don't keep on top of it. So once bitten, twice shy, I don't mind keeping schema and lookup info (list of customer types) with an additive only change management scheme.

Solution 8 - Ruby on-Rails

I feel the best approach is to do rake db:drop db:create db:migrate to regenerate schema using migrations that exist only on the current branch. That way migrations from other branches won't leak into your schema file and give you headache later - in case if you switch branches a lot.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmaxenglanderView Question on Stackoverflow
Solution 1 - Ruby on-RailsWizard of OgzView Answer on Stackoverflow
Solution 2 - Ruby on-RailsSean MView Answer on Stackoverflow
Solution 3 - Ruby on-RailsAdam SibikView Answer on Stackoverflow
Solution 4 - Ruby on-RailslulalalaView Answer on Stackoverflow
Solution 5 - Ruby on-RailsMark KreymanView Answer on Stackoverflow
Solution 6 - Ruby on-RailsDorianView Answer on Stackoverflow
Solution 7 - Ruby on-RailsAdam DymitrukView Answer on Stackoverflow
Solution 8 - Ruby on-RailsDr.StrangeloveView Answer on Stackoverflow