method
indexes
v8.0.0 -
Show latest stable
- Class:
ActiveRecord::ConnectionAdapters::MySQL::SchemaStatements
indexes(table_name)public
Returns an array of indexes for the given table.
# File activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb, line 8
def indexes(table_name)
indexes = []
current_index = nil
internal_exec_query("SHOW KEYS FROM #{quote_table_name(table_name)}", "SCHEMA").each do |row|
if current_index != row["Key_name"]
next if row["Key_name"] == "PRIMARY" # skip the primary key
current_index = row["Key_name"]
mysql_index_type = row["Index_type"].downcase.to_sym
case mysql_index_type
when :fulltext, :spatial
index_type = mysql_index_type
when :btree, :hash
index_using = mysql_index_type
end
indexes << [
row["Table"],
row["Key_name"],
row["Non_unique"].to_i == 0,
[],
lengths: {},
orders: {},
type: index_type,
using: index_using,
comment: row["Index_comment"].presence
]
end
if expression = row["Expression"]
expression = expression.gsub("\\'", "'")
expression = +"(#{expression})" unless expression.start_with?("(")
indexes.last[-2] << expression
indexes.last[-1][:expressions] ||= {}
indexes.last[-1][:expressions][expression] = expression
indexes.last[-1][:orders][expression] = :desc if row["Collation"] == "D"
else
indexes.last[-2] << row["Column_name"]
indexes.last[-1][:lengths][row["Column_name"]] = row["Sub_part"].to_i if row["Sub_part"]
indexes.last[-1][:orders][row["Column_name"]] = :desc if row["Collation"] == "D"
end
end
indexes.map do |index|
options = index.pop
if expressions = options.delete(:expressions)
orders = options.delete(:orders)
lengths = options.delete(:lengths)
columns = index[-1].to_h { |name|
[ name.to_sym, expressions[name] || +quote_column_name(name) ]
}
index[-1] = add_options_for_index_columns(
columns, order: orders, length: lengths
).values.join(", ")
end
IndexDefinition.new(*index, **options)
end
rescue StatementInvalid => e
if e.message.match?(/Table '.+' doesn't exist/)
[]
else
raise
end
end Related methods
- Instance methods
- create_schema_dumper
- create_table
- indexes
- internal_string_options_for_primary_key
- remove_column
- schema_creation
- table_alias_length
- type_to_sql
- update_table_definition
- Private methods
-
add_index_length -
add_options_for_index_columns -
create_table_definition -
data_source_sql -
default_row_format -
default_type -
extract_foreign_key_action -
extract_schema_qualified_name -
fetch_type_metadata -
integer_to_sql -
limit_to_size -
new_column_from_field -
quoted_scope -
row_format_dynamic_by_default? -
type_with_size_to_sql -
valid_primary_key_options