Skip to content

Commit

Permalink
try a next thing
Browse files Browse the repository at this point in the history
  • Loading branch information
BuonOmo committed Oct 4, 2024
1 parent db75eb6 commit 1067d43
Showing 1 changed file with 9 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,77 +74,16 @@ def primary_keys(table_name)
SQL
end

# Returns an array of indexes for the given table.
def indexes(table_name) # :nodoc:
return super unless database_version >= 24_02_02

scope = quoted_scope(table_name)

result = query(<<~SQL, "SCHEMA")
SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid,
pg_catalog.obj_description(i.oid, 'pg_class') AS comment, d.indisvalid
FROM pg_class t
INNER JOIN pg_index d ON t.oid = d.indrelid
INNER JOIN pg_class i ON d.indexrelid = i.oid
INNER JOIN pg_attribute a ON t.oid = a.attrelid
LEFT JOIN pg_namespace n ON n.oid = t.relnamespace
WHERE i.relkind IN ('i', 'I')
AND d.indisprimary = 'f'
AND t.relname = #{scope[:name]}
AND n.nspname = #{scope[:schema]}
AND NOT a.attishidden
ORDER BY i.relname
def column_names_from_column_numbers(table_oid, column_numbers)
super unless database_version >= 24_02_02

Hash[query(<<~SQL, "SCHEMA")].values_at(*column_numbers).compact
SELECT a.attnum, a.attname
FROM pg_attribute a
WHERE a.attrelid = #{table_oid}
AND a.attnum IN (#{column_numbers.join(", ")})
AND NOT a.attishidden
SQL

result.map do |row|
index_name = row[0]
unique = row[1]
indkey = row[2].split(" ").map(&:to_i)
inddef = row[3]
oid = row[4]
comment = row[5]
valid = row[6]
using, expressions, include, nulls_not_distinct, where = inddef.scan(/ USING (\w+?) \((.+?)\)(?: INCLUDE \((.+?)\))?( NULLS NOT DISTINCT)?(?: WHERE (.+))?\z/m).flatten

orders = {}
opclasses = {}
include_columns = include ? include.split(",").map { |c| Utils.unquote_identifier(c.strip.gsub('""', '"')) } : []

if indkey.include?(0)
columns = expressions
else
columns = column_names_from_column_numbers(oid, indkey)

# prevent INCLUDE columns from being matched
columns.reject! { |c| include_columns.include?(c) }

# add info on sort order (only desc order is explicitly specified, asc is the default)
# and non-default opclasses
expressions.scan(/(?<column>\w+)"?\s?(?<opclass>\w+_ops(_\w+)?)?\s?(?<desc>DESC)?\s?(?<nulls>NULLS (?:FIRST|LAST))?/).each do |column, opclass, desc, nulls|
opclasses[column] = opclass.to_sym if opclass
if nulls
orders[column] = [desc, nulls].compact.join(" ")
else
orders[column] = :desc if desc
end
end
end

IndexDefinition.new(
table_name,
index_name,
unique,
columns,
orders: orders,
opclasses: opclasses,
where: where,
using: using.to_sym,
include: include_columns.presence,
nulls_not_distinct: nulls_not_distinct.present?,
comment: comment.presence,
valid: valid
)
end
end

# OVERRIDE: CockroachDB does not support deferrable constraints.
Expand Down

0 comments on commit 1067d43

Please sign in to comment.