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
When I find myself with this conflict, I simply migrate the database. Whether there are pending migrations or not, the conflict will be corrected.
Accept the Upstream version and run rake db:migrate
as you'd normally do.
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:
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.
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.
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
~/bin/update-schema-rb
:
#!/usr/bin/env bash
git co master
bin/rake db:reset db:seed
git co -
bin/rake db:migrate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With