ruby-on-railsrakerake-task

Rake task to copy database.example.yml to database.yml


Getting error while copying database.example.yml to database.yml through rake task

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

Error

$ 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)

I see that the error is because there is no database.yml file. Is there any possible way for copying database.yml file through a rake task?


Solution

  • I am surprised!!! But @SrikanthJeeva is right :)

    I managed to run rake task without database.yml. You should remove :environment and you can't use Rails.root (because you can't load Rails without database.yml). This works for me

      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