Module: SchemaPlus::Indexes::Middleware::Dumper::Table

Defined in:
lib/schema_plus/indexes/middleware/dumper.rb

Instance Method Summary collapse

Instance Method Details

#after(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/schema_plus/indexes/middleware/dumper.rb', line 6

def after(env)

  # move each column's index to its column, and remove them from the
  # list of indexes that AR would dump after the table.  Any left
  # over will still be dumped by AR.
  env.table.columns.each do |column|

    # first check for a single-column index
    if (index = env.table.indexes.find(&its.columns == [column.name]))
      column.options[:index] = index_options(env, column, index)
      env.table.indexes.delete(index)

    # then check for the first of a multi-column index
    elsif (index = env.table.indexes.find(&its.columns.first == column.name))
      column.options[:index] = index_options(env, column, index)
      env.table.indexes.delete(index)
    end

  end

end

#index_options(env, column, index) ⇒ Object



28
29
30
31
32
33
# File 'lib/schema_plus/indexes/middleware/dumper.rb', line 28

def index_options(env, column, index)
  options = {}
  options[:name] = index.name
  options[:with] = (index.columns - [column.name]) if index.columns.length > 1
  options.merge index.options
end