class Scenic::Adapters::Postgres::Indexes

Fetches indexes on objects from the Postgres connection.

@api private

Attributes

connection[R]

Public Class Methods

new(connection:) click to toggle source
# File lib/scenic/adapters/postgres/indexes.rb, line 8
def initialize(connection:)
  @connection = connection
end

Public Instance Methods

on(name) click to toggle source

Indexes on the provided object.

@param name [String] The name of the object we want indexes from. @return [Array<Scenic::Index>]

# File lib/scenic/adapters/postgres/indexes.rb, line 16
def on(name)
  indexes_on(name).map(&method(:index_from_database))
end

Private Instance Methods

index_from_database(result) click to toggle source
# File lib/scenic/adapters/postgres/indexes.rb, line 43
def index_from_database(result)
  Scenic::Index.new(
    object_name: result["object_name"],
    index_name: result["index_name"],
    definition: result["definition"]
  )
end
indexes_on(name) click to toggle source
# File lib/scenic/adapters/postgres/indexes.rb, line 25
        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