Sha256: b4e696c7d1443a1a5f792b411d57822e59c590a23d5dae3411d98d2c9d60ab88

Contents?: true

Size: 1.92 KB

Versions: 16

Compression:

Stored size: 1.92 KB

Contents

# Copyright (c) 2011 - 2013, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias
# Schmidt

module Lhm
  module SqlHelper
    extend self

    def annotation
      '/* large hadron migration */'
    end

    def idx_name(table_name, cols)
      column_names = column_definition(cols).map(&:first)
      "index_#{ table_name }_on_#{ column_names.join('_and_') }"
    end

    def idx_spec(cols)
      column_definition(cols).map do |name, length|
        "`#{ name }`#{ length }"
      end.join(', ')
    end

    def version_string
      row = connection.select_one("show variables like 'version'")
      value = struct_key(row, 'Value')
      row[value]
    end

    def tagged(statement)
      "#{ statement } #{ SqlHelper.annotation }"
    end

    private

    def column_definition(cols)
      Array(cols).map do |column|
        column.to_s.match(/`?([^\(]+)`?(\([^\)]+\))?/).captures
      end
    end

    # 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: http://dev.mysql.com/doc/refman/5.5/en/metadata-locking.html
    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

    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
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
lhm-teak-3.6.4 lib/lhm/sql_helper.rb
lhm-teak-3.6.3 lib/lhm/sql_helper.rb
lhm-teak-3.6.2 lib/lhm/sql_helper.rb
lhm-teak-3.6.1 lib/lhm/sql_helper.rb
lhm-teak-3.6.0 lib/lhm/sql_helper.rb
lhm-shopify-3.5.5 lib/lhm/sql_helper.rb
lhm-shopify-3.5.4 lib/lhm/sql_helper.rb
lhm-shopify-3.5.3 lib/lhm/sql_helper.rb
lhm-shopify-3.5.2 lib/lhm/sql_helper.rb
lhm-shopify-3.5.0 lib/lhm/sql_helper.rb
lhm-shopify-3.5.1 lib/lhm/sql_helper.rb
lhm-shopify-3.4.2 lib/lhm/sql_helper.rb
lhm-shopify-3.4.1 lib/lhm/sql_helper.rb
lhm-shopify-3.4.0 lib/lhm/sql_helper.rb
lhm-shopify-3.3.6 lib/lhm/sql_helper.rb
lhm-shopify-3.3.5 lib/lhm/sql_helper.rb