Method: ActiveRecord::ConnectionAdapters::SQLite3::SchemaStatements#indexes

Defined in:
activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb

#indexes(table_name) ⇒ Object

Returns an array of indexes for the given table.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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("      SELECT sql\n      FROM sqlite_master\n      WHERE name = \#{quote(row['name'])} AND type = 'index'\n      UNION ALL\n      SELECT sql\n      FROM sqlite_temp_master\n      WHERE name = \#{quote(row['name'])} AND type = 'index'\n    SQL\n\n    /\\bON\\b\\s*\"?(\\w+?)\"?\\s*\\((?<expressions>.+?)\\)(?:\\s*WHERE\\b\\s*(?<where>.+))?(?:\\s*\\/\\*.*\\*\\/)?\\z/i =~ index_sql\n\n    columns = internal_exec_query(\"PRAGMA index_info(\#{quote(row['name'])})\", \"SCHEMA\").map do |col|\n      col[\"name\"]\n    end\n\n    orders = {}\n\n    if columns.any?(&:nil?) # index created with an expression\n      columns = expressions\n    else\n      # Add info on sort order for columns (only desc order is explicitly specified,\n      # asc is the default)\n      if index_sql # index_sql can be null in case of primary key indexes\n        index_sql.scan(/\"(\\w+)\" DESC/).flatten.each { |order_column|\n          orders[order_column] = :desc\n        }\n      end\n    end\n\n    IndexDefinition.new(\n      table_name,\n      row[\"name\"],\n      row[\"unique\"] != 0,\n      columns,\n      where: where,\n      orders: orders\n    )\n  end\nend\n", "SCHEMA")