method
indexes
rails latest stable - Class:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::SchemaStatements
Method deprecated or moved
This method is deprecated or moved on the latest stable version. The last existing version (v4.1.8) is shown here.
indexes(table_name, name = nil)public
Returns an array of indexes for the given table.
# File activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb, line 143
def indexes(table_name, name = nil)
result = query( SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid FROM pg_class t INNER JOIN pg_index d ON t.oid = d.indrelid INNER JOIN pg_class i ON d.indexrelid = i.oid WHERE i.relkind = 'i' AND d.indisprimary = 'f' AND t.relname = '#{table_name}' AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) ) ORDER BY i.relname, 'SCHEMA')
result.map do |row|
index_name = row[0]
unique = row[1] == 't'
indkey = row[2].split(" ")
inddef = row[3]
oid = row[4]
columns = Hash[query( SELECT a.attnum, a.attname FROM pg_attribute a WHERE a.attrelid = #{oid} AND a.attnum IN (#{indkey.join(",")}), "SCHEMA")]
column_names = columns.values_at(*indkey).compact
unless column_names.empty?
# add info on sort order for columns (only desc order is explicitly specified, asc is the default)
desc_order_columns = inddef.scan(/(\w+) DESC/).flatten
orders = desc_order_columns.any? ? Hash[desc_order_columns.map {|order_column| [order_column, :desc]}] : {}
where = inddef.scan(/WHERE (.+)$/).flatten[0]
using = inddef.scan(/USING (.+?) /).flatten[0].to_sym
IndexDefinition.new(table_name, index_name, unique, column_names, [], orders, where, nil, using)
end
end.compact
end Related methods
- Instance methods
- add_column
- add_index
- change_column
- change_column_default
- change_column_null
- client_min_messages
- client_min_messages=
- collation
- column_for
- columns
- columns_for_distinct
- create_database
- create_schema
- ctype
- current_database
- current_schema
- default_sequence_name
- drop_database
- drop_schema
- encoding
- index_name_exists?
- index_name_length
- indexes
- pk_and_sequence_for
- primary_key
- 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
- table_exists?
- tables
- type_to_sql