7

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
jrhorn424
  • 1,981
  • 2
  • 21
  • 27

3 Answers3

1

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.

mahercbeaucoup
  • 597
  • 5
  • 15
0

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

Community
  • 1
  • 1
Vibhuti
  • 1,584
  • 16
  • 19
0

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?

gwintrob
  • 3,253
  • 2
  • 18
  • 14
  • Good idea, but that's not it either. The `database.yml` is set up correctly and checking `ActiveRecord::Base.configurations.blank?` in my custom rake task returns `false`. – jrdioko Jul 30 '13 at 03:45
  • Where are you placing the debug statement? Could you try adding it immediately before line https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L364? – gwintrob Jul 30 '13 at 15:02
  • I just tried that and still got `false`. And with the debugger I was able to step through and confirm that it was entering the `unless` clause (and from there stepped into Rails internals that I don't quite understand). – jrdioko Jul 30 '13 at 16:46
  • I see. Could you include what the db:populate task does (perhaps it's this gem https://github.com/ffmike/db-populate)? Another idea--could you call your example task that wraps db:test:prepare rather than use the task directly? Not a great solution but at least it solves your problem! – gwintrob Jul 30 '13 at 20:13
  • My case actually isn't using `db:populate` at all (and doesn't have the gem installed). I'm doing a series of standard rake tasks (`db:drop`, `db:create`, etc). – jrdioko Jul 30 '13 at 21:39
  • And playing around with subtasks that call it might be another workaround, but I'm still curious why it doesn't work directly. – jrdioko Jul 30 '13 at 21:43