Skip to content

Commit bc9dc41

Browse files
committed
Merge pull request rails#26208 from nanaya/pg-insensitive-text
Fix case insensitive check for text column in pg
2 parents 469dd36 + 4c2f7ee commit bc9dc41

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Fixed support for case insensitive comparisons of `text` columns in
2+
PostgreSQL.
3+
4+
*Edho Arief*
5+
16
* Made ActiveRecord consistently use `ActiveRecord::Type` (not `ActiveModel::Type`)
27

38
*Iain Beeston*

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,11 +770,15 @@ def can_perform_case_insensitive_comparison_for?(column)
770770
@case_insensitive_cache[column.sql_type] ||= begin
771771
sql = <<-end_sql
772772
SELECT exists(
773+
SELECT * FROM pg_proc
774+
WHERE proname = 'lower'
775+
AND proargtypes = ARRAY[#{quote column.sql_type}::regtype]::oidvector
776+
) OR exists(
773777
SELECT * FROM pg_proc
774778
INNER JOIN pg_cast
775-
ON casttarget::text::oidvector = proargtypes
779+
ON ARRAY[casttarget]::oidvector = proargtypes
776780
WHERE proname = 'lower'
777-
AND castsource = '#{column.sql_type}'::regtype::oid
781+
AND castsource = #{quote column.sql_type}::regtype
778782
)
779783
end_sql
780784
execute_and_clear(sql, "SCHEMA", []) do |result|
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require "cases/helper"
2+
3+
class PostgresqlCaseInsensitiveTest < ActiveRecord::PostgreSQLTestCase
4+
class Default < ActiveRecord::Base; end
5+
6+
def test_case_insensitiveness
7+
connection = ActiveRecord::Base.connection
8+
table = Default.arel_table
9+
10+
column = Default.columns_hash["char1"]
11+
comparison = connection.case_insensitive_comparison table, :char1, column, nil
12+
assert_match /lower/i, comparison.to_sql
13+
14+
column = Default.columns_hash["char2"]
15+
comparison = connection.case_insensitive_comparison table, :char2, column, nil
16+
assert_match /lower/i, comparison.to_sql
17+
18+
column = Default.columns_hash["char3"]
19+
comparison = connection.case_insensitive_comparison table, :char3, column, nil
20+
assert_match /lower/i, comparison.to_sql
21+
22+
column = Default.columns_hash["multiline_default"]
23+
comparison = connection.case_insensitive_comparison table, :multiline_default, column, nil
24+
assert_match /lower/i, comparison.to_sql
25+
end
26+
end

0 commit comments

Comments
 (0)