Skip to content

Instantly share code, notes, and snippets.

@deril
Forked from jeromedalbert/.gitattributes
Created November 22, 2019 13:54
Show Gist options
  • Save deril/123b36ce19d3197f1f3b3125c5b984a1 to your computer and use it in GitHub Desktop.
Save deril/123b36ce19d3197f1f3b3125c5b984a1 to your computer and use it in GitHub Desktop.
Automatically resolve Git merge conflicts in Rails schema.rb by picking the most recent date in the conflict (now works with Rails 5 and recent versions of Git). The following files should be in your home ~ directory. Inspired by https://tbaggery.com/2010/10/24/reduce-your-rails-schema-conflicts.html
db/schema.rb merge=railsschema
...
[core]
...
attributesfile = /Users/your_user/.gitattributes
...
[merge "railsschema"]
name = newer Rails schema version
driver = "ruby -e '\n\
system %(git), %(merge-file), %(--marker-size=%L), %(%A), %(%O), %(%B)\n\
b = File.read(%(%A))\n\
b.sub!(/^<+ .*\\nActiveRecord::Schema\\.define.version: ([0-9_]+). do\\n=+\\nActiveRecord::Schema\\.define.version: ([0-9_]+). do\\n>+ .*/) do\n\
%(ActiveRecord::Schema.define(version: #{[$1, $2].max}) do)\n\
end\n\
File.open(%(%A), %(w)) {|f| f.write(b)}\n\
exit 1 if b.include?(%(<)*%L)'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment