Search code examples
ruby-on-rails-3rakeubuntu-12.04rake-task

Custom rake tasks are running when I run 'rake db:migrate'. How do I prevent this?


I'm running rails on ubuntu 12.04. When I run the code below I get an error from one of my custom tasks. Problem is I didn't want my custom tasks to run in the first place. Just the db migrations.

rake db:migrate --trace

rake aborted!
cannot load such file -- CSV
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in `require'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in `block in require'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in `require'
/var/www/myapp/lib/tasks/import_inventory.rake:1:in `<top (required)>'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:245:in `load'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:245:in `block in load'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:245:in `load'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/engine.rb:425:in `block in load_tasks'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/engine.rb:425:in `each'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/engine.rb:425:in `load_tasks'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/application.rb:145:in `load_tasks'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/railtie/configurable.rb:30:in `method_missing'
/var/www/myapp/Rakefile:7:in `<top (required)>'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load_rakefile'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:501:in `raw_load_rakefile'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:82:in `block in load_rakefile'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:81:in `load_rakefile'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:65:in `block in run'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in `load'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in `<main>'
/home/js/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in `eval'
/home/js/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in `<main>'

Rake task being ran:

require 'CSV'

namespace :db do
  desc 'Prepare transition database.'
  task :import_inventory => :environment do
    desc 'Import Assets Table'

    file_path = "/Users/js/data/TSI/inventory.csv"

    CSV.foreach(file_path, :headers => true) do |row|
      asset = Asset.new
      asset.asid_tag = row[0]
      asset.description = row[1]
      asset.qty = row[2]
      asset.manufactuer = row[3]
      asset.model = row[4]
      asset.save
    end
   end
end

Solution

  • When you run Rake all the task files (lib/tasks/*.rb) are loaded and parsed before rake can determine what tasks are available. This means, whether you call the import task or not, the file import_inventory.rake is being parsed by ruby before the actual task you requested (db:migrate) is run. As a result require 'CSV' is executed, regardless of what task you are really trying to run.

    As for a fix, I'm guessing it should be require 'csv' (lowercase) rather than require 'CSV' (uppercase)?