Rails migration is always inserting whitespaces or changing the ordering of columns in the schema

pg, postgresql-9.2, rails-postgresql, ruby-on-rails, ruby-on-rails-3.2

Solution

I built a gem to solve this problem.

It sorts columns, index names and foreign keys, removes excess whitespace and runs Rubocop for some formatting to unify the output of your schema.rb file.

https://github.com/jakeonrails/fix-db-schema-conflicts

After you add it to your Gemfile you just run rake db:migrate or rake db:schema:dump like normal.

Problem

There is a frustrating issue where my rails migrations update the schema with whitespaces and the position of the table's columns. So most times when I run `bundle exec rake db:migrate` it will do one of the below scenarios. When I merge this into our main branch and other developers work off this, then their rails migration reverts tabs and position ordering. We have noticed that all three developers on the team have the same issue when running a migration if I have been the last committer of the schema. I just updated `postgres to v9.2.4` that is the same as the other devs. Any ideas of what else I could try? Examples Below are git diffs to demonstrate what is happening. Example of re-ordering the schema: ``` create_table "accounts", :force => true do |t| t.integer "organisation_id" - t.boolean "active", :default => false t.text "notes" + t.boolean "active", :default => false end ``` Example of adding tabs to the schema: ``` create_table "comments", :force => true do |t| - t.integer "commentable_id", :default => 0 - t.string "commentable_type", :default => "" + t.integer "commentable_id", :default => 0 + t.string "commentable_type", :default => "" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false ```

Original source

Related problems