Module: Sunrise::Utils::Mysql::ClassMethods

Defined in:
lib/sunrise/utils/mysql.rb

Instance Method Summary collapse

Instance Method Details

#database_exists?Boolean

Check if database exists

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sunrise/utils/mysql.rb', line 17

def database_exists?
 _options = configurations[Rails.env].dup
 _options.symbolize_keys!

  begin
    connection
    return true
  rescue Exception => e
    return false
  end
end

#disable_keysObject

Disables key updates for model table



36
37
38
# File 'lib/sunrise/utils/mysql.rb', line 36

def disable_keys
  connection.execute("ALTER TABLE #{quoted_table_name} DISABLE KEYS")
end

#enable_keysObject

Enables key updates for model table



41
42
43
# File 'lib/sunrise/utils/mysql.rb', line 41

def 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)



58
59
60
61
62
63
# File 'lib/sunrise/utils/mysql.rb', line 58

def 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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sunrise/utils/mysql.rb', line 77

def 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

#load_data_infile_multiple(files, options = {}) ⇒ Object

Loads data from multiple files using MySQL native LOAD DATA INFILE query



66
67
68
69
70
# File 'lib/sunrise/utils/mysql.rb', line 66

def load_data_infile_multiple(files, options = {})
  files.each do |file|
    load_data_infile(file, options)
  end
end

#truncate_tableObject

nor any hooks.



31
32
33
# File 'lib/sunrise/utils/mysql.rb', line 31

def truncate_table
 transaction { connection.execute("TRUNCATE TABLE #{quoted_table_name};") }
end

#values(column = 'id') ⇒ Object



11
12
13
14
# File 'lib/sunrise/utils/mysql.rb', line 11

def values(column = 'id')
  query = scoped.select(column)
  connection.select_values(query.to_sql).map(&:to_i).uniq
end

#with_keys_disabledObject

Disables keys, yields block, enables keys.



46
47
48
49
50
# File 'lib/sunrise/utils/mysql.rb', line 46

def with_keys_disabled
  disable_keys
  yield
  enable_keys
end