How to know if DB exists from within Rake task

混江龙づ霸主 提交于 2019-12-11 01:59:59

问题


How do i find out if the database exists from within a rake task?

that is, i'd like to do something like:

  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

how do i write the db_exists? condition?


回答1:


How about instead doing a begin/rescue:

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



回答2:


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


来源:https://stackoverflow.com/questions/7129452/how-to-know-if-db-exists-from-within-rake-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!