method
indexes
v5.2.3 -
Show latest stable
- Class:
ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements
indexes(table_name)public
Returns an array of indexes for the given table.
# File activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb, line 85
def indexes(table_name) # :nodoc:
scope = quoted_scope(table_name)
result = query( SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid, pg_catalog.obj_description(i.oid, 'pg_class') AS comment 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 = #{scope[:name]} AND n.nspname = #{scope[:schema]} ORDER BY i.relname, "SCHEMA")
result.map do |row|
index_name = row[0]
unique = row[1]
indkey = row[2].split(" ").map(&:to_i)
inddef = row[3]
oid = row[4]
comment = row[5]
using, expressions, where = inddef.scan(/ USING (\w+?) \((.+?)\)(?: WHERE (.+))?\z/).flatten
orders = {}
opclasses = {}
if indkey.include?(0)
columns = expressions
else
columns = Hash[query( SELECT a.attnum, a.attname FROM pg_attribute a WHERE a.attrelid = #{oid} AND a.attnum IN (#{indkey.join(",")}).strip_heredoc, "SCHEMA")].values_at(*indkey).compact
# add info on sort order (only desc order is explicitly specified, asc is the default)
# and non-default opclasses
expressions.scan(/(?<column>\w+)"?\s?(?<opclass>\w+_ops)?\s?(?<desc>DESC)?\s?(?<nulls>NULLS (?:FIRST|LAST))?/).each do |column, opclass, desc, nulls|
opclasses[column] = opclass.to_sym if opclass
if nulls
orders[column] = [desc, nulls].compact.join(" ")
else
orders[column] = :desc if desc
end
end
end
IndexDefinition.new(
table_name,
index_name,
unique,
columns,
orders: orders,
opclasses: opclasses,
where: where,
using: using.to_sym,
comment: comment.presence
)
end
end Related methods
- Instance methods
- add_column
- add_index
- bulk_change_table
- change_column
- change_column_comment
- change_column_default
- change_column_null
- change_table_comment
- client_min_messages
- client_min_messages=
- collation
- columns_for_distinct
- create_database
- create_schema
- create_schema_dumper
- ctype
- current_database
- current_schema
- default_sequence_name
- drop_database
- drop_schema
- drop_table
- encoding
- foreign_keys
- foreign_table_exists?
- foreign_tables
- index_name_exists?
- indexes
- pk_and_sequence_for
- primary_keys
- recreate_database
- remove_index
- rename_column
- rename_index
- rename_table
- reset_pk_sequence!
- schema_exists?
- schema_names
- schema_search_path
- schema_search_path=
- serial_sequence
- set_pk_sequence!
- table_comment
- table_options
- type_to_sql
- update_table_definition
- validate_constraint
- validate_foreign_key
- Private methods
-
add_column_for_alter -
add_index_opclass -
add_options_for_index_columns -
add_timestamps_for_alter -
change_column_default_for_alter -
change_column_for_alter -
change_column_null_for_alter -
create_alter_table -
create_table_definition -
data_source_sql -
extract_foreign_key_action -
extract_schema_qualified_name -
fetch_type_metadata -
new_column_from_field -
quoted_scope -
remove_timestamps_for_alter -
schema_creation