-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactiverecord-fast-import.rb
77 lines (69 loc) · 2.7 KB
/
activerecord-fast-import.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module ActiveRecord #:nodoc:
class Base
# Deletes all rows in table very fast, but without calling +destroy+ method
# nor any hooks.
def self.truncate_table
connection.execute("TRUNCATE TABLE #{quoted_table_name}")
end
# Disables key updates for model table
def self.disable_keys
connection.execute("ALTER TABLE #{quoted_table_name} DISABLE KEYS")
end
# Enables key updates for model table
def self.enable_keys
connection.execute("ALTER TABLE #{quoted_table_name} ENABLE KEYS")
end
# Disables keys, yields block, enables keys.
def self.with_keys_disabled
disable_keys
yield
enable_keys
end
# Loads data from file(s) using MySQL native LOAD DATA INFILE query, disabling
# key updates for even faster import speed
#
# ==== Parameters
# * +files+ file(s) to import
# * +options+ (see <tt>load_data_infile</tt>)
def self.fast_import(files, options = {})
files = [files] unless files.is_a? Array
with_keys_disabled do
load_data_infile_multiple(files, options)
end
end
# Loads data from multiple files using MySQL native LOAD DATA INFILE query
def self.load_data_infile_multiple(files, options = {})
files.each do |file|
load_data_infile(file, options)
end
end
# Loads data from file using MySQL native LOAD DATA INFILE query
#
# ==== Parameters
# * +file+ the file to import
# * +options+
def self.load_data_infile(file, options = {})
sql = "LOAD DATA LOCAL INFILE '#{file}' "
sql << "#{options[:insert_method]} " if options[:insert_method]
sql << "INTO TABLE #{quoted_table_name} "
sql << "CHARACTER SET #{options[:charset_name]} " if options[:charset_name]
fields = ""
fields << "TERMINATED BY '#{options[:fields_terminated_by]}' " if options[:fields_terminated_by]
fields << "OPTIONALLY ENCLOSED BY '#{options[:fields_optionally_enclosed_by]}' " if options[:fields_optionally_enclosed_by]
fields << "ESCAPED BY '#{options[:fields_escaped_by]}' " if options[:fields_escaped_by]
sql << "FIELDS #{fields} " unless fields.empty?
sql << "LINES TERMINATED BY '#{options[:lines_terminated_by]}' " if options[:lines_terminated_by]
sql << "IGNORE #{options[:ignore_lines]} LINES " if options[:ignore_lines]
sql << "(" + options[:columns].join(', ') + ") " if options[:columns]
if options[:mapping]
mappings = []
options[:mapping].each_pair do |column, mapping|
mappings << "#{column} = #{mapping}"
end
sql << "SET #{mappings.join(', ')} " if mappings.size > 0
end
sql << ";"
connection.execute(sql)
end
end
end