Class: PGTrunk::Operations::Indexes::AddIndex

Inherits:
PGTrunk::Operation show all
Defined in:
lib/pg_trunk/operations/indexes/add_index.rb

Overview

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.

Instance Method Summary collapse

Methods included from PGTrunk::Operation::SQLHelpers

#quote

Methods included from PGTrunk::Operation::RubyHelpers

#dump, #to_a, #to_opts

Methods included from PGTrunk::Operation::Inversion

#invert!, #irreversible!

Methods included from PGTrunk::Operation::Validations

#error_messages

Methods included from PGTrunk::Operation::Attributes

#attributes, #initialize

Instance Method Details

#<=>(other) ⇒ Object

Indexes are ordered by table and name



22
23
24
25
26
27
# 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_rubyObject

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 Also:

  • module (in `core/railtie`).


59
60
61
62
63
64
65
66
# 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