通过rake任务将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 文件的方法?
我很惊讶!但@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
您需要一个“有效”环境来使用
=> :environment
运行 rake 任务。这样,rake 任务就会加载 Rails 环境,并且需要存在 database.yaml
文件。
例如,您可以在 IRB 会话中使用 Ruby 本身来实现此目的:
$ cd /app_rails_root_path
$ irb
> Dir.glob("config/*.yml.example").each { |f| FileUtils.cp(f, f.gsub(/\.example/, '')) }