method
indexes
v7.1.3.2 -
Show latest stable
- Class:
ActiveRecord::ConnectionAdapters::SQLite3::SchemaStatements
indexes(table_name)public
Returns an array of indexes for the given table.
# File activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb, line 8
def indexes(table_name)
internal_exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", "SCHEMA").filter_map do |row|
# Indexes SQLite creates implicitly for internal use start with "sqlite_".
# See https://www.sqlite.org/fileformat2.html#intschema
next if row["name"].start_with?("sqlite_")
index_sql = query_value(<<~SQL, "SCHEMA")
SELECT sql
FROM sqlite_master
WHERE name = #{quote(row['name'])} AND type = 'index'
UNION ALL
SELECT sql
FROM sqlite_temp_master
WHERE name = #{quote(row['name'])} AND type = 'index'
SQL
/\bON\b\s*"?(\w+?)"?\s*\((?<expressions>.+?)\)(?:\s*WHERE\b\s*(?<where>.+))?(?:\s*\/\*.*\*\/)?\z/ =~ index_sql
columns = internal_exec_query("PRAGMA index_info(#{quote(row['name'])})", "SCHEMA").map do |col|
col["name"]
end
orders = {}
if columns.any?(&:nil?) # index created with an expression
columns = expressions
else
# Add info on sort order for columns (only desc order is explicitly specified,
# asc is the default)
if index_sql # index_sql can be null in case of primary key indexes
index_sql.scan(/"(\w+)" DESC/).flatten.each { |order_column|
orders[order_column] = :desc
}
end
end
IndexDefinition.new(
table_name,
row["name"],
row["unique"] != 0,
columns,
where: where,
orders: orders
)
end
end Related methods
- Instance methods
- add_check_constraint
- add_foreign_key
- check_constraints
- create_schema_dumper
- indexes
- remove_check_constraint
- remove_foreign_key
- schema_creation
- Private methods
-
create_table_definition -
data_source_sql -
is_column_the_rowid? -
new_column_from_field -
quoted_scope -
valid_table_definition_options -
validate_index_length!