Skip to content

Commit 95e5a54

Browse files
fatkodimachaadow
andcommitted
Fix retrieving PostgreSQL indexes with keyword-named columns
Co-authored-by: Chedli Bourguiba <[email protected]>
1 parent d8a3011 commit 95e5a54

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def indexes(table_name) # :nodoc:
112112
inddef = row[3]
113113
comment = row[4]
114114
valid = row[5]
115-
columns = decode_string_array(row[6])
115+
columns = decode_string_array(row[6]).map { |c| Utils.unquote_identifier(c.strip.gsub('""', '"')) }
116116

117117
using, expressions, include, nulls_not_distinct, where = inddef.scan(/ USING (\w+?) \((.+?)\)(?: INCLUDE \((.+?)\))?( NULLS NOT DISTINCT)?(?: WHERE (.+))?\z/m).flatten
118118

activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,22 @@ def test_index_with_not_distinct_nulls
414414
end
415415
end
416416

417+
def test_index_keyword_column_name
418+
with_example_table("timestamp integer") do
419+
@connection.add_index "ex", :timestamp, name: "keyword"
420+
index = @connection.indexes("ex").find { |idx| idx.name == "keyword" }
421+
assert_equal ["timestamp"], index.columns
422+
end
423+
end
424+
425+
def test_index_escaped_quotes_column_name
426+
with_example_table(%{"I""like""quotes" integer}) do
427+
@connection.add_index "ex", :"I\"like\"quotes", name: "quotes"
428+
index = @connection.indexes("ex").find { |idx| idx.name == "quotes" }
429+
assert_equal ["I\"like\"quotes"], index.columns
430+
end
431+
end
432+
417433
def test_columns_for_distinct_zero_orders
418434
assert_equal "posts.id",
419435
@connection.columns_for_distinct("posts.id", [])

0 commit comments

Comments
 (0)