activerecord-fast-import 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -20
- data/README.markdown +75 -75
- data/Rakefile +33 -49
- data/VERSION +1 -1
- data/activerecord-fast-import.gemspec +51 -58
- data/lib/activerecord-fast-import.rb +86 -76
- data/nbproject/project.properties +6 -6
- data/nbproject/project.xml +15 -15
- data/spec/activerecord-fast-import_spec.rb +7 -7
- data/spec/spec_helper.rb +9 -9
- metadata +45 -52
- data/.gitignore +0 -7
data/LICENSE
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
Copyright (c) 2009 Jan Suchal
|
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.
|
1
|
+
Copyright (c) 2009 Jan Suchal
|
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.markdown
CHANGED
@@ -1,75 +1,75 @@
|
|
1
|
-
# activerecord-fast-import
|
2
|
-
|
3
|
-
Loads data from text files into tables using fast native MySQL [LOAD DATA INFILE](http://dev.mysql.com/doc/refman/5.1/en/load-data.html) query.
|
4
|
-
|
5
|
-
## Examples
|
6
|
-
|
7
|
-
### Loading data from tab delimited log file
|
8
|
-
|
9
|
-
Suppose you have an ActiveRecord model LogEntry defined as `LogEntry happened_at:datetime url:string` and a log file with tab delimited columns like this:
|
10
|
-
|
11
|
-
2009-09-30 12:32:43<tab>http://github.com/
|
12
|
-
2009-09-30 13:36:13<tab>http://facebook.com/
|
13
|
-
|
14
|
-
To import data from this log file, you have to use
|
15
|
-
|
16
|
-
LogEntry.fast_import('huge.log')
|
17
|
-
|
18
|
-
That's it!
|
19
|
-
|
20
|
-
Of course in real world you will also need more advanced features. Read on...
|
21
|
-
|
22
|
-
|
23
|
-
### Changing delimiters and ignoring some rows
|
24
|
-
|
25
|
-
Of course not all log files are delimited by tabs and newlines. Just pass custom delimiters to options. If you want to ignore first 10 lines, just use `:ignore_lines`
|
26
|
-
|
27
|
-
import_options = {
|
28
|
-
:fields_terminated_by => ',',
|
29
|
-
:lines_terminated_by => ';',
|
30
|
-
:ignore_lines => 10
|
31
|
-
}
|
32
|
-
|
33
|
-
### Changing order of columns and ignoring columns
|
34
|
-
|
35
|
-
Now, imagine you want to import data from a huge log file with following format:
|
36
|
-
|
37
|
-
http://github.com/<tab>Mozilla<tab>2009-09-30 12:32:43
|
38
|
-
http://facebook.com/<tab>Opera<tab>2009-09-30 13:36:13
|
39
|
-
|
40
|
-
It is clear that columns are in different order and we even want to ignore the second column. Let's do it
|
41
|
-
|
42
|
-
import_options = {:columns => ["url", "@dummy", "happened_at"]}
|
43
|
-
LogEntry.fast_import('huge.log', import_options)
|
44
|
-
|
45
|
-
The special `@dummy` loads that column into a local variable and when unused (in a transformation) is just ignored.
|
46
|
-
|
47
|
-
### Transforming data
|
48
|
-
|
49
|
-
Now imagine we have a log file like this:
|
50
|
-
|
51
|
-
2009-09-30 12:32:43<tab>http://github.com/<tab>image.jpg
|
52
|
-
2009-09-30 13:36:13<tab>http://facebook.com/<tab>styles/default.css
|
53
|
-
|
54
|
-
We want to concatenate those two columns into one.
|
55
|
-
|
56
|
-
import_options = {
|
57
|
-
:columns => ["happened_at", "@domain", "@file"],
|
58
|
-
:mapping => { :url => "CONCAT(@domain, @file)" }
|
59
|
-
}
|
60
|
-
LogEntry.fast_import('huge.log', import_options)
|
61
|
-
|
62
|
-
Of course you can use any of those shiny [MySQL functions](http://dev.mysql.com/doc/refman/5.1/en/functions.html).
|
63
|
-
|
64
|
-
### Extra features - added by Rafal Piekarski
|
65
|
-
|
66
|
-
This version also provides other "LOAD DATA" features and they are fields in options hash:
|
67
|
-
|
68
|
-
:insert_method # defines method for inserting data: IGNORE or REPLACE
|
69
|
-
:charset_name # defines file charset
|
70
|
-
:fields_optionally_enclosed_by # allows you to define fields enclosure method
|
71
|
-
:fields_escaped_by # changes the escape character
|
72
|
-
|
73
|
-
## Copyright
|
74
|
-
|
75
|
-
Copyright (c) 2009 Jan Suchal. See LICENSE for details.
|
1
|
+
# activerecord-fast-import
|
2
|
+
|
3
|
+
Loads data from text files into tables using fast native MySQL [LOAD DATA INFILE](http://dev.mysql.com/doc/refman/5.1/en/load-data.html) query.
|
4
|
+
|
5
|
+
## Examples
|
6
|
+
|
7
|
+
### Loading data from tab delimited log file
|
8
|
+
|
9
|
+
Suppose you have an ActiveRecord model LogEntry defined as `LogEntry happened_at:datetime url:string` and a log file with tab delimited columns like this:
|
10
|
+
|
11
|
+
2009-09-30 12:32:43<tab>http://github.com/
|
12
|
+
2009-09-30 13:36:13<tab>http://facebook.com/
|
13
|
+
|
14
|
+
To import data from this log file, you have to use
|
15
|
+
|
16
|
+
LogEntry.fast_import('huge.log')
|
17
|
+
|
18
|
+
That's it!
|
19
|
+
|
20
|
+
Of course in real world you will also need more advanced features. Read on...
|
21
|
+
|
22
|
+
|
23
|
+
### Changing delimiters and ignoring some rows
|
24
|
+
|
25
|
+
Of course not all log files are delimited by tabs and newlines. Just pass custom delimiters to options. If you want to ignore first 10 lines, just use `:ignore_lines`
|
26
|
+
|
27
|
+
import_options = {
|
28
|
+
:fields_terminated_by => ',',
|
29
|
+
:lines_terminated_by => ';',
|
30
|
+
:ignore_lines => 10
|
31
|
+
}
|
32
|
+
|
33
|
+
### Changing order of columns and ignoring columns
|
34
|
+
|
35
|
+
Now, imagine you want to import data from a huge log file with following format:
|
36
|
+
|
37
|
+
http://github.com/<tab>Mozilla<tab>2009-09-30 12:32:43
|
38
|
+
http://facebook.com/<tab>Opera<tab>2009-09-30 13:36:13
|
39
|
+
|
40
|
+
It is clear that columns are in different order and we even want to ignore the second column. Let's do it
|
41
|
+
|
42
|
+
import_options = {:columns => ["url", "@dummy", "happened_at"]}
|
43
|
+
LogEntry.fast_import('huge.log', import_options)
|
44
|
+
|
45
|
+
The special `@dummy` loads that column into a local variable and when unused (in a transformation) is just ignored.
|
46
|
+
|
47
|
+
### Transforming data
|
48
|
+
|
49
|
+
Now imagine we have a log file like this:
|
50
|
+
|
51
|
+
2009-09-30 12:32:43<tab>http://github.com/<tab>image.jpg
|
52
|
+
2009-09-30 13:36:13<tab>http://facebook.com/<tab>styles/default.css
|
53
|
+
|
54
|
+
We want to concatenate those two columns into one.
|
55
|
+
|
56
|
+
import_options = {
|
57
|
+
:columns => ["happened_at", "@domain", "@file"],
|
58
|
+
:mapping => { :url => "CONCAT(@domain, @file)" }
|
59
|
+
}
|
60
|
+
LogEntry.fast_import('huge.log', import_options)
|
61
|
+
|
62
|
+
Of course you can use any of those shiny [MySQL functions](http://dev.mysql.com/doc/refman/5.1/en/functions.html).
|
63
|
+
|
64
|
+
### Extra features - added by Rafal Piekarski
|
65
|
+
|
66
|
+
This version also provides other "LOAD DATA" features and they are fields in options hash:
|
67
|
+
|
68
|
+
:insert_method # defines method for inserting data: IGNORE or REPLACE
|
69
|
+
:charset_name # defines file charset
|
70
|
+
:fields_optionally_enclosed_by # allows you to define fields enclosure method
|
71
|
+
:fields_escaped_by # changes the escape character
|
72
|
+
|
73
|
+
## Copyright
|
74
|
+
|
75
|
+
Copyright (c) 2009 Jan Suchal. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,49 +1,33 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'rake'
|
3
|
-
|
4
|
-
begin
|
5
|
-
require 'jeweler'
|
6
|
-
Jeweler::Tasks.new do |gem|
|
7
|
-
gem.name = "activerecord-fast-import"
|
8
|
-
gem.summary = "Fast MySQL import for ActiveRecord"
|
9
|
-
gem.description = "Native MySQL additions to ActiveRecord, like LOAD DATA INFILE, ENABLE/DISABLE KEYS, TRUNCATE TABLE."
|
10
|
-
gem.email = "johno@jsmf.net"
|
11
|
-
gem.homepage = "http://github.com/jsuchal/activerecord-fast-import"
|
12
|
-
gem.authors = ["Jan Suchal"]
|
13
|
-
gem.add_development_dependency "rspec"
|
14
|
-
gem.add_dependency 'activerecord', [">= 2.1.2"]
|
15
|
-
end
|
16
|
-
Jeweler::GemcutterTasks.new
|
17
|
-
rescue LoadError
|
18
|
-
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
-
end
|
20
|
-
|
21
|
-
require '
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
task :default => :spec
|
36
|
-
|
37
|
-
require 'rake/rdoctask'
|
38
|
-
Rake::RDocTask.new do |rdoc|
|
39
|
-
if File.exist?('VERSION')
|
40
|
-
version = File.read('VERSION')
|
41
|
-
else
|
42
|
-
version = ""
|
43
|
-
end
|
44
|
-
|
45
|
-
rdoc.rdoc_dir = 'rdoc'
|
46
|
-
rdoc.title = "activerecord-fast-import #{version}"
|
47
|
-
rdoc.rdoc_files.include('README*')
|
48
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
-
end
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "activerecord-fast-import"
|
8
|
+
gem.summary = "Fast MySQL import for ActiveRecord"
|
9
|
+
gem.description = "Native MySQL additions to ActiveRecord, like LOAD DATA INFILE, ENABLE/DISABLE KEYS, TRUNCATE TABLE."
|
10
|
+
gem.email = "johno@jsmf.net"
|
11
|
+
gem.homepage = "http://github.com/jsuchal/activerecord-fast-import"
|
12
|
+
gem.authors = ["Jan Suchal"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
gem.add_dependency 'activerecord', [">= 2.1.2"]
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/rdoctask'
|
22
|
+
Rake::RDocTask.new do |rdoc|
|
23
|
+
if File.exist?('VERSION')
|
24
|
+
version = File.read('VERSION')
|
25
|
+
else
|
26
|
+
version = ""
|
27
|
+
end
|
28
|
+
|
29
|
+
rdoc.rdoc_dir = 'rdoc'
|
30
|
+
rdoc.title = "activerecord-fast-import #{version}"
|
31
|
+
rdoc.rdoc_files.include('README*')
|
32
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
33
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
@@ -1,58 +1,51 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.2.
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Jan Suchal"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE",
|
17
|
-
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
"
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
s.
|
33
|
-
s.
|
34
|
-
s.
|
35
|
-
|
36
|
-
s.
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
53
|
-
else
|
54
|
-
s.add_dependency(%q<rspec>, [">= 0"])
|
55
|
-
s.add_dependency(%q<activerecord>, [">= 2.1.2"])
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "activerecord-fast-import"
|
8
|
+
s.version = "0.2.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jan Suchal"]
|
12
|
+
s.date = "2012-01-04"
|
13
|
+
s.description = "Native MySQL additions to ActiveRecord, like LOAD DATA INFILE, ENABLE/DISABLE KEYS, TRUNCATE TABLE."
|
14
|
+
s.email = "johno@jsmf.net"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"LICENSE",
|
21
|
+
"README.markdown",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"activerecord-fast-import.gemspec",
|
25
|
+
"lib/activerecord-fast-import.rb",
|
26
|
+
"nbproject/project.properties",
|
27
|
+
"nbproject/project.xml",
|
28
|
+
"spec/activerecord-fast-import_spec.rb",
|
29
|
+
"spec/spec_helper.rb"
|
30
|
+
]
|
31
|
+
s.homepage = "http://github.com/jsuchal/activerecord-fast-import"
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = "1.8.10"
|
34
|
+
s.summary = "Fast MySQL import for ActiveRecord"
|
35
|
+
|
36
|
+
if s.respond_to? :specification_version then
|
37
|
+
s.specification_version = 3
|
38
|
+
|
39
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
40
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
41
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 2.1.2"])
|
42
|
+
else
|
43
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
44
|
+
s.add_dependency(%q<activerecord>, [">= 2.1.2"])
|
45
|
+
end
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
48
|
+
s.add_dependency(%q<activerecord>, [">= 2.1.2"])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
@@ -1,77 +1,87 @@
|
|
1
|
-
module ActiveRecord #:nodoc:
|
2
|
-
class Base
|
3
|
-
# Deletes all rows in table very fast, but without calling +destroy+ method
|
4
|
-
# nor any hooks.
|
5
|
-
def self.truncate_table
|
6
|
-
connection.execute("TRUNCATE TABLE #{quoted_table_name}")
|
7
|
-
end
|
8
|
-
|
9
|
-
# Disables key updates for model table
|
10
|
-
def self.disable_keys
|
11
|
-
connection.execute("ALTER TABLE #{quoted_table_name} DISABLE KEYS")
|
12
|
-
end
|
13
|
-
|
14
|
-
# Enables key updates for model table
|
15
|
-
def self.enable_keys
|
16
|
-
connection.execute("ALTER TABLE #{quoted_table_name} ENABLE KEYS")
|
17
|
-
end
|
18
|
-
|
19
|
-
# Disables keys, yields block, enables keys.
|
20
|
-
def self.with_keys_disabled
|
21
|
-
disable_keys
|
22
|
-
yield
|
23
|
-
enable_keys
|
24
|
-
end
|
25
|
-
|
26
|
-
# Loads data from file(s) using MySQL native LOAD DATA INFILE query, disabling
|
27
|
-
# key updates for even faster import speed
|
28
|
-
#
|
29
|
-
# ==== Parameters
|
30
|
-
# * +files+ file(s) to import
|
31
|
-
# * +options+ (see <tt>load_data_infile</tt>)
|
32
|
-
def self.fast_import(files, options = {})
|
33
|
-
files = [files] unless files.is_a? Array
|
34
|
-
with_keys_disabled do
|
35
|
-
load_data_infile_multiple(files, options)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
# Loads data from multiple files using MySQL native LOAD DATA INFILE query
|
40
|
-
def self.load_data_infile_multiple(files, options = {})
|
41
|
-
files.each do |file|
|
42
|
-
load_data_infile(file, options)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# Loads data from file using MySQL native LOAD DATA INFILE query
|
47
|
-
#
|
48
|
-
# ==== Parameters
|
49
|
-
# * +file+ the file to import
|
50
|
-
# * +options+
|
51
|
-
def self.load_data_infile(file, options = {})
|
52
|
-
sql = "LOAD DATA LOCAL INFILE '#{file}' "
|
53
|
-
sql << "#{options[:insert_method]} " if options[:insert_method]
|
54
|
-
sql << "INTO TABLE #{quoted_table_name} "
|
55
|
-
sql << "CHARACTER SET #{options[:charset_name]} " if options[:charset_name]
|
56
|
-
|
57
|
-
fields = ""
|
58
|
-
fields << "TERMINATED BY
|
59
|
-
fields << "OPTIONALLY ENCLOSED BY '#{options[:fields_optionally_enclosed_by]}' " if options[:fields_optionally_enclosed_by]
|
60
|
-
fields << "ESCAPED BY '#{options[:fields_escaped_by]}' " if options[:fields_escaped_by]
|
61
|
-
|
62
|
-
sql << "FIELDS #{fields} " unless fields.empty?
|
63
|
-
sql << "LINES TERMINATED BY
|
64
|
-
sql << "IGNORE #{options[:ignore_lines]} LINES " if options[:ignore_lines]
|
65
|
-
sql << "(" + options[:columns].join(', ') + ") " if options[:columns]
|
66
|
-
if options[:mapping]
|
67
|
-
mappings = []
|
68
|
-
options[:mapping].each_pair do |column, mapping|
|
69
|
-
mappings << "#{column} = #{mapping}"
|
70
|
-
end
|
71
|
-
sql << "SET #{mappings.join(', ')} " if mappings.size > 0
|
72
|
-
end
|
73
|
-
sql << ";"
|
74
|
-
connection.execute(sql)
|
75
|
-
end
|
76
|
-
|
1
|
+
module ActiveRecord #:nodoc:
|
2
|
+
class Base
|
3
|
+
# Deletes all rows in table very fast, but without calling +destroy+ method
|
4
|
+
# nor any hooks.
|
5
|
+
def self.truncate_table
|
6
|
+
connection.execute("TRUNCATE TABLE #{quoted_table_name}")
|
7
|
+
end
|
8
|
+
|
9
|
+
# Disables key updates for model table
|
10
|
+
def self.disable_keys
|
11
|
+
connection.execute("ALTER TABLE #{quoted_table_name} DISABLE KEYS")
|
12
|
+
end
|
13
|
+
|
14
|
+
# Enables key updates for model table
|
15
|
+
def self.enable_keys
|
16
|
+
connection.execute("ALTER TABLE #{quoted_table_name} ENABLE KEYS")
|
17
|
+
end
|
18
|
+
|
19
|
+
# Disables keys, yields block, enables keys.
|
20
|
+
def self.with_keys_disabled
|
21
|
+
disable_keys
|
22
|
+
yield
|
23
|
+
enable_keys
|
24
|
+
end
|
25
|
+
|
26
|
+
# Loads data from file(s) using MySQL native LOAD DATA INFILE query, disabling
|
27
|
+
# key updates for even faster import speed
|
28
|
+
#
|
29
|
+
# ==== Parameters
|
30
|
+
# * +files+ file(s) to import
|
31
|
+
# * +options+ (see <tt>load_data_infile</tt>)
|
32
|
+
def self.fast_import(files, options = {})
|
33
|
+
files = [files] unless files.is_a? Array
|
34
|
+
with_keys_disabled do
|
35
|
+
load_data_infile_multiple(files, options)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Loads data from multiple files using MySQL native LOAD DATA INFILE query
|
40
|
+
def self.load_data_infile_multiple(files, options = {})
|
41
|
+
files.each do |file|
|
42
|
+
load_data_infile(file, options)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Loads data from file using MySQL native LOAD DATA INFILE query
|
47
|
+
#
|
48
|
+
# ==== Parameters
|
49
|
+
# * +file+ the file to import
|
50
|
+
# * +options+
|
51
|
+
def self.load_data_infile(file, options = {})
|
52
|
+
sql = "LOAD DATA LOCAL INFILE '#{file}' "
|
53
|
+
sql << "#{options[:insert_method]} " if options[:insert_method]
|
54
|
+
sql << "INTO TABLE #{quoted_table_name} "
|
55
|
+
sql << "CHARACTER SET #{options[:charset_name]} " if options[:charset_name]
|
56
|
+
|
57
|
+
fields = ""
|
58
|
+
fields << "TERMINATED BY #{structure_string options[:fields_terminated_by]} " if options[:fields_terminated_by]
|
59
|
+
fields << "OPTIONALLY ENCLOSED BY '#{options[:fields_optionally_enclosed_by]}' " if options[:fields_optionally_enclosed_by]
|
60
|
+
fields << "ESCAPED BY '#{options[:fields_escaped_by]}' " if options[:fields_escaped_by]
|
61
|
+
|
62
|
+
sql << "FIELDS #{fields} " unless fields.empty?
|
63
|
+
sql << "LINES TERMINATED BY #{structure_string options[:lines_terminated_by]} " if options[:lines_terminated_by]
|
64
|
+
sql << "IGNORE #{options[:ignore_lines]} LINES " if options[:ignore_lines]
|
65
|
+
sql << "(" + options[:columns].join(', ') + ") " if options[:columns]
|
66
|
+
if options[:mapping]
|
67
|
+
mappings = []
|
68
|
+
options[:mapping].each_pair do |column, mapping|
|
69
|
+
mappings << "#{column} = #{mapping}"
|
70
|
+
end
|
71
|
+
sql << "SET #{mappings.join(', ')} " if mappings.size > 0
|
72
|
+
end
|
73
|
+
sql << ";"
|
74
|
+
connection.execute(sql)
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def self.structure_string(spec)
|
80
|
+
if spec.is_a? Fixnum
|
81
|
+
"0x%x" % spec
|
82
|
+
else
|
83
|
+
"'#{spec}'"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
77
87
|
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
javac.classpath=
|
2
|
-
main.file=
|
3
|
-
platform.active=Ruby
|
4
|
-
source.encoding=UTF-8
|
5
|
-
src.lib.dir=lib
|
6
|
-
test.spec.dir=spec
|
1
|
+
javac.classpath=
|
2
|
+
main.file=
|
3
|
+
platform.active=Ruby
|
4
|
+
source.encoding=UTF-8
|
5
|
+
src.lib.dir=lib
|
6
|
+
test.spec.dir=spec
|
data/nbproject/project.xml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
-
<project xmlns="http://www.netbeans.org/ns/project/1">
|
3
|
-
<type>org.netbeans.modules.ruby.rubyproject</type>
|
4
|
-
<configuration>
|
5
|
-
<data xmlns="http://www.netbeans.org/ns/ruby-project/1">
|
6
|
-
<name>activerecord-fast-import</name>
|
7
|
-
<source-roots>
|
8
|
-
<root id="src.lib.dir"/>
|
9
|
-
</source-roots>
|
10
|
-
<test-roots>
|
11
|
-
<root id="test.spec.dir"/>
|
12
|
-
</test-roots>
|
13
|
-
</data>
|
14
|
-
</configuration>
|
15
|
-
</project>
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project xmlns="http://www.netbeans.org/ns/project/1">
|
3
|
+
<type>org.netbeans.modules.ruby.rubyproject</type>
|
4
|
+
<configuration>
|
5
|
+
<data xmlns="http://www.netbeans.org/ns/ruby-project/1">
|
6
|
+
<name>activerecord-fast-import</name>
|
7
|
+
<source-roots>
|
8
|
+
<root id="src.lib.dir"/>
|
9
|
+
</source-roots>
|
10
|
+
<test-roots>
|
11
|
+
<root id="test.spec.dir"/>
|
12
|
+
</test-roots>
|
13
|
+
</data>
|
14
|
+
</configuration>
|
15
|
+
</project>
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
describe "ActiverecordFastImport" do
|
4
|
-
it "fails" do
|
5
|
-
fail "hey buddy, you should probably rename this file and start specing for real"
|
6
|
-
end
|
7
|
-
end
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "ActiverecordFastImport" do
|
4
|
+
it "fails" do
|
5
|
+
fail "hey buddy, you should probably rename this file and start specing for real"
|
6
|
+
end
|
7
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
-
require 'activerecord-fast-import'
|
4
|
-
require 'spec'
|
5
|
-
require 'spec/autorun'
|
6
|
-
|
7
|
-
Spec::Runner.configure do |config|
|
8
|
-
|
9
|
-
end
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'activerecord-fast-import'
|
4
|
+
require 'spec'
|
5
|
+
require 'spec/autorun'
|
6
|
+
|
7
|
+
Spec::Runner.configure do |config|
|
8
|
+
|
9
|
+
end
|
metadata
CHANGED
@@ -1,48 +1,47 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-fast-import
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
|
-
authors:
|
7
|
+
authors:
|
7
8
|
- Jan Suchal
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-01-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
16
15
|
name: rspec
|
16
|
+
requirement: &18209120 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
17
22
|
type: :development
|
18
|
-
|
19
|
-
version_requirements:
|
20
|
-
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: "0"
|
24
|
-
version:
|
25
|
-
- !ruby/object:Gem::Dependency
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *18209120
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
26
|
name: activerecord
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
- !ruby/object:Gem::Version
|
27
|
+
requirement: &18288400 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
33
32
|
version: 2.1.2
|
34
|
-
|
35
|
-
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *18288400
|
36
|
+
description: Native MySQL additions to ActiveRecord, like LOAD DATA INFILE, ENABLE/DISABLE
|
37
|
+
KEYS, TRUNCATE TABLE.
|
36
38
|
email: johno@jsmf.net
|
37
39
|
executables: []
|
38
|
-
|
39
40
|
extensions: []
|
40
|
-
|
41
|
-
extra_rdoc_files:
|
41
|
+
extra_rdoc_files:
|
42
42
|
- LICENSE
|
43
43
|
- README.markdown
|
44
|
-
files:
|
45
|
-
- .gitignore
|
44
|
+
files:
|
46
45
|
- LICENSE
|
47
46
|
- README.markdown
|
48
47
|
- Rakefile
|
@@ -53,34 +52,28 @@ files:
|
|
53
52
|
- nbproject/project.xml
|
54
53
|
- spec/activerecord-fast-import_spec.rb
|
55
54
|
- spec/spec_helper.rb
|
56
|
-
has_rdoc: true
|
57
55
|
homepage: http://github.com/jsuchal/activerecord-fast-import
|
58
56
|
licenses: []
|
59
|
-
|
60
57
|
post_install_message:
|
61
|
-
rdoc_options:
|
62
|
-
|
63
|
-
require_paths:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
64
60
|
- lib
|
65
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
77
73
|
requirements: []
|
78
|
-
|
79
74
|
rubyforge_project:
|
80
|
-
rubygems_version: 1.
|
75
|
+
rubygems_version: 1.8.10
|
81
76
|
signing_key:
|
82
77
|
specification_version: 3
|
83
78
|
summary: Fast MySQL import for ActiveRecord
|
84
|
-
test_files:
|
85
|
-
- spec/activerecord-fast-import_spec.rb
|
86
|
-
- spec/spec_helper.rb
|
79
|
+
test_files: []
|