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

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.

Need help on your Ruby on Rails or React project?

Join Our Newsletter