db:drop can run without failure when db does not exist
这正是我所需要的:我需要运行 db:drop 但如果数据库不存在则不抛出异常或停止我的整个过程,如果数据库存在则删除该数据库或什么都不做。
我怎样才能做到这一点?我怎么知道
db:drop
如果数据库不存在,不要破坏我的生活?这是我遇到问题的代码(它有帮助):
namespace :db do
task import: :environment do
Rake::Task["db:drop"].invoke # If the database doesn't already exist, the whole import process terminates!
Rake::Task["db:create"].invoke
Rake::Task["db:migrate"].invoke
database_config = Rails.configuration.database_configuration[Rails.env]
system "psql --username=#{database_config['username']} #{database_config['database']} < PostgreSQL.sql"
end
end
最佳答案
为什么不能进行简单的异常处理
namespace :db do
task import: :environment do
begin
Rake::Task["db:drop"].invoke # If the database doesn't already exist, the whole import process terminates!
Rake::Task["db:create"].invoke
Rake::Task["db:migrate"].invoke
database_config = Rails.configuration.database_configuration[Rails.env]
system "psql --username=#{database_config['username']} #{database_config['database']} < PostgreSQL.sql"
rescue Exception => e
p "The Exception is #{e.message}"
end
end
end
关于ruby-on-rails - 运行 rake db :drop without failure if database doesn't exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16532772/