Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
58 lines (32 sloc) 1.85 KB

ImportFu

This gem adds bulk import functionality to ActiveRecord model using the fast MySQL's 'LOAD DATA INFILE' feature. This gem code is largely inspired by the import_with_load_data_in_file plugin. (thanks Paolo Dona)

Installation

Install the gem from github

gem install guillaumegentil-import_fu --source http://gems.github.com

Add the gem to your Rails environment.rb file:

config.gem "guillaumegentil-import_fu", :lib => "import_fu", :source => "http://gems.github.com"

Include import_fu in your ActiveRecord Model:

class Foo < ActiveRecord::Base
  include ImportFu
end

Usage

Import from an array of values

columns = [:name, :size]
values = [["toto", "32"],
          ["tata", "33"]]

Foo.import columns, values

Import from a csv file (MySQL needs to have access on this file, so be sure it has the proper ownership/permissions)

columns = [:name, :size]

Foo.import columns, '/path/of/a/csv'

Options

By default ImportFu adds timestamps values (Time.now.utc) when importing from an array, this can be avoided with:

Foo.import columns, values, :timestamps => false

By default ImportFu does NOT reformat values passed in the array (like Time or Data value), if you want the reformat use:

Foo.import columns, values, :format => true

By default ImportFu does NOT replace already existing values in database and ignores them (like values with an id already used), if you want the replace behavior use:

Foo.import columns, values, :replace => true

If your Rails app and your MySQL database are on the same machine, you can speed up the import by passing:

Foo.import columns, values, :local => true

Author

Thibaud Guillaume-Gentil