module Lhm::SqlHelper

Public Instance Methods

annotation() click to toggle source
# File lib/lhm/sql_helper.rb, line 8
def annotation
  '/* large hadron migration */'
end
idx_name(table_name, cols) click to toggle source
# File lib/lhm/sql_helper.rb, line 12
def idx_name(table_name, cols)
  column_names = column_definition(cols).map(&:first)
  "index_#{ table_name }_on_#{ column_names.join('_and_') }"
end
idx_spec(cols) click to toggle source
# File lib/lhm/sql_helper.rb, line 17
def idx_spec(cols)
  column_definition(cols).map do |name, length|
    "`#{ name }`#{ length }"
  end.join(', ')
end
tagged(statement) click to toggle source
# File lib/lhm/sql_helper.rb, line 29
def tagged(statement)
  "#{ statement } #{ SqlHelper.annotation }"
end
version_string() click to toggle source
# File lib/lhm/sql_helper.rb, line 23
def version_string
  row = connection.select_one("show variables like 'version'")
  value = struct_key(row, 'Value')
  row[value]
end

Private Instance Methods

column_definition(cols) click to toggle source
# File lib/lhm/sql_helper.rb, line 35
def column_definition(cols)
  Array(cols).map do |column|
    column.to_s.match(/`?([^\(]+)`?(\([^\)]+\))?/).captures
  end
end
struct_key(struct, key) click to toggle source
# File lib/lhm/sql_helper.rb, line 67
def struct_key(struct, key)
  keys = if struct.is_a? Hash
           struct.keys
         else
           struct.members
         end

  keys.find { |k| k.to_s.downcase == key.to_s.downcase }
end
supports_atomic_switch?() click to toggle source

Older versions of MySQL contain an atomic rename bug affecting bin log order. Affected versions extracted from bug report:

http://bugs.mysql.com/bug.php?id=39675

More Info: dev.mysql.com/doc/refman/5.5/en/metadata-locking.html

# File lib/lhm/sql_helper.rb, line 47
def supports_atomic_switch?
  major, minor, tiny = version_string.split('.').map(&:to_i)

  case major
  when 4 then return false if minor and minor < 2
  when 5
    case minor
    when 0 then return false if tiny and tiny < 52
    when 1 then return false
    when 4 then return false if tiny and tiny < 4
    when 5 then return false if tiny and tiny < 3
    end
  when 6
    case minor
    when 0 then return false if tiny and tiny < 11
    end
  end
  true
end