jrhorn424
jrhorn424

Reputation: 2007

Executing db:test:prepare from another rake task

I'm trying to execute rake db:test:prepare from another task.

namespace :db do
  namespace :populate do
    desc "Seed development database"
    task development: :environment do
      puts "Kill local server"
      %x{ ps xauwww | grep -i --regex="[t]hin" | awk '{print $2}' | xargs kill }
      puts "Resetting development database"
      Rake::Task['db:reset'].execute
      puts "Migrating development database"
      Rake::Task['db:migrate'].execute
      puts "Populating development database"
      Rake::Task['db:populate'].execute
      puts "Pepare test database"
      Rake::Task['db:test:prepare'].execute
      puts "Start local server"
      system 'thin -d start'
    end
   …
end

Using invoke instead of execute doesn't help. It seems to work fine if I define it by itself:

task example: :environment do
  Rake::Task['db:test:prepare'].execute
end

When I run rake db:populate:development, all the tasks are run except for Rake::Task['db:test:prepare'].execute. There's no activity in the development log for that command, but it doesn't prevent the next task from running (starting the server). Usually, I see some SQL statements when I run db:test:prepare by itself.

Notes:

$ rails -v
Rails 3.2.2

$ ruby -v
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]

$ uname -a
Darwin hook 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:47:41 PST 2012; root:xnu-1699.24.23~1/RELEASE_X86_64 x86_64

Upvotes: 7

Views: 1088

Answers (3)

gwintrob
gwintrob

Reputation: 3543

The Rake task likely gets to this line https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L364 and ActiveRecord::Base.configurations is blank. This variable is set here https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L5 (see https://github.com/rails/rails/blob/370e1ad3f1f30f955bd781f0ecd113409b8ccf8f/activerecord/lib/active_record/tasks/database_tasks.rb#L21).

Is there a chance that your database.yml is missing the current environment or is something else clearing the configuration?

Upvotes: 0

mahercbeaucoup
mahercbeaucoup

Reputation: 597

I know this isn't the correct way of doing it, but I was having similar problems and ended up calling it using:

`rake db:test:prepare`

This is the only method that seemed to work for me.

Upvotes: 1

Vibhuti
Vibhuti

Reputation: 1634

Try Rake::Task['db:test:prepare'].invoke instead of .execute, so that it runs dependent tasks first. But it only invokes the task if its not invoked first.

Refer: this

Upvotes: 0

Related Questions