Default behaviour for rails is to generate a
db/schema.rb when running migrations or dumping the database.
Schema.rb is great because it can be used for populating other database types and is used to populate the test database.
And I personally think it's great becaus of the vscode plugin Rails Schema which shows the database structure of your application in a sidebar (TIP, move this bar to the right panel).
The rake task
db:migrate default behaviour is to invoke a
db:schema:dump when migrating is done. By default this generates the
This output of the generated file is used to popuplate the test-database..
But somethimes the
db/schema.rb isn't good enough for popuplating the test database or using the file to generate the basic structure. Contraints are quirky.
(For example see previous article mysql json constraints).
To generate a native sql dump of the database structure, you can change the default structure to sql. Place the following line in your
config.active_record.schema_format = :sql
When enabling this dumps are generated to
But you loose the
Here's a tip to work with
structure.sql and just generate the
schema.rb (for your lovely sidebar).
Create a file in
lib/tasks/schema_dump.rb and enhance the dump task, so it also creates the schema.rb file.
Rake::Task['db:schema:dump'].enhance do File.open(Rails.root.join('db/schema.rb'), 'w') do |stream| ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) end end
NOTE: structure.sql contains yet another MySQL bug. (See next article for workaround)
Tip: use postgresql !