load_data_infile 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +26 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/load_data_infile.rb +113 -0
- data/rails/init.rb +1 -0
- data/spec/active_record_helper.rb +13 -0
- data/spec/fixtures/csv_with_headers.csv +2 -0
- data/spec/fixtures/csv_without_headers.csv +1 -0
- data/spec/load_data_infile_spec.rb +61 -0
- data/spec/spec_helper.rb +16 -0
- metadata +90 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Emmanuel Oga
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
= load_data_infile
|
2
|
+
|
3
|
+
This is a plugin for ActiveRecord.
|
4
|
+
|
5
|
+
Some chunks of code for this library were taken from activerecord-fast-import
|
6
|
+
(http://github.com/jsuchal/activerecord-fast-import)
|
7
|
+
|
8
|
+
It provides a mean of calling MySql's LOAD DATA INFILE clause for importing a csv file without having to proces it with ruby.
|
9
|
+
|
10
|
+
For details see MySql's manual
|
11
|
+
|
12
|
+
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
|
13
|
+
|
14
|
+
== Note on Patches/Pull Requests
|
15
|
+
|
16
|
+
* Fork the project.
|
17
|
+
* Make your feature addition or bug fix.
|
18
|
+
* Add tests for it. This is important so I don't break it in a
|
19
|
+
future version unintentionally.
|
20
|
+
* Commit, do not mess with rakefile, version, or history.
|
21
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
22
|
+
* Send me a pull request. Bonus points for topic branches.
|
23
|
+
|
24
|
+
== Copyright
|
25
|
+
|
26
|
+
Copyright (c) 2010 Emmanuel Oga. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "load_data_infile"
|
8
|
+
gem.summary = %Q{MySQL LOAD DATA INFILE support for ActiveRecord}
|
9
|
+
gem.description = %Q{MySQL LOAD DATA INFILE support for ActiveRecord}
|
10
|
+
gem.email = "EmmanuelOga@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/EmmanuelOga/load_data_infile"
|
12
|
+
gem.authors = ["Emmanuel Oga"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "load_data_infile #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
module LoadDataInfile
|
4
|
+
module MySql
|
5
|
+
|
6
|
+
# Deletes all rows in table very fast, but without calling +destroy+ method
|
7
|
+
# nor any hooks.
|
8
|
+
def truncate_table(table = quoted_table_name)
|
9
|
+
connection.execute("TRUNCATE TABLE #{table}")
|
10
|
+
end
|
11
|
+
|
12
|
+
# Disables key updates for model table
|
13
|
+
def disable_keys(table = quoted_table_name)
|
14
|
+
connection.execute("ALTER TABLE #{table} DISABLE KEYS")
|
15
|
+
end
|
16
|
+
|
17
|
+
# Enables key updates for model table
|
18
|
+
def enable_keys(table = quoted_table_name)
|
19
|
+
connection.execute("ALTER TABLE #{table} ENABLE KEYS")
|
20
|
+
end
|
21
|
+
|
22
|
+
# Disables keys, yields block, enables keys.
|
23
|
+
def with_keys_disabled(table = quoted_table_name)
|
24
|
+
disable_keys(table)
|
25
|
+
yield
|
26
|
+
ensure
|
27
|
+
enable_keys(table)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Load csv from a file using MySql's LOAD DATA INFILE
|
31
|
+
# For details see: http://dev.mysql.com/doc/refman/5.1/en/load-data.html
|
32
|
+
#
|
33
|
+
# Options:
|
34
|
+
#
|
35
|
+
# path :: CSV file path
|
36
|
+
#
|
37
|
+
# charset :: [OPTIONAL] Charset
|
38
|
+
# columns :: [OPTIONAL] Array of columns. Tries to use all columns if not provided. Use @dummy as column name to ignore a column. E.G.: (column_a, @column_b, @dummy)
|
39
|
+
# concurrent :: [OPTIONAL] True or false
|
40
|
+
# enclosed_by :: [OPTIONAL] Character
|
41
|
+
# escaped_by :: [OPTIONAL] Character
|
42
|
+
# ignore :: [OPTIONAL] Number, If provided, skips that number of lines.
|
43
|
+
# lines_starting_by :: [OPTIONAL] Character
|
44
|
+
# lines_terminated_by :: [OPTIONAL] Character
|
45
|
+
# local :: [OPTIONAL] true or fase. Defaults to true
|
46
|
+
# low_priority :: [OPTIONAL] true or false
|
47
|
+
# mappings :: [OPTIONAL] An array to map column values according to the mysql manual. E.G.: { :column_a => "TRIM(@column_b)"}
|
48
|
+
# on_duplicates :: [OPTIONAL] Action to perform when a duplicate row is found. Can be IGNORE or REPLACE
|
49
|
+
# optionally_enclosed_by :: [OPTIONAL] Character
|
50
|
+
# table :: [OPTIONAL] Table name. Defaults to quoted_table_name (won't work if used from an abstract class, e.g. ActiveRecord::Base')
|
51
|
+
# terminated_by :: [OPTIONAL] Character
|
52
|
+
def load_data_infile(options = {})
|
53
|
+
c = Context.new
|
54
|
+
|
55
|
+
if options[:low_priority]
|
56
|
+
c.low_priority_or_concurrent = :LOW_PRIORITY
|
57
|
+
elsif options[:concurrent]
|
58
|
+
c.low_priority_or_concurrent = :CONCURRENT
|
59
|
+
end
|
60
|
+
|
61
|
+
c.local = :LOCAL if !options.member?(:local) || options[:local]
|
62
|
+
|
63
|
+
c.file_name = quote_value options[:path]
|
64
|
+
|
65
|
+
c.replace_or_ignore = options[:on_duplicates] if options[:on_duplicates] # REPLACE or IGNORE
|
66
|
+
|
67
|
+
c.table_name = options[:table] ? "`#{ options[:table] }`" : quoted_table_name
|
68
|
+
|
69
|
+
c.charset = "CHARACTER SET #{quote_value options[:charset]}" if options[:charset]
|
70
|
+
|
71
|
+
if options[:terminated_by] || options[:enclosed_by] || options[:optionally_enclosed_by] || options[:escaped_by]
|
72
|
+
c.fields_definitions = " FIELDS " # or COLUMNS
|
73
|
+
c.fields_definitions << " TERMINATED BY #{ quote_value options[:terminated_by] } " if options[:terminated_by]
|
74
|
+
c.fields_definitions << " ENCLOSED BY #{ quote_value options[:enclosed_by] } " if options[:enclosed_by]
|
75
|
+
c.fields_definitions << " OPTIONALLY ENCLOSED BY #{ quote_value options[:optionally_enclosed_by] } " if options[:optionally_enclosed_by]
|
76
|
+
c.fields_definitions << " ESCAPED BY #{ quote_value options[:escaped_by] } " if options[:escaped_by]
|
77
|
+
end
|
78
|
+
|
79
|
+
if options[:lines_terminated_by] || options[:lines_starting_by]
|
80
|
+
c.lines_defitions = " LINES "
|
81
|
+
c.lines_defitions << " STARTING BY #{quote_value options[:lines_starting_by]} " if options[:lines_starting_by]
|
82
|
+
c.lines_defitions << " TERMINATED BY #{quote_value options[:lines_terminated_by]} " if options[:lines_terminated_by]
|
83
|
+
end
|
84
|
+
|
85
|
+
c.ignores = "IGNORE #{options[:ignore]} LINES" if options[:ignore]
|
86
|
+
|
87
|
+
c.columns = " (#{options[:columns].join(", ")}) " if options[:columns]
|
88
|
+
|
89
|
+
if options[:mappings] && options[:mappings].length > 0
|
90
|
+
s = options[:mappings].map{|column, mapping| "#{column} = #{mapping}" }.join(",")
|
91
|
+
c.mappings = "SET #{s}"
|
92
|
+
end
|
93
|
+
|
94
|
+
connection.execute(ERB.new(LOAD_DATA_INFILE_SQL).result(c.binding).gsub(/^\s*\n/, ""))
|
95
|
+
end
|
96
|
+
|
97
|
+
class Context < OpenStruct
|
98
|
+
public :binding
|
99
|
+
end
|
100
|
+
|
101
|
+
LOAD_DATA_INFILE_SQL = <<-SQL
|
102
|
+
LOAD DATA <%= low_priority_or_concurrent %> <%= local %> INFILE <%= file_name %>
|
103
|
+
<%= replace_or_ignore %>
|
104
|
+
INTO TABLE <%= table_name %>
|
105
|
+
<%= charset %>
|
106
|
+
<%= fields_definitions %>
|
107
|
+
<%= lines_defitions %>
|
108
|
+
<%= ignores %>
|
109
|
+
<%= columns %>
|
110
|
+
<%= mappings %> ;
|
111
|
+
SQL
|
112
|
+
end
|
113
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ActiveRecord::Base.extend LoadDataInfile::MySql
|
@@ -0,0 +1,13 @@
|
|
1
|
+
ActiveRecord::Base.establish_connection(:adapter => "mysql", :database => "load_data_infile_test", :user => "root", :password => "")
|
2
|
+
|
3
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
4
|
+
|
5
|
+
ActiveRecord::Schema.define do
|
6
|
+
create_table "things", :force => true do |t|
|
7
|
+
t.string :field_a, :field_b
|
8
|
+
t.integer :field_c
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Thing < ActiveRecord::Base
|
13
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
61,live,from,2400
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe LoadDataInfile do
|
4
|
+
before :each do
|
5
|
+
Thing.truncate_table
|
6
|
+
end
|
7
|
+
|
8
|
+
it "loads data from a csv file with headers into an ActiveRecord table" do
|
9
|
+
Thing.with_keys_disabled do
|
10
|
+
Thing.load_data_infile(
|
11
|
+
:path => FIXTURE_WITH_HEADERS,
|
12
|
+
:columns => %w|id field_a field_b field_c|,
|
13
|
+
:terminated_by => ",",
|
14
|
+
:ignore => 1
|
15
|
+
)
|
16
|
+
end
|
17
|
+
Thing.all.map(&:attributes).should == [{
|
18
|
+
"id" => 71,
|
19
|
+
"field_a" => "Hello",
|
20
|
+
"field_b" => "Brother",
|
21
|
+
"field_c" => 42
|
22
|
+
}]
|
23
|
+
end
|
24
|
+
|
25
|
+
it "loads data from a csv file without headers into an ActiveRecord table" do
|
26
|
+
Thing.with_keys_disabled do
|
27
|
+
Thing.load_data_infile(
|
28
|
+
:path => FIXTURE_WITHOUT_HEADERS,
|
29
|
+
:terminated_by => ",",
|
30
|
+
:columns => %w|id field_a field_b field_c|
|
31
|
+
)
|
32
|
+
end
|
33
|
+
Thing.all.map(&:attributes).should == [{
|
34
|
+
"id" => 61,
|
35
|
+
"field_a" => "live",
|
36
|
+
"field_b" => "from",
|
37
|
+
"field_c" => 2400
|
38
|
+
}]
|
39
|
+
end
|
40
|
+
|
41
|
+
it "loads data from a csv file with mapping" do
|
42
|
+
Thing.with_keys_disabled do
|
43
|
+
Thing.load_data_infile(
|
44
|
+
:path => FIXTURE_WITHOUT_HEADERS,
|
45
|
+
:terminated_by => ",",
|
46
|
+
:columns => %w|id @field_a @field_b @field_c|,
|
47
|
+
:mappings => {
|
48
|
+
:field_a => "CONCAT('So ', @field_a)",
|
49
|
+
:field_b => "CONCAT('Much ', @field_b)",
|
50
|
+
:field_c => "@field_c * 10",
|
51
|
+
}
|
52
|
+
)
|
53
|
+
end
|
54
|
+
Thing.all.map(&:attributes).should == [{
|
55
|
+
"id" => 61,
|
56
|
+
"field_a" => "So live",
|
57
|
+
"field_b" => "Much from",
|
58
|
+
"field_c" => 24000
|
59
|
+
}]
|
60
|
+
end
|
61
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
SPEC_PATH = File.dirname(__FILE__)
|
2
|
+
$LOAD_PATH.unshift(SPEC_PATH)
|
3
|
+
$LOAD_PATH.unshift(File.join(SPEC_PATH, '..', 'lib'))
|
4
|
+
require 'load_data_infile'
|
5
|
+
require 'spec'
|
6
|
+
require 'spec/autorun'
|
7
|
+
require 'rubygems'
|
8
|
+
require 'active_record'
|
9
|
+
require 'active_record_helper'
|
10
|
+
require File.join(SPEC_PATH, "..", "rails", "init.rb")
|
11
|
+
|
12
|
+
FIXTURE_WITH_HEADERS = File.join(SPEC_PATH, "fixtures", "csv_with_headers.csv")
|
13
|
+
FIXTURE_WITHOUT_HEADERS = File.join(SPEC_PATH, "fixtures", "csv_without_headers.csv")
|
14
|
+
|
15
|
+
Spec::Runner.configure do |config|
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: load_data_infile
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Emmanuel Oga
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-02-23 00:00:00 -03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: MySQL LOAD DATA INFILE support for ActiveRecord
|
35
|
+
email: EmmanuelOga@gmail.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- .document
|
45
|
+
- .gitignore
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- VERSION
|
50
|
+
- lib/load_data_infile.rb
|
51
|
+
- rails/init.rb
|
52
|
+
- spec/active_record_helper.rb
|
53
|
+
- spec/fixtures/csv_with_headers.csv
|
54
|
+
- spec/fixtures/csv_without_headers.csv
|
55
|
+
- spec/load_data_infile_spec.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/EmmanuelOga/load_data_infile
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --charset=UTF-8
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.6
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: MySQL LOAD DATA INFILE support for ActiveRecord
|
87
|
+
test_files:
|
88
|
+
- spec/load_data_infile_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
- spec/active_record_helper.rb
|