7

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?

Assaf Shomer
  • 1,429
  • 1
  • 15
  • 21
  • 1
    Does this apply only to migrations or to spinning up your app in general? – max Dec 11 '15 at 14:50
  • Hi max. Only the migration part – Assaf Shomer Dec 13 '15 at 13:28
  • 1
    same, using Rails 5.2.2. still looking for solution. mina deploy --version extremely slow at migration part – NamNamNam Jan 21 '19 at 10:14
  • Same problem here (Rails 5.2, Ruby 2.6). Capistrano runs the `rake db:migrate` task and that takes about 2 minutes, even if *there aren't any pending migrations*. The database is very large (100M records). Still looking for a solution. – collimarco Sep 11 '19 at 08:42

2 Answers2

1

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?

Community
  • 1
  • 1
ChrisBarthol
  • 4,871
  • 2
  • 21
  • 24
0

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.

collimarco
  • 34,231
  • 36
  • 108
  • 142