guillaumegentil-import_fu 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +58 -0
- data/Rakefile +2 -2
- data/import_fu.gemspec +7 -7
- data/lib/import_fu.rb +5 -6
- data/test/database.yml +7 -0
- data/test/fixtures/foos.csv +2 -0
- metadata +8 -6
data/README.markdown
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# ImportFu
|
2
|
+
|
3
|
+
This gem adds bulk import functionality to ActiveRecord model using the fast [MySQL's 'LOAD DATA INFILE'](http://dev.mysql.com/doc/refman/5.0/en/load-data.html) feature.
|
4
|
+
This gem code is largely inspired by the [import\_with\_load\_data\_in\_file](http://github.com/paolodona/import\_with\_load\_data\_in\_file/tree) plugin. (thanks Paolo Dona)
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Install the gem from github
|
9
|
+
|
10
|
+
gem install guillaumegentil-import_fu --source http://gems.github.com
|
11
|
+
|
12
|
+
Add the gem to your Rails environment.rb file:
|
13
|
+
|
14
|
+
config.gem "guillaumegentil-import_fu", :lib => "import_fu", :source => "http://gems.github.com"
|
15
|
+
|
16
|
+
Include import_fu in your ActiveRecord Model:
|
17
|
+
|
18
|
+
class Foo < ActiveRecord::Base
|
19
|
+
include ImportFu
|
20
|
+
end
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Import from an array of values
|
25
|
+
|
26
|
+
columns = [:name, :size]
|
27
|
+
values = [["toto", "32"],
|
28
|
+
["tata", "33"]]
|
29
|
+
|
30
|
+
Foo.import columns, values
|
31
|
+
|
32
|
+
Import from a csv file (MySQL needs to have access on this file, so be sure it has the proper ownership/permissions)
|
33
|
+
|
34
|
+
columns = [:name, :size]
|
35
|
+
|
36
|
+
Foo.import columns, '/path/of/a/csv'
|
37
|
+
|
38
|
+
### Options
|
39
|
+
|
40
|
+
By default ImportFu adds timestamps values (Time.now.utc) when importing from an array, this can be avoided with:
|
41
|
+
|
42
|
+
Foo.import columns, values, :timestamps => false
|
43
|
+
|
44
|
+
By default ImportFu does NOT reformat values passed in the array (like Time or Data value), if you want the reformat use:
|
45
|
+
|
46
|
+
Foo.import columns, values, :format => true
|
47
|
+
|
48
|
+
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:
|
49
|
+
|
50
|
+
Foo.import columns, values, :replace => true
|
51
|
+
|
52
|
+
If your Rails app and your MySQL database are on the same machine, you can speed up the import by passing:
|
53
|
+
|
54
|
+
Foo.import columns, values, :local => true
|
55
|
+
|
56
|
+
## Author
|
57
|
+
|
58
|
+
Thibaud Guillaume-Gentil
|
data/Rakefile
CHANGED
@@ -4,11 +4,11 @@ require 'rake/testtask'
|
|
4
4
|
require 'rake/rdoctask'
|
5
5
|
require 'echoe'
|
6
6
|
|
7
|
-
Echoe.new('import_fu', '0.1.
|
7
|
+
Echoe.new('import_fu', '0.1.2') do |p|
|
8
8
|
p.description = "Add quick mass data import from CSV or Array to Active Record model"
|
9
9
|
p.url = "http://github.com/guillaumegentil/import_fu/tree"
|
10
10
|
p.author = "Thibaud Guillaume-Gentil"
|
11
11
|
p.email = "guillaumegentil@gmail.com"
|
12
12
|
p.ignore_pattern = ["tmp/*", "script/*"]
|
13
|
-
p.development_dependencies = ["
|
13
|
+
p.development_dependencies = ["fastercsv", "active_record"]
|
14
14
|
end
|
data/import_fu.gemspec
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{import_fu}
|
5
|
-
s.version = "0.1.
|
5
|
+
s.version = "0.1.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Thibaud Guillaume-Gentil"]
|
9
|
-
s.date = %q{2008-12-
|
9
|
+
s.date = %q{2008-12-18}
|
10
10
|
s.description = %q{Add quick mass data import from CSV or Array to Active Record model}
|
11
11
|
s.email = %q{guillaumegentil@gmail.com}
|
12
|
-
s.extra_rdoc_files = ["lib/import_fu.rb", "README"]
|
13
|
-
s.files = ["init.rb", "lib/import_fu.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README", "test/
|
12
|
+
s.extra_rdoc_files = ["lib/import_fu.rb", "README.markdown"]
|
13
|
+
s.files = ["import_fu.gemspec", "init.rb", "lib/import_fu.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README.markdown", "test/database.yml", "test/fixtures/foos.csv", "test/import_fu_test.rb", "test/test_helper.rb"]
|
14
14
|
s.has_rdoc = true
|
15
15
|
s.homepage = %q{http://github.com/guillaumegentil/import_fu/tree}
|
16
16
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Import_fu", "--main", "README.markdown"]
|
@@ -25,14 +25,14 @@ Gem::Specification.new do |s|
|
|
25
25
|
s.specification_version = 2
|
26
26
|
|
27
27
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
28
|
-
s.add_development_dependency(%q<
|
28
|
+
s.add_development_dependency(%q<fastercsv>, [">= 0"])
|
29
29
|
s.add_development_dependency(%q<active_record>, [">= 0"])
|
30
30
|
else
|
31
|
-
s.add_dependency(%q<
|
31
|
+
s.add_dependency(%q<fastercsv>, [">= 0"])
|
32
32
|
s.add_dependency(%q<active_record>, [">= 0"])
|
33
33
|
end
|
34
34
|
else
|
35
|
-
s.add_dependency(%q<
|
35
|
+
s.add_dependency(%q<fastercsv>, [">= 0"])
|
36
36
|
s.add_dependency(%q<active_record>, [">= 0"])
|
37
37
|
end
|
38
38
|
end
|
data/lib/import_fu.rb
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
require 'fastercsv'
|
2
2
|
|
3
|
-
# Adds a import_with_load_data_infile class method.
|
4
|
-
# this lets you import data using mysql "LOAD DATA INFILE"
|
5
|
-
# This is about 30% faster than using ar-extensions bulk import
|
6
|
-
#
|
7
3
|
# be careful, there's no validation or escaping here!
|
8
4
|
module ImportFu
|
9
5
|
|
@@ -29,7 +25,7 @@ module ImportFu
|
|
29
25
|
load_data_infile_sql = build_load_data_infile_statement(csv_path, columns, options)
|
30
26
|
ActiveRecord::Base.connection.execute(load_data_infile_sql)
|
31
27
|
|
32
|
-
csv_tempfile.
|
28
|
+
csv_tempfile.delete if csv_tempfile
|
33
29
|
end
|
34
30
|
|
35
31
|
protected
|
@@ -58,6 +54,8 @@ module ImportFu
|
|
58
54
|
case value.class.to_s
|
59
55
|
when "Time", "Date"
|
60
56
|
value.to_s(:db)
|
57
|
+
when 'NilClass'
|
58
|
+
'NULL'
|
61
59
|
else
|
62
60
|
value.to_s
|
63
61
|
end
|
@@ -66,9 +64,10 @@ module ImportFu
|
|
66
64
|
def build_load_data_infile_statement(csv_path, columns, options = {})
|
67
65
|
local = options[:local] ? 'LOCAL' : ''
|
68
66
|
replace = options[:replace] ? 'REPLACE' : 'IGNORE'
|
67
|
+
charset = options[:charset] || 'UTF8'
|
69
68
|
column_list = columns.map(&:to_s).join(',')
|
70
69
|
|
71
|
-
"LOAD DATA #{local} INFILE '#{csv_path}' #{replace} INTO TABLE #{table_name} FIELDS TERMINATED BY ',' ENCLOSED BY '\"' (#{column_list});"
|
70
|
+
"LOAD DATA #{local} INFILE '#{csv_path}' #{replace} INTO TABLE #{table_name} CHARACTER SET #{charset} FIELDS TERMINATED BY ',' ENCLOSED BY '\"' (#{column_list});"
|
72
71
|
end
|
73
72
|
|
74
73
|
end
|
data/test/database.yml
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: guillaumegentil-import_fu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thibaud Guillaume-Gentil
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-12-
|
12
|
+
date: 2008-12-18 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
16
|
+
name: fastercsv
|
17
17
|
version_requirement:
|
18
18
|
version_requirements: !ruby/object:Gem::Requirement
|
19
19
|
requirements:
|
@@ -38,17 +38,19 @@ extensions: []
|
|
38
38
|
|
39
39
|
extra_rdoc_files:
|
40
40
|
- lib/import_fu.rb
|
41
|
-
- README
|
41
|
+
- README.markdown
|
42
42
|
files:
|
43
|
+
- import_fu.gemspec
|
43
44
|
- init.rb
|
44
45
|
- lib/import_fu.rb
|
45
46
|
- Manifest
|
46
47
|
- MIT-LICENSE
|
47
48
|
- Rakefile
|
48
|
-
- README
|
49
|
+
- README.markdown
|
50
|
+
- test/database.yml
|
51
|
+
- test/fixtures/foos.csv
|
49
52
|
- test/import_fu_test.rb
|
50
53
|
- test/test_helper.rb
|
51
|
-
- import_fu.gemspec
|
52
54
|
has_rdoc: true
|
53
55
|
homepage: http://github.com/guillaumegentil/import_fu/tree
|
54
56
|
post_install_message:
|