简体   繁体   中英

Rails migration extremely slow even when there are no pending migrations

My production rails application takes 167 seconds to run rake db:migrate. The sad part is that there are no migrations to run . I tried to condition the migration running on checking that there are pending migrations but then the check took just as long. The only "excuse" in my mind is that the db is not tiny, there are 1M records there, but I see no reason why that would matter at all. I looked in the log but there is nothing indicating anything going wrong. I am running with

  • Ruby 2.2.0
  • Rails 4.2.0

Does anyone have an idea why this is so, and whether there is anything to do about it?

Running rake db:migrate task also invoke the db:schema:dump task, which will update your db/schema.rb. So even though you have no migrations you are causing other rake tasks to run which could be taking up that time depending on how many migrations/large your database schema is.

You can look into source code of db:* tasks (.../activerecord/railties/databases.rake)

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

References: http://guides.rubyonrails.org/active_record_migrations.html#running-migrations

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

The rake db:migrate task was extremely slow on a new server, even when there were no pending migrations.

Finally I found that I had a wrong Redis configuration, and the time was actually spent loading the Rails environment (which rake db:migrate does) and not running the migrations.

If you have a similar issue I suggest that you run:

rails runner "puts 'hello'"

If that takes a long time, then the problem is related to the Rails configuration, not something specific to migrations. In that case you can use CTRL-C after some seconds to kill the process, so that you can see the stack trace and identify where your code hangs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM