14

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
user229044
  • 232,980
  • 40
  • 330
  • 338
Coderama
  • 11,050
  • 12
  • 44
  • 58
  • What makes you think the whitespaces are tabs? Any chance of seeing the whole before and after versions of one of those `create_table`s? – mu is too short Jul 18 '13 at 02:44
  • @muistooshort Thanks, they could be whitespaces – Coderama Jul 18 '13 at 23:45
  • 1
    The first one just means that not everyone's columns are in the same order inside the database, compare the `\d accounts` output inside `psql` and you'll find the culprit; this can happen in various ways and shouldn't be a problem. The second one is probably just an alignment thing (i.e. make all the `:default => ...` options line up) but it is hard to tell without seeing the whole before and after `schema.rb` files. – mu is too short Jul 19 '13 at 01:39

3 Answers3

6

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.

jakeonrails
  • 1,885
  • 15
  • 37
2

Good news! Rails finally pushed out an update which solves exactly this. Starting from Rails v5.1.0, the default (and only option) is to remove any extra spaces between the columns.

Girrafish
  • 2,320
  • 1
  • 19
  • 32
-17

You just should ignore schema.rb from your version control. It changes differently depending on th order of migrations and who generated them. You only need the migrations to generate the appropiated schema for each developer involved in the project.

Regards.

Core2Juan
  • 354
  • 1
  • 11
  • 5
    *Correction*: You just should **NEVER** ignore schema.rb from your version control. http://stackoverflow.com/questions/6520017/is-it-a-good-idea-to-put-db-schema-rb-to-gitignore-list – Dorian Feb 06 '15 at 17:35