Sha256: 10bb695ae555475e53af21c4700b775a3682642a1e179753ce20ce07fa20abf2

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

module SchemaPlusPgIndexes
  module Middleware
    module Postgresql
      module Dumper

        module Indexes

          # Dump index extensions
          def after(env)
            index_defs = Dumper.get_index_definitions(env, env.table)

            env.table.indexes.each do |index_dump|
              index_def = index_defs.find(&its.name == index_dump.name)
              if index_def.columns.blank?
                index_dump.add_option "expression: #{index_def.expression.inspect}" if index_def.expression and index_def.columns.blank?
              else
                index_dump.add_option "case_sensitive: false" unless index_def.case_sensitive?
                unless index_def.operator_classes.blank?
                  if index_def.columns.uniq.length == 1 && index_def.operator_classes.values.uniq.length == 1
                    index_dump.add_option "operator_class: #{index_def.operator_classes.values.first.inspect}"
                  else
                    index_dump.add_option "operator_class: {" + index_def.operator_classes.map{|column, val| "#{column.inspect}=>#{val.inspect}"}.join(", ") + "}"
                  end
                end
              end
            end
          end
        end

        module Table

          # Move index definitions inline
          def after(env)
            index_defs = Dumper.get_index_definitions(env, env.table)

            env.table.indexes.select(&its.columns.blank?).each do |index|
              env.table.statements << "t.index name: #{index.name.inspect}, #{index.options}"
              env.table.indexes.delete(index)
            end
          end
        end

        def self.get_index_definitions(env, table_dump)
          env.dump.data.index_definitions ||= {}
          env.dump.data.index_definitions[table_dump.name] ||= env.connection.indexes(table_dump.name)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
schema_plus_pg_indexes-0.1.6 lib/schema_plus_pg_indexes/middleware/postgresql/dumper.rb