Class: Lhm::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/lhm/adapter.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(migration, table_name) ⇒ Adapter

Constructor

Parameters:

  • migration (ActiveRecord::Migtration)
  • table_name (String, Symbol)


15
16
17
18
# File 'lib/lhm/adapter.rb', line 15

def initialize(migration, table_name)
  @migration = migration
  @table_name = table_name
end

Instance Method Details

#add_column(column_name, definition) ⇒ Object

Adds the specified column through ActiveRecord

Parameters:

  • column_name (String, Symbol)
  • definition (String, Symbol)


24
25
26
27
# 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) ⇒ Object

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

Parameters:

  • columns (Array<String>, Array<Symbol>, String, Symbol)
  • index_name (String) (defaults to: nil)


41
42
43
44
# 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) ⇒ Object

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

Parameters:

  • columns (Array<String>, Array<Symbol>, String, Symbol)
  • index_name (String) (defaults to: nil)


80
81
82
83
84
85
# 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) ⇒ Object

Change the column to use the provided definition, through ActiveRecord

Parameters:

  • column_name (String, Symbol)
  • definition (String, Symbol)


63
64
65
66
# 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) ⇒ Object

Removes the specified column through ActiveRecord

Parameters:

  • column_name (String, Symbol)


32
33
34
# 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) ⇒ Object

Removes the index in the given columns or by its name

Parameters:

  • columns (Array<String>, Array<Symbol>, String, Symbol)
  • index_name (String) (defaults to: nil)


50
51
52
53
54
55
56
57
# 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) ⇒ Object

Renames the old_name column to new_name by using ActiveRecord

Parameters:

  • old_name (String, Symbol)
  • new_name (String, Symbol)


72
73
74
# File 'lib/lhm/adapter.rb', line 72

def rename_column(old_name, new_name)
  migration.rename_column(table_name, old_name, new_name)
end