Whenever someone else in my repo changes something related to the database, the Schema version will obviously conflict if I also make a change related to the database. Is there a way to configure git to automatically use the newest version of the scheme in ActiveRecord::Schema.define
, or will I need to always do these merge conflicts manually?

- 7,173
- 6
- 33
- 61
-
Yeah, super annoying, I just had to do this. I've had some projects that were so bad, that we just took the schema file out of version control (which only has minor downsides). Otherwise, there is stuff like [this](http://tbaggery.com/2010/10/24/reduce-your-rails-schema-conflicts.html) which I've never tried because it seems a bit heavy-handed. – Brad Werth Feb 03 '17 at 18:59
1 Answers
I think you're looking at this the wrong way. If there are conflicts in schema.rb
(or structure.sql
) then your database is probably no longer consistent with your code and migrations. This inconsistency is the real problem and trying to ignore it by fiddling with git won't solve the problem but it might hide it.
If you merge in some code and end up with conflicts in schema.rb
then either there are new migrations that you haven't run or someone is doing something shifty; in the latter case you have a human problem that has to be solved through human means. If, however, there are new migrations then you should be able to rake db:migrate
to run them and get a brand new conflict-free schema.rb
that you can commit to solve the conflicts and complete the merge. If a simple rake db:migrate
doesn't solve the problem then your new migrations are fighting with the migrations you're trying to merge and git won't help here either, you have to fix that conflict by hand (i.e. make peace between the migrations) so that rake db:migrate
will solve your problem.
If there is a conflict in schema.rb
but it isn't related to migrations (this should be rare but less rare if you use structure.sql
) then a simple rake db:schema:dump
(or rake db:structure:dump
) should give you a fresh and clean schema.rb
to commit.
Executive summary:
schema.rb
conflicts suggest conflicts elsewhere that git cannot fix.- Humans fix human problems.
- Programmers (also humans for the most part) fix migration conflicts and those fix
schema.rb
conflicts. - If all else fails,
rake db:schema:dump
a newschema.rb
and commit that with the merge.
Leaving schema.rb
out of your repository is not a solution.

- 1
- 1

- 426,620
- 70
- 833
- 800