2015-07-10 101 views
1

我跑我的任务箱DB:恢复并显示错误:Rspec的 - 未初始化的常量RAILS_ENV

** Execute db:restore 
rake aborted! 
NameError: uninitialized constant RAILS_ENV 
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rspec-core-2.5.2/lib/rspec/core/backward_compatibility.rb:20:in `const_missing' 
/home/dima/myapp/bdsmgalaxy/lib/tasks/mysql.rake:24:in `block (2 levels) in <top (required)>' 

我的任务是:

require 'yaml' 

namespace :db do 
    def backup_prep 
    @directory = File.join(RAILS_ROOT, 'db', 'backup') 
    @db = YAML::load(File.open(File.join(RAILS_ROOT, 'config', 'database.yml')))[ RAILS_ENV ] 
    @db_params = "-u #{@db['username']} #{@db['database']}" 
    @db_params = "-p#{@db['password']} #{@db_params}" unless @db['password'].blank? 
    end 

    desc 'Backup database by mysqldump' 
    task :backup => :environment do 
    backup_prep 
    FileUtils.mkdir @directory unless File.exists?(@directory) 
    file = File.join(@directory, "#{RAILS_ENV}_#{DateTime.now.to_s}.sql") 
    command = "mysqldump #{@db_params} | gzip > #{file}.gz" #--opt --skip-add-locks 
    puts "dumping to #{file}..." 
    # p command 
    exec command 
    end 

    desc "restore most recent mysqldump (from db/backup/*.sql.*) into the current environment's database." 
    task :restore => :environment do 
    unless RAILS_ENV=='development'  
     puts "Are you sure you want to import into #{RAILS_ENV}?! [y/N]" 
     return unless STDIN.gets =~ /^y/i 
    end 
    backup_prep 
    wildcard = File.join(@directory, ENV['FILE'] || "#{ENV['FROM']}*.sql*") 
    puts file = `ls -t #{wildcard} | head -1`.chomp # default to file, or most recent ENV['FROM'] or just plain most recent 
    if file =~ /\.gz(ip)?$/ 
     command = "gunzip < #{file} | mysql #{@db_params}" 
    else 
     command = "mysql #{@db_params} < #{file}" 
    end 
    p command 
    puts "please wait, this may take a minute or two..." 
    exec command 
    end 
end 

如何解决这个问题呢?

+0

在你的boot.rb'RAILS_ENV'定义? – Saurabh

+0

你尝试过'rake db:restore RAILS_ENV =“development”'? – CoupDeMistral

+0

@saurabh,不,不要在开机,rb。如何添加正确? – dev85

回答

2

RAILS_ENV是一个环境变量。你必须使用ENV['RAILS_ENV']

即:

unless ENV['RAILS_ENV']=='development' 
+0

谢谢,但现在显示错误NoMethodError:未定义方法'[]'为零:NilClass /home/dima/myapp/bdsmgalaxy/lib/tasks/mysql.rake:7:in'backup_prep' – dev85

+0

7行 - '@ db = YAML :: load(File.open(File.join(Rails.root,'config','database.yml')))[ENV ['RAILS_ENV']]' – dev85

相关问题