activerecord_sane_schema_dumper
activerecord_sane_schema_dumper copied to clipboard
:memo: ActiveRecord::SaneSchemaDumper removes useless/harmful whitespace from Rails’ generated `db/schema.rb` file.
ActiveRecord::SaneSchemaDumper
ActiveRecord::SaneSchemaDumper modifies the behavior of Rails’
ActiveRecord::SchemaDumper so that the generated db/schema.rb file doesn’t
contain any useless/harmful whitespace column alignment.
⚠️ WARNING · OBSOLETE ⚠️
This gem is no longer required since Rails 6 (reference commit). Rails no longer adds superfulous whitespace when dumping the schema 🙌.
Installation
Add this line to your application’s Gemfile in your development group:
group :development do
gem 'activerecord_sane_schema_dumper'
end
Usage
The gem modifies the behavior of Rails’ rake db:schema:dump task. This task
is part of Rails migrations process to ensure that db/schema.rb stays in-sync
with your local database.
It removes all useless whitespace that Rails adds so adding/modifying/removing columns from your tables only touches relevant lines (because other table lines do not try to stay aligned).
Before
# db/schema.rb
create_table "event_data", force: true do |t|
t.string "event_external_id"
t.json "data", default: {}
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
But what happens if I remove the data column and regenerate db/schema.rb? Here’s the resulting diff:
create_table "event_data", force: true do |t|
t.string "event_external_id"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
- t.json "data", default: {}
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
end
We’ve removed the data column but created_at and updated_at lines are affected too. This is not good.
After
# db/schema.rb
create_table "event_data", force: true do |t|
t.string "event_external_id"
t.json "data", default: {}
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
If I remove the data column and regenerate the db/schema.rb file, only the relevant line will be touched:
create_table "event_data", force: true do |t|
t.string "event_external_id"
- t.json "data", default: {}
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
One removed column, one affected line in the table definition. Much better.
License
ActiveRecord::SaneSchemaDumper is © 2014-2016 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.
About Mirego
Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.
We also love open-source software and we try to give back to the community as much as we can.