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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/schema_plus/indexes/middleware/migration.rb', line 56

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, name, env.options[:unique], env.column_names,
    **env.options.except(:unique)
  )
  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



41
42
43
44
45
46
# File 'lib/schema_plus/indexes/middleware/migration.rb', line 41

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