Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 830
Coderama Avatar asked Jul 18 '13 02:07

Coderama


People also ask

How does migration work in Rails?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.

What is migration in Ruby on Rails?

Migrations are a convenient way to alter your database schema over time in a consistent way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent. You can think of each migration as being a new 'version' of the database.

What is up and down in rails migration?

The up and down methods are a more granular way of defining the change method in a Rails migration. The up method is a set of instructions of what to do when you migrate, and the down method is a set of directions of what to do when you rollback.


1 Answers

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.

like image 166
jakeonrails Avatar answered Oct 19 '22 04:10

jakeonrails