class Lhm::Adapter

Translates Lhm DSL to ActiveRecord's one, so Lhm migrations will now go through Percona as well, without any modification on the migration's code

Attributes

migration[R]
table_name[R]

Public Class Methods

new(migration, table_name) click to toggle source

Constructor

@param migration [ActiveRecord::Migtration] @param table_name [String, Symbol]

# File lib/lhm/adapter.rb, line 15
def initialize(migration, table_name)
  @migration = migration
  @table_name = table_name
end

Public Instance Methods

add_column(column_name, definition) click to toggle source

Adds the specified column through ActiveRecord

@param column_name [String, Symbol] @param definition [String, Symbol]

# File lib/lhm/adapter.rb, line 24
def add_column(column_name, definition)
  attributes = column_attributes(column_name, definition)
  migration.add_column(*attributes)
end
add_index(columns, index_name = nil) click to toggle source

Adds an index in the specified columns through ActiveRecord. Note you can provide a name as well

@param columns [Array<String>, Array<Symbol>, String, Symbol] @param index_name [String]

# File lib/lhm/adapter.rb, line 41
def add_index(columns, index_name = nil)
  options = { name: index_name } if index_name
  migration.add_index(table_name, columns, options || {})
end
add_unique_index(columns, index_name = nil) click to toggle source

Adds a unique index on the given columns, with the provided name if passed

@param columns [Array<String>, Array<Symbol>, String, Symbol] @param index_name [String]

# File lib/lhm/adapter.rb, line 80
def add_unique_index(columns, index_name = nil)
  options = { unique: true }
  options.merge!(name: index_name) if index_name

  migration.add_index(table_name, columns, options)
end
change_column(column_name, definition) click to toggle source

Change the column to use the provided definition, through ActiveRecord

@param column_name [String, Symbol] @param definition [String, Symbol]

# File lib/lhm/adapter.rb, line 63
def change_column(column_name, definition)
  attributes = column_attributes(column_name, definition)
  migration.change_column(*attributes)
end
remove_column(column_name) click to toggle source

Removes the specified column through ActiveRecord

@param column_name [String, Symbol]

# File lib/lhm/adapter.rb, line 32
def remove_column(column_name)
  migration.remove_column(table_name, column_name)
end
remove_index(columns, index_name = nil) click to toggle source

Removes the index in the given columns or by its name

@param columns [Array<String>, Array<Symbol>, String, Symbol] @param index_name [String]

# File lib/lhm/adapter.rb, line 50
def remove_index(columns, index_name = nil)
  options = if index_name
              { name: index_name }
            else
              { column: columns }
            end
  migration.remove_index(table_name, options)
end
rename_column(old_name, new_name) click to toggle source

Renames the old_name column to new_name by using ActiveRecord

@param old_name [String, Symbol] @param new_name [String, Symbol]

# File lib/lhm/adapter.rb, line 72
def rename_column(old_name, new_name)
  migration.rename_column(table_name, old_name, new_name)
end

Private Instance Methods

column(name, definition) click to toggle source

Returns the instance of ActiveRecord column with the given name and definition

@param name [String, Symbol] @param definition [String]

# File lib/lhm/adapter.rb, line 96
def column(name, definition)
  @column ||= if definition.is_a?(Symbol)
                ColumnWithType.new(name, definition)
              else
                ColumnWithSql.new(name, definition)
              end
end
column_attributes(name, definition) click to toggle source
# File lib/lhm/adapter.rb, line 104
def column_attributes(name, definition)
  attributes = column(name, definition).attributes
  [table_name, name].concat(attributes)
end