Does rake db:schema:dump recreate schema.rb from migrations or the database itself?

50,274

The answer is simple: from the database.

By the way - when you take a look into the source code of db:* tasks you can see that migration tasks calls schema:dump after the run

desc "Migrate the database (options: VERSION=x, VERBOSE=false)."
task :migrate => :environment do
  ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
  ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
  Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end

So the migration works in the way that it change the database and then generate schema.rb file.

Share:
50,274
pingu
Author by

pingu

Updated on July 05, 2022

Comments

  • pingu
    pingu almost 2 years

    Does

    rake db:schema:dump
    

    recreate schema.rb from migrations or the database itself?

  • daraul
    daraul almost 2 years
    I have an existing SQL Server database that I wanted to dump. Doing rails db:schema:dump:sqlserver just created a sqlserver_schema.rb file with no tables in it, though.