How do I force RAILS_ENV in a rake task?

后端 未结 6 886
暗喜
暗喜 2020-11-28 06:00

I have this little rake task:

namespace :db do 
  namespace :test do 
    task :reset do 
      ENV[\'RAILS_ENV\'] = \"test\" 
      Rake::Task[\'db:drop\']         


        
6条回答
  •  抹茶落季
    2020-11-28 06:51

    Another option is to check the env and refuse to continue:

    unless Rails.env.development?
      puts "This task can only be run in development environment"
      exit
    end
    

    or ask if they really want to continue:

    unless Rails.env.development?
      puts "You are using #{Rails.env} environment, are you sure? y/n"
      continue = STDIN.gets.chomp
      exit unless continue == 'y'
    end
    

提交回复
热议问题