Rake任务将database.example.yml复制到database.yml

时间:2014-10-16 18:49:06

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

通过rake task

将database.example.yml复制到database.yml时出错

copy_files.rake

namespace :copy do
  desc "copy database.yml & application.yml"
  task :config_files => :environment do
    source = File.join(Rails.root, "config", "database.example.yml")
    target = File.join(Rails.root, "config", "database.yml")
    FileUtils.cp_r source, target
  end
end

错误

$ rake copy:config_files
rake aborted!
Cannot load `Rails.application.database_configuration`:
Could not load database configuration. No such file - 
/Library/Ruby/Gems/2.0.0/gems/railties-4.1.4/lib/rails/application/configuration.rb:105:in `database_configuration'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.4/lib/active_record/railtie.rb:128:in `block (2 levels) in <class:Railtie>'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/lazy_load_hooks.rb:27:in `each'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.4/lib/active_record/railtie.rb:118:in `block in <class:Railtie>'
/Library/Ruby/Gems/2.0.0/gems/railties-4.1.4/lib/rails/initializable.rb:30:in `instance_exec'
/Library/Ruby/Gems/2.0.0/gems/railties-4.1.4/lib/rails/initializable.rb:30:in `run'
/Library/Ruby/Gems/2.0.0/gems/railties-4.1.4/lib/rails/initializable.rb:55:in `block in run_initializers'
/Library/Ruby/Gems/2.0.0/gems/railties-4.1.4/lib/rails/initializable.rb:54:in `run_initializers'
/Library/Ruby/Gems/2.0.0/gems/railties-4.1.4/lib/rails/application.rb:300:in `initialize!'
/Library/Ruby/Gems/2.0.0/gems/railties-4.1.4/lib/rails/railtie.rb:194:in `public_send'
/Library/Ruby/Gems/2.0.0/gems/railties-4.1.4/lib/rails/railtie.rb:194:in `method_missing'
/config/environment.rb:5:in `<top (required)>'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `block in require'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
/Library/Ruby/Gems/2.0.0/gems/railties-4.1.4/lib/rails/application.rb:276:in `require_environment!'
/Library/Ruby/Gems/2.0.0/gems/railties-4.1.4/lib/rails/application.rb:379:in `block in run_tasks_blocks'
Tasks: TOP => copy:config_files => environment
(See full trace by running task with --trace)

我看到错误是因为没有database.yml文件。有没有办法通过rake任务复制database.yml文件?

2 个答案:

答案 0 :(得分:2)

我很惊讶!但@SrikanthJeeva是对的:))

我设法在没有database.yml的情况下运行rake任务。您应该删除:environment并且不能使用Rails.root(因为您无法在没有database.yml的情况下加载Rails)。这对我有用

  desc "copy database.yml & application.yml"
  task :config_files do
    source = File.join(Dir.pwd, "config", "database.example.yml")
    target = File.join(Dir.pwd, "config", "database.yml")
    FileUtils.cp_r source, target
  end

答案 1 :(得分:0)

正如上面评论中提到的@gotva,你需要一个&#34;有效的&#34;使用=> :environment运行rake任务的环境。这样,rake任务加载Rails环境,它需要一个database.yaml文件。

您可以使用脚本实现这一目标,或者使用Ruby实现IRB会话:

$ cd /app_rails_root_path
$ irb
> Dir.glob("config/*.yml.example").each { |f| FileUtils.cp(f, f.gsub(/\.example/,'')) }