class PGTrunk::Operations::Indexes::AddIndex

@private

PGTrunk excludes indexes from table definitions provided by Rails. That’s why we have to fetch and dump indexes separately.

We fetch indexes from the database by their names and oids, and then rely on the original method +ActiveRecord::SchemaDumper#add_index+

We doesn’t overload the method ‘create_table`, but keep the original implementation unchanged. That’s why neither ‘to_sql`, `invert` or `generates_object` are necessary.

Public Instance Methods

<=>(other) click to toggle source

Indexes are ordered by table and name

Calls superclass method PGTrunk::Operation#<=>
# File lib/pg_trunk/operations/indexes/add_index.rb, line 22
def <=>(other)
  return unless other.is_a?(self.class)

  result = table <=> other.table
  result&.zero? ? super : result
end
to_ruby() click to toggle source

Instead of defining ruby_snippet, we overload the to_ruby to rely on the original implementation.

We overloaded the ActiveRecord::SchemaDumper method indexes_in_create so that it does nothing to exclude indexes from a table definition.

@see PGTrunk::SchemaDumper module (in ‘core/railtie`).

# File lib/pg_trunk/operations/indexes/add_index.rb, line 59
def to_ruby
  indexes = PGTrunk.database.send(:indexes, table.lean)
  index = indexes.find { |i| i.name == name.lean }
  return unless index

  line = PGTrunk.dumper.send(:index_parts, index).join(", ")
  "add_index #{table.lean.inspect}, #{line}"
end