6

Working on a Rails 5 app, I want to use structure.sql instead of schema.rb (we're using PostGIS with lots of custom SQL calls...). In config/initializers/database_options.rb I have the following:

# use structure.sql, not schema.rb
Rails.application.config.active_record.schema_format = :sql

If I run the following:

$ rake db:migrate

it generates db/schema.rb, not db/structure.sql.

The rails guides say:

There are two ways to dump the schema. This is set in config/application.rb by the config.active_record.schema_format setting, which may be either :sql or :ruby.

What magic am I missing here?

Doug
  • 176
  • 1
  • 9

2 Answers2

6

I think you should put your rails component config before Initializers. The rails application initialize by the following order.

  • config/application.rb
  • Environment-specific configuration files
  • Initializers
  • After-initializers

You could put your config config.active_record.schema_format = :sql either in config/application.rb or config/environments/development.rb depends on environment you used.

That should work.

Weibo Chen
  • 369
  • 1
  • 10
3

In your initializer do:

Rails.application.configure do
  config.active_record.schema_format = :sql
end
Paul Sturgess
  • 3,254
  • 2
  • 23
  • 22
  • Thanks for providing an answer, since that notified me and I realized that I hadn't ever accepted bananaappletw's answer. The problem I had was that I was setting this in an initializer (as you suggest as well), but it needs to be in either `config/application.rb` or one of the environment configs. – Doug Apr 16 '19 at 17:18
  • 2
    The solution I posted works for me in an initializer. – Paul Sturgess Apr 17 '19 at 19:26