ruby-on-railsactiverecord

Need help creating structure.sql for my Rails app


I'm trying to manually create/update the db/structure.sql file for test purposes. I have "config.active_record.schema_format = :sql" set in application.rb. I am wondering if there is a command equivalent to "ActiveRecord::SchemaDumper.dump" that will create or update structure.sql. I've tried structure_dump, but the output is nil:

> ActiveRecord::Base.connection.structure_dump
=> nil

I also tried passing a file to that method:

> File.open( File.join(Rails.root, 'db/structure.sql')) { |f| ActiveRecord::Base.connection.structure_dump() }
 => nil 

So far, the only way I've been able to generate or update db/structure.sql is by running rake db:migrate. Any alternatives? Or am I missing something when trying to run ActiveRecord::Base.connection.structure_dump?


Solution

  • UPDATE:

    Use SCHEMA_FORMAT=sql rails db:schema:dump for Rails 7+

    OLD ANSWER:

    Try rake db:structure:dump which will recreate the db/structure.sql from the database.

    For the latest versions of rails the command rake db:schema:dump works with db/structure.sql too.