Class: PGTrunk::Operations::Indexes::AddIndex
- Inherits:
-
PGTrunk::Operation
- Object
- PGTrunk::Operation
- PGTrunk::Operations::Indexes::AddIndex
- 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
-
#<=>(other) ⇒ Object
Indexes are ordered by table and name.
-
#to_ruby ⇒ Object
Instead of defining +ruby_snippet+, we overload the +to_ruby+ to rely on the original implementation.
Methods included from PGTrunk::Operation::SQLHelpers
Methods included from PGTrunk::Operation::RubyHelpers
Methods included from PGTrunk::Operation::Inversion
Methods included from PGTrunk::Operation::Validations
Methods included from PGTrunk::Operation::Attributes
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_ruby ⇒ Object
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.
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 |