Sha256: 52c67f241294f2753339c31b1ace2714f340d3b567c0bb265eec4f7f9472910c

Contents?: true

Size: 1.5 KB

Versions: 14

Compression:

Stored size: 1.5 KB

Contents

module Scenic
  module Adapters
    class Postgres
      # Fetches indexes on objects from the Postgres connection.
      #
      # @api private
      class Indexes
        def initialize(connection:)
          @connection = connection
        end

        # Indexes on the provided object.
        #
        # @param name [String] The name of the object we want indexes from.
        # @return [Array<Scenic::Index>]
        def on(name)
          indexes_on(name).map(&method(:index_from_database))
        end

        private

        attr_reader :connection
        delegate :quote_table_name, to: :connection

        def indexes_on(name)
          connection.execute(<<-SQL)
            SELECT
              t.relname as object_name,
              i.relname as index_name,
              pg_get_indexdef(d.indexrelid) AS definition
            FROM pg_class t
            INNER JOIN pg_index d ON t.oid = d.indrelid
            INNER JOIN pg_class i ON d.indexrelid = i.oid
            LEFT JOIN pg_namespace n ON n.oid = i.relnamespace
            WHERE i.relkind = 'i'
              AND d.indisprimary = 'f'
              AND t.relname = '#{name}'
              AND n.nspname = ANY (current_schemas(false))
            ORDER BY i.relname
          SQL
        end

        def index_from_database(result)
          Scenic::Index.new(
            object_name: result["object_name"],
            index_name: result["index_name"],
            definition: result["definition"],
          )
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
scenic-1.7.0 lib/scenic/adapters/postgres/indexes.rb
scenic-1.6.0 lib/scenic/adapters/postgres/indexes.rb
scenic-1.5.5 lib/scenic/adapters/postgres/indexes.rb
scenic-jets-1.5.5 lib/scenic/adapters/postgres/indexes.rb
scenic-jets-1.5.4 lib/scenic/adapters/postgres/indexes.rb
scenic-1.5.4 lib/scenic/adapters/postgres/indexes.rb
scenic-1.5.3 lib/scenic/adapters/postgres/indexes.rb
scenic-1.5.2 lib/scenic/adapters/postgres/indexes.rb
scenic-1.5.1 lib/scenic/adapters/postgres/indexes.rb
scenic-1.5.0 lib/scenic/adapters/postgres/indexes.rb
scenic-1.4.1 lib/scenic/adapters/postgres/indexes.rb
scenic-1.4.0 lib/scenic/adapters/postgres/indexes.rb
scenic-1.3.0 lib/scenic/adapters/postgres/indexes.rb
scenic-1.2.0 lib/scenic/adapters/postgres/indexes.rb