class RuboCop::Cop::Rails::AddColumnIndex
add_index :table, :column
add_column :table, :column, :integer
# good
add_column :table, :column, :integer, index: true
# bad (will not add an index)
@example
realizing it will not actually add an index.
error for extra keys, so it is possible to mistakenly add the key without
key. ‘add_column` does not accept `index`, but also does not raise an
Checks for migrations using `add_column` that have an `index`
def index_range(pair_node)
def index_range(pair_node) range_with_surrounding_comma(range_with_surrounding_space(pair_node.source_range, side: :left), :left) end
def on_send(node)
def on_send(node) table, column, pair, value = add_column_with_index(node) return unless pair add_offense(pair) do |corrector| corrector.remove(index_range(pair)) add_index = "add_index #{table.source}, #{column.source}" add_index_opts = '' if value.hash_type? hash = value.source_range.adjust(begin_pos: 1, end_pos: -1).source.strip add_index_opts = ", #{hash}" end corrector.insert_after(node, "\n#{add_index}#{add_index_opts}") end end