Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-fast-import.rb

Class Method Summary collapse

Class Method Details

.disable_keysObject

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_keysObject

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 (see load_data_infile)



32
33
34
35
36
37
# File 'lib/activerecord-fast-import.rb', line 32

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

.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, 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 #{structure_string 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 #{structure_string 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

.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, options = {})
  files.each do |file|
    load_data_infile(file, options)
  end
end

.truncate_tableObject

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_disabledObject

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