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)


13
14
15
16
# File 'lib/lhm/adapter.rb', line 13

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)


22
23
24
25
# File 'lib/lhm/adapter.rb', line 22

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)


39
40
41
42
# File 'lib/lhm/adapter.rb', line 39

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)


78
79
80
81
82
83
# File 'lib/lhm/adapter.rb', line 78

def add_unique_index(columns, index_name = nil)
  options = { unique: true }
  options.merge!(name: index_name) if index_name # rubocop:disable Performance/RedundantMerge

  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)


61
62
63
64
# File 'lib/lhm/adapter.rb', line 61

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)


30
31
32
# File 'lib/lhm/adapter.rb', line 30

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)


48
49
50
51
52
53
54
55
# File 'lib/lhm/adapter.rb', line 48

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)


70
71
72
# File 'lib/lhm/adapter.rb', line 70

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