Class: Lhm::Adapter
- Inherits:
-
Object
- Object
- Lhm::Adapter
- 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
-
#add_column(column_name, definition) ⇒ Object
Adds the specified column through ActiveRecord.
-
#add_index(columns, index_name = nil) ⇒ Object
Adds an index in the specified columns through ActiveRecord.
-
#add_unique_index(columns, index_name = nil) ⇒ Object
Adds a unique index on the given columns, with the provided name if passed.
-
#change_column(column_name, definition) ⇒ Object
Change the column to use the provided definition, through ActiveRecord.
-
#initialize(migration, table_name) ⇒ Adapter
constructor
Constructor.
-
#remove_column(column_name) ⇒ Object
Removes the specified column through ActiveRecord.
-
#remove_index(columns, index_name = nil) ⇒ Object
Removes the index in the given columns or by its name.
-
#rename_column(old_name, new_name) ⇒ Object
Renames the old_name column to new_name by using ActiveRecord.
Constructor Details
#initialize(migration, table_name) ⇒ Adapter
Constructor
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
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
39 40 41 42 |
# File 'lib/lhm/adapter.rb', line 39 def add_index(columns, index_name = nil) = { name: index_name } if index_name migration.add_index(table_name, columns, || {}) end |
#add_unique_index(columns, index_name = nil) ⇒ Object
Adds a unique index on the given columns, with the provided name if passed
78 79 80 81 82 83 |
# File 'lib/lhm/adapter.rb', line 78 def add_unique_index(columns, index_name = nil) = { unique: true } .merge!(name: index_name) if index_name # rubocop:disable Performance/RedundantMerge migration.add_index(table_name, columns, ) end |
#change_column(column_name, definition) ⇒ Object
Change the column to use the provided definition, through ActiveRecord
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
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
48 49 50 51 52 53 54 55 |
# File 'lib/lhm/adapter.rb', line 48 def remove_index(columns, index_name = nil) = if index_name { name: index_name } else { column: columns } end migration.remove_index(table_name, ) end |
#rename_column(old_name, new_name) ⇒ Object
Renames the old_name column to new_name by using ActiveRecord
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 |