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?
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
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.
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.
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.
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.
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