method
indexes
v8.0.0 -
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 86
def indexes(table_name) # :nodoc:
scope = quoted_scope(table_name)
result = query(<<~SQL, "SCHEMA")
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, d.indisvalid
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 = t.relnamespace
WHERE i.relkind IN ('i', 'I')
AND d.indisprimary = 'f'
AND t.relname = #{scope[:name]}
AND n.nspname = #{scope[:schema]}
ORDER BY i.relname
SQL
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]
valid = row[6]
using, expressions, include, nulls_not_distinct, where = inddef.scan(/ USING (\w+?) \((.+?)\)(?: INCLUDE \((.+?)\))?( NULLS NOT DISTINCT)?(?: WHERE (.+))?\z/).flatten
orders = {}
opclasses = {}
include_columns = include ? include.split(",").map { |c| Utils.unquote_identifier(c.strip.gsub('""', '"')) } : []
if indkey.include?(0)
columns = expressions
else
columns = column_names_from_column_numbers(oid, indkey)
# prevent INCLUDE columns from being matched
columns.reject! { |c| include_columns.include?(c) }
# 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(_\w+)?)?\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,
include: include_columns.presence,
nulls_not_distinct: nulls_not_distinct.present?,
comment: comment.presence,
valid: valid
)
end
end Related methods
- Instance methods
- ON
- add_column
- add_column_for_alter
- add_exclusion_constraint
- add_foreign_key
- add_index
- add_index_opclass
- add_index_options
- add_options_for_index_columns
- add_unique_constraint
- assert_valid_deferrable
- build_change_column_default_definition
- build_change_column_definition
- build_create_index_definition
- change_column
- change_column_comment
- change_column_default
- change_column_for_alter
- change_column_null
- change_column_null_for_alter
- change_table_comment
- check_constraints
- client_min_messages
- client_min_messages=
- collation
- column_names_from_column_numbers
- columns_for_distinct
- create_alter_table
- create_database
- create_schema
- create_schema_dumper
- create_table_definition
- ctype
- current_database
- current_schema
- data_source_sql
- default_sequence_name
- drop_database
- drop_schema
- drop_table
- encoding
- exclusion_constraint_for
- exclusion_constraint_for!
- exclusion_constraint_name
- exclusion_constraint_options
- exclusion_constraints
- extract_constraint_deferrable
- extract_foreign_key_action
- extract_schema_qualified_name
- fetch_type_metadata
- foreign_key_column_for
- foreign_keys
- foreign_table_exists?
- foreign_tables
- index_name
- index_name_exists?
- indexes
- inherited_table_names
- new_column_from_field
- pk_and_sequence_for
- primary_keys
- quoted_include_columns_for_index
- quoted_scope
- recreate_database
- reference_name_for_table
- remove_exclusion_constraint
- remove_index
- remove_unique_constraint
- rename_column
- rename_index
- rename_table
- reset_pk_sequence!
- schema_creation
- schema_exists?
- schema_names
- schema_search_path
- schema_search_path=
- sequence_name_from_parts
- serial_sequence
- set_pk_sequence!
- table_comment
- table_options
- table_partition_definition
- type_to_sql
- unique_constraint_for
- unique_constraint_for!
- unique_constraint_name
- unique_constraint_options
- unique_constraints
- update_table_definition
- validate_check_constraint
- validate_constraint
- validate_foreign_key