0

I have already built several Rake tasks to import a series of CSV files into my PostgreSQL database, but I've always had those tasks directed to a path on my local computer.

I need to have the tasks set up to access a secure on directory so my client can simply copy files into this directory, and when the Rake task is executed it automatically pulls from the online source.

This seems like it would be a simple adjustment in my code but I'm not sure how to make it work. If anyone has any suggested reading or can quickly help edit my code it would be appreciated.

Below is a copy of one of my Rake tasks:

require 'csv'
namespace :import_command_vehicles_csv do
  task :create_command_vehicles => :environment do
    puts "Import Command Vehicles"
    csv_text = File.read('/Users/Ben/Sites/ror/LFD/command_vehicles.csv', :encoding => 'windows-1251:utf-8')
    csv = CSV.parse(csv_text, :headers => true)
    csv.each_with_index do |row,index|
      row = row.to_hash.with_indifferent_access
      CommandVehicle.create!(row.to_hash.symbolize_keys)
      command_vehicle = CommandVehicle.last
      if @report_timesheet_hash.key?(command_vehicle.report_nr)
        command_vehicle.timesheet_id = "#{@report_timesheet_hash[command_vehicle.report_nr]}"
      end
      #command_vehicle.timesheet_id = @timesheet_id_array[index]
      command_vehicle.save
    end
  end
end
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
RubyDude1012
  • 477
  • 1
  • 8
  • 14

2 Answers2

1

'/Users/Ben/Sites/ror/LFD/command_vehicles.csv' will only work on your local machine, unless you have an identical path to your resources on the remote host, which is hardly likely. As a result, you need to change the absolute path, to one that is relative to your file.

I have a folder hierarchy on my Desktop, stuck several folders down, like:

- test
  | lib
  | - test.rb
  | config
  | - config.yaml

And test.rb contains:

File.realpath('../config/config.yaml', File.dirname(__FILE__))

__FILE__ is a special variable that returns the absolute path to the script currently being interpreted. That makes it easy to find where something else is if you know where it exists relative to the current __FILE__.

The value returned by realpath will be the absolute path to the "config.yaml" file, based on its relative path from the current file. In other words, that would return something like:

/Users/ttm/Desktop/tests/junk/test/config/config.yaml

The nice thing about Rake is it's really using Ruby scripts, so you can take advantage of things like File.realpath. Also, remember that Rake's idea of the current-working directory is based on the location of the script being run, so everything it wants to access has to be relative to the script's location, or an absolute path, or you have to change Rake's idea of the current-working directory by using a chdir or cd.

Doing the later makes my head hurt eventually so I prefer to define some constants pointing to the required resources, and figuring out the real absolute path becomes easy doing it this way.

See "How can I get the name of the command called for usage prompts in Ruby?" for more information.

Community
  • 1
  • 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
1

You can create rake tast that can accept parameters and then you can pass whatever path you like.

Check out this link Set Multiple Environmental Variables On Invocation of Rake Task to see how to pass parameters via enviroment variables and this link http://viget.com/extend/protip-passing-parameters-to-your-rake-tasks to see bracket notation.

Community
  • 1
  • 1
Michał Młoźniak
  • 5,466
  • 2
  • 22
  • 38