Method: ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements#indexes
- Defined in:
- activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
#indexes(table_name) ⇒ Object
Returns an array of indexes for the given table.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# 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(" SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid,\n pg_catalog.obj_description(i.oid, 'pg_class') AS comment, d.indisvalid\n FROM pg_class t\n INNER JOIN pg_index d ON t.oid = d.indrelid\n INNER JOIN pg_class i ON d.indexrelid = i.oid\n LEFT JOIN pg_namespace n ON n.oid = t.relnamespace\n WHERE i.relkind IN ('i', 'I')\n AND d.indisprimary = 'f'\n AND t.relname = \#{scope[:name]}\n AND n.nspname = \#{scope[:schema]}\n ORDER BY i.relname\n SQL\n\n result.map do |row|\n index_name = row[0]\n unique = row[1]\n indkey = row[2].split(\" \").map(&:to_i)\n inddef = row[3]\n oid = row[4]\n comment = row[5]\n valid = row[6]\n using, expressions, include, nulls_not_distinct, where = inddef.scan(/ USING (\\w+?) \\((.+?)\\)(?: INCLUDE \\((.+?)\\))?( NULLS NOT DISTINCT)?(?: WHERE (.+))?\\z/m).flatten\n\n orders = {}\n opclasses = {}\n include_columns = include ? include.split(\",\").map { |c| Utils.unquote_identifier(c.strip.gsub('\"\"', '\"')) } : []\n\n if indkey.include?(0)\n columns = expressions\n else\n columns = column_names_from_column_numbers(oid, indkey)\n\n # prevent INCLUDE columns from being matched\n columns.reject! { |c| include_columns.include?(c) }\n\n # add info on sort order (only desc order is explicitly specified, asc is the default)\n # and non-default opclasses\n expressions.scan(/(?<column>\\w+)\"?\\s?(?<opclass>\\w+_ops(_\\w+)?)?\\s?(?<desc>DESC)?\\s?(?<nulls>NULLS (?:FIRST|LAST))?/).each do |column, opclass, desc, nulls|\n opclasses[column] = opclass.to_sym if opclass\n if nulls\n orders[column] = [desc, nulls].compact.join(\" \")\n else\n orders[column] = :desc if desc\n end\n end\n end\n\n IndexDefinition.new(\n table_name,\n index_name,\n unique,\n columns,\n orders: orders,\n opclasses: opclasses,\n where: where,\n using: using.to_sym,\n include: include_columns.presence,\n nulls_not_distinct: nulls_not_distinct.present?,\n comment: comment.presence,\n valid: valid\n )\n end\nend\n", "SCHEMA") |