简体   繁体   中英

How to solve Rails merge conflicts with db/structure.sql

I was told that I should follow the below steps if make new migrations in my branch and have merge conflicts with master in the db/structure.sql file.

  1. In the branch, bundle exec rake db:drop
  2. In the branch, bundle exec rake db:create
  3. In master, bundle exec rake db:structure:load
  4. In the branch, git merge master
  5. In the branch, bundle exec rake db:migrate

What are steps 1-3 necessary if all I want to do is align the db/structure.sql file? By merging in master, don't I get the new migrations I haven't ran yet and then by running them, it will update my db/structure.sql ?

You're right, dropping and recreating the database to solve a conflict in db/structure.sql (or db/schema.rb for that matter) is a little ridiculous. You should be able to simply run the new migrations and get an updated structure.sql from the db:migrate .


The db/stucture.sql file is simply the database's structure as the database sees it (whereas db/schema.rb is the database's structure in the limited view of it that ActiveRecord has). If there is a conflict in structure.sql , that simply means:

  1. The merge involves new migrations which change the database structure.
  2. The branch you're merging in has run the migrations in a different order so the schema's don't quite match up even though they may be functionally equivalent.

(1) is solved by running the new migrations and possibly fixing any places where the migrations themselves are in conflict. A quick bin/rake db:migrate should fix this and leave you with a new non-conflicted db/structure.sql .

(2) is solved the same way. You could also do aa manual bin/rake db:structure:dump to rebuild db/structure.sql but you'd only do this if you're certain that you really do have this situation; but really, db:migrate will take care of it so there's no reason not to just db:migrate .

Conflicts in db/structure.sql (or db/schema.rb ) don't indicate a problem with the db/structure.sql itself, they indicate problems with the database that git can't directly see. The solution to the conflicts is to fix the database.

You can just run bundle exec rake db:structure:dump to re-generate the db/structure.sql file.

For schema.rb I found https://stackoverflow.com/a/3815807/6003161

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM