jsuchal-activerecord-fast-import 0.1.1 → 0.1.2
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/VERSION +1 -1
- data/activerecord-fast-import.gemspec +2 -2
- data/lib/activerecord-fast-import.rb +53 -52
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{activerecord-fast-import}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jan Suchal"]
|
12
|
-
s.date = %q{2009-08-
|
12
|
+
s.date = %q{2009-08-21}
|
13
13
|
s.description = %q{Native MySQL additions to ActiveRecord, like LOAD DATA INFILE, ENABLE/DISABLE KEYS, TRUNCATE TABLE.}
|
14
14
|
s.email = %q{johno@jsmf.net}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -1,53 +1,54 @@
|
|
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
|
-
# Loads data from file using MySQL native LOAD DATA INFILE query, disabling
|
20
|
-
# key updates for even faster import speed
|
21
|
-
#
|
22
|
-
# ==== Parameters
|
23
|
-
# * +file+
|
24
|
-
# * +options+ (see <tt>load_data_infile</tt>)
|
25
|
-
def self.fast_import(
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
# * +
|
36
|
-
|
37
|
-
|
38
|
-
sql
|
39
|
-
sql << "
|
40
|
-
sql << "
|
41
|
-
sql << "
|
42
|
-
if options[:
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
+
# Loads data from file using MySQL native LOAD DATA INFILE query, disabling
|
20
|
+
# key updates for even faster import speed
|
21
|
+
#
|
22
|
+
# ==== Parameters
|
23
|
+
# * +file+ file(s) to import
|
24
|
+
# * +options+ (see <tt>load_data_infile</tt>)
|
25
|
+
def self.fast_import(files, options = {})
|
26
|
+
files = [files] unless files.is_a? Array
|
27
|
+
enable_keys
|
28
|
+
files.each {|file| load_data_infile(file, options)}
|
29
|
+
disable_keys
|
30
|
+
end
|
31
|
+
|
32
|
+
# Loads data from file using MySQL native LOAD DATA INFILE query
|
33
|
+
#
|
34
|
+
# ==== Parameters
|
35
|
+
# * +file+ the file to import
|
36
|
+
# * +options+
|
37
|
+
def self.load_data_infile(file, options = {})
|
38
|
+
sql = "LOAD DATA LOCAL INFILE '#{file}' INTO TABLE #{quoted_table_name} "
|
39
|
+
sql << "FIELDS TERMINATED BY '#{options[:fields_terminated_by]}' " if options[:fields_terminated_by]
|
40
|
+
sql << "LINES TERMINATED BY '#{options[:lines_terminated_by]}' " if options[:lines_terminated_by]
|
41
|
+
sql << "IGNORE #{options[:ignore_lines]} LINES " if options[:ignore_lines]
|
42
|
+
sql << "(" + options[:columns].join(', ') + ") " if options[:columns]
|
43
|
+
if options[:mapping]
|
44
|
+
mappings = []
|
45
|
+
options[:mapping].each_pair do |column, mapping|
|
46
|
+
mappings << "#{column} = #{mapping}"
|
47
|
+
end
|
48
|
+
sql << "SET #{mappings.join(', ')} " if mappings.size > 0
|
49
|
+
end
|
50
|
+
sql << ";"
|
51
|
+
connection.execute(sql)
|
52
|
+
end
|
53
|
+
end
|
53
54
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsuchal-activerecord-fast-import
|
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
|
- Jan Suchal
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-21 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|