6

Rails 7 now reads ENV["SCHEMA_FORMAT"] when doing rails db:schema:{dum...

 2 years ago
source link: https://blog.saeloun.com/2022/08/30/rails-db-schema-supports-schema-format-env
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Rails 7 now reads ENV["SCHEMA_FORMAT"] when doing rails db:schema:{dump,load}

Aug 30, 2022 , by Swaathi Kakarla

1 minute read

Rails is notorious for introducing breaking changes. Almost 2 years ago the Rails team decided to combine and deprecate rails db:structure:{dump,load} tasks into rails db:schema:{dump,load}. This was part of the “May of WTFs” strategy aimed to reduce many WTFs that happen when we use Rails. One of the WTFs chosen was to reduce the duplication of schema dump functions.

In Rails, there are separate tasks for db:schema:{dump,load} and db:structure:{dump,load}. The schema function dumps/loads the database into a Ruby format, while the structure function dumps it in a SQL format. It was decided to combine these into a single set of tasks and load either the schema or structure based on config.active_record.schema_format.

It seemed like a good idea at the time, however, it seems like there was solid reasoning to have separate tasks to load/dump different formats. As pointed out by @jeremy on Github,

  • Apps, engines, CI flows, etc that invoke db:schema:dump to get a schema.rb file (say, for distribution) may now get a structure.sql file instead.
  • And workflows that produce an additional structure.sql dump (say, for fast Docker database boot-up) will no longer be possible.

This was because the load/dump structure could only be changed by a Rails config, instead of something that’s outside the system.

Before

When Rails deprecated db:structure:{dump,load} tasks this is what was followed:

  • To generate a db/schema.rb file, set config.active_record.schema_format to :ruby before running rails db:schema:dump.
  • To generate a db/structure.sql file, set config.active_record.schema_format to :sql before running rails db:schema:dump.

After

Now it’s easier than ever after this PR:

  • To generate a db/schema.rb file, set ENV["SCHEMA_FORMAT"] to :ruby before running rails db:schema:dump.
  • To generate a db/structure.sql file, set ENV["SCHEMA_FORMAT"] to :sql before running rails db:schema:dump.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK