Module: SchemaPlus::Indexes::Middleware::Migration::Index

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

Instance Method Summary collapse

Instance Method Details

#around(env) ⇒ Object

Ignore duplicates

SchemaPlus::Indexes modifies SchemaStatements::add_index so that it ignores errors raised about add an index that already exists – i.e. that has the same index name, same columns, and same options – and writes a warning to the log. Some combinations of rails & DB adapter versions would log such a warning, others would raise an error; with SchemaPlus::Indexes all versions log the warning and do not raise the error.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/schema_plus/indexes/middleware/migration.rb', line 43

def around(env)
  yield env
rescue => e
  raise unless e.message.match(/["']([^"']+)["'].*already exists/)

  name = $1
  existing = env.caller.indexes(env.table_name).find{|i| i.name == name}
  attempted = ::ActiveRecord::ConnectionAdapters::IndexDefinition.new(env.table_name, env.column_names, env.options.merge(:name => name))
  raise if attempted != existing
  ::ActiveRecord::Base.logger.warn "[schema_plus_indexes] Index name #{name.inspect}' on table #{env.table_name.inspect} already exists. Skipping."
end

#before(env) ⇒ Object

Normalize Args



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

def before(env)
  [:length, :order].each do |key|
    env.options[key].stringify_keys! if env.options[key].is_a? Hash
  end
  env.column_names = Array.wrap(env.column_names).map(&:to_s) + Array.wrap(env.options.delete(:with)).map(&:to_s)
end