如何知道Rake任务中是否存在DB

时间:2011-08-20 03:32:46

标签: ruby-on-rails ruby-on-rails-3 rake

如何从rake任务中查明数据库是否存在?

也就是说,我想做类似的事情:

  task :drop_and_create => :environment do
    Rails.env = "development"
    if (db_exists?)
      Rake::Task["db:drop"].invoke
    end
    Rake::Task["db:create"].invoke
    #more stuff...
  end

我该如何编写db_exists?条件?

2 个答案:

答案 0 :(得分:1)

如何做一个开始/救援:

task :drop_and_create => :environment do
    Rails.env = "development"
    if (db_exists?)
    begin
      Rake::Task["db:drop"].invoke

    rescue Exception => e
      logger.debug("Error:#{e}")
    Rake::Task["db:create"].invoke
    #more stuff...
  end

答案 1 :(得分:0)

  task :drop_and_create => :environment do
    Rails.env = "development"
    Rake::Task["db:reset"].invoke
    #more stuff...
  end