Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/activerecord-fast-import.rb
Class Method Summary collapse
-
.disable_keys ⇒ Object
Disables key updates for model table.
-
.enable_keys ⇒ Object
Enables key updates for model table.
-
.fast_import(files, options = {}) ⇒ Object
Loads data from file(s) using MySQL native LOAD DATA INFILE query, disabling key updates for even faster import speed.
-
.load_data_infile(file, options = {}) ⇒ Object
Loads data from file using MySQL native LOAD DATA INFILE query.
-
.load_data_infile_multiple(files, options = {}) ⇒ Object
Loads data from multiple files using MySQL native LOAD DATA INFILE query.
-
.truncate_table ⇒ Object
Deletes all rows in table very fast, but without calling
destroy
method nor any hooks. -
.with_keys_disabled ⇒ Object
Disables keys, yields block, enables keys.
Class Method Details
.disable_keys ⇒ Object
Disables key updates for model table
10 11 12 |
# File 'lib/activerecord-fast-import.rb', line 10 def self.disable_keys connection.execute("ALTER TABLE #{quoted_table_name} DISABLE KEYS") end |
.enable_keys ⇒ Object
Enables key updates for model table
15 16 17 |
# File 'lib/activerecord-fast-import.rb', line 15 def self.enable_keys connection.execute("ALTER TABLE #{quoted_table_name} ENABLE KEYS") end |
.fast_import(files, options = {}) ⇒ Object
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
(seeload_data_infile
)
32 33 34 35 36 37 |
# File 'lib/activerecord-fast-import.rb', line 32 def self.fast_import(files, = {}) files = [files] unless files.is_a? Array with_keys_disabled do load_data_infile_multiple(files, ) end end |
.load_data_infile(file, options = {}) ⇒ Object
Loads data from file using MySQL native LOAD DATA INFILE query
Parameters
-
file
the file to import -
options
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 |
# File 'lib/activerecord-fast-import.rb', line 51 def self.load_data_infile(file, = {}) sql = "LOAD DATA LOCAL INFILE '#{file}' " sql << "#{[:insert_method]} " if [:insert_method] sql << "INTO TABLE #{quoted_table_name} " sql << "CHARACTER SET #{[:charset_name]} " if [:charset_name] fields = "" fields << "TERMINATED BY #{structure_string [:fields_terminated_by]} " if [:fields_terminated_by] fields << "OPTIONALLY ENCLOSED BY '#{[:fields_optionally_enclosed_by]}' " if [:fields_optionally_enclosed_by] fields << "ESCAPED BY '#{[:fields_escaped_by]}' " if [:fields_escaped_by] sql << "FIELDS #{fields} " unless fields.empty? sql << "LINES TERMINATED BY #{structure_string [:lines_terminated_by]} " if [:lines_terminated_by] sql << "IGNORE #{[:ignore_lines]} LINES " if [:ignore_lines] sql << "(" + [:columns].join(', ') + ") " if [:columns] if [:mapping] mappings = [] [:mapping].each_pair do |column, mapping| mappings << "#{column} = #{mapping}" end sql << "SET #{mappings.join(', ')} " if mappings.size > 0 end sql << ";" connection.execute(sql) end |
.load_data_infile_multiple(files, options = {}) ⇒ Object
Loads data from multiple files using MySQL native LOAD DATA INFILE query
40 41 42 43 44 |
# File 'lib/activerecord-fast-import.rb', line 40 def self.load_data_infile_multiple(files, = {}) files.each do |file| load_data_infile(file, ) end end |
.truncate_table ⇒ Object
Deletes all rows in table very fast, but without calling destroy
method nor any hooks.
5 6 7 |
# File 'lib/activerecord-fast-import.rb', line 5 def self.truncate_table connection.execute("TRUNCATE TABLE #{quoted_table_name}") end |
.with_keys_disabled ⇒ Object
Disables keys, yields block, enables keys.
20 21 22 23 24 |
# File 'lib/activerecord-fast-import.rb', line 20 def self.with_keys_disabled disable_keys yield enable_keys end |