Module: Freeberry::MysqlUtils::ClassMethods
- Defined in:
- lib/freeberry/mysql_utils.rb
Instance Method Summary collapse
-
#database_exists? ⇒ Boolean
Check if database exists.
-
#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
nor any hooks.
- #values(column = 'id') ⇒ Object
-
#with_keys_disabled ⇒ Object
Disables keys, yields block, enables keys.
Instance Method Details
#database_exists? ⇒ Boolean
Check if database exists
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/freeberry/mysql_utils.rb', line 16 def database_exists? = configurations[Rails.env].dup .symbolize_keys! begin connection return true rescue Exception => e return false end end |
#disable_keys ⇒ Object
Disables key updates for model table
35 36 37 |
# File 'lib/freeberry/mysql_utils.rb', line 35 def disable_keys connection.execute("ALTER TABLE #{quoted_table_name} DISABLE KEYS") end |
#enable_keys ⇒ Object
Enables key updates for model table
40 41 42 |
# File 'lib/freeberry/mysql_utils.rb', line 40 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
(seeload_data_infile
)
57 58 59 60 61 62 |
# File 'lib/freeberry/mysql_utils.rb', line 57 def 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
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/freeberry/mysql_utils.rb', line 76 def 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 '#{[: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 '#{[: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
65 66 67 68 69 |
# File 'lib/freeberry/mysql_utils.rb', line 65 def load_data_infile_multiple(files, = {}) files.each do |file| load_data_infile(file, ) end end |
#truncate_table ⇒ Object
nor any hooks.
30 31 32 |
# File 'lib/freeberry/mysql_utils.rb', line 30 def truncate_table transaction { connection.execute("TRUNCATE TABLE #{quoted_table_name};") } end |
#values(column = 'id') ⇒ Object
10 11 12 13 |
# File 'lib/freeberry/mysql_utils.rb', line 10 def values(column = 'id') query = scoped.select(column) connection.select_values(query.to_sql).map(&:to_i).uniq end |
#with_keys_disabled ⇒ Object
Disables keys, yields block, enables keys.
45 46 47 48 49 |
# File 'lib/freeberry/mysql_utils.rb', line 45 def with_keys_disabled disable_keys yield enable_keys end |