Class: ActiveRecord::SchemaDumper

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/active_record/schema_dumper.rb

Overview

Patched version: 3.1.3

Patched methods
  • indexes

Instance Method Summary collapse

Instance Method Details

#indexes(table, stream) ⇒ Object

Writes out index-related details to the schema stream

Patch reason:

#indexes does not support writing out details related to partial indexes.

Patch:

Append :where clause if there’s a partial index



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/core_ext/active_record/schema_dumper.rb', line 15

def indexes(table, stream)
  if (indexes = @connection.indexes(table)).any?
    add_index_statements = indexes.map do |index|
      columns = index.columns.map do |column_name|
        column_name += ' ' +index.operators[column_name] if index.operators.key?(column_name)

        column_name
      end

      is_json_index = (columns.count == 1 && columns.first =~ /^(.+->.+)$/)

      statement_parts = [
        ('add_index ' + index.table.inspect),
        is_json_index ? "\"#{columns.first}\"" : columns.inspect,
        (':name => ' + index.name.inspect),
      ]
      statement_parts << ':unique => true' if index.unique

      index_lengths = (index.lengths || []).compact
      statement_parts << (':length => ' + Hash[index.columns.zip(index.lengths)].inspect) unless index_lengths.empty?

      # Patch
      #  Append :where clause if a partial index
      statement_parts << (':where => ' + index.where.inspect) if index.where

      statement_parts << (':using => ' + index.access_method.inspect) unless index.access_method.downcase == 'btree'

      statement_parts << ':skip_column_quoting => true' if is_json_index

      '  ' + statement_parts.join(', ')
    end

    stream.puts add_index_statements.sort.join("\n")
    stream.puts
  end
end