diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-03-11 19:10:29 -0300 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2016-03-11 19:10:29 -0300 |
commit | eefcc78f7a7d75709bdf03d21d6b039853d06ccf (patch) | |
tree | b3cd93b01cebe61daca2f13b4b00e2a4aad45531 | |
parent | 12cce89c89b48b15dcf2fcdb41feaaa5b7c06991 (diff) | |
parent | b8f86ae29fde93b5f1e3ab75e57e4acb48a12b78 (diff) | |
download | rails-eefcc78f7a7d75709bdf03d21d6b039853d06ccf.tar.gz rails-eefcc78f7a7d75709bdf03d21d6b039853d06ccf.tar.bz2 rails-eefcc78f7a7d75709bdf03d21d6b039853d06ccf.zip |
Merge pull request #23797 from kamipo/case_sensitive_comparison_for_non_string_column
The BINARY Operator is only needed for string columns
3 files changed, 13 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index f453e717ed..22b71fbaba 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -607,10 +607,10 @@ module ActiveRecord end def case_sensitive_comparison(table, attribute, column, value) - if value.nil? || column.case_sensitive? - super - else + if !value.nil? && column.collation && !column.case_sensitive? table[attribute].eq(Arel::Nodes::Bin.new(Arel::Nodes::BindParam.new)) + else + super end end diff --git a/activerecord/test/cases/adapters/mysql2/case_sensitivity_test.rb b/activerecord/test/cases/adapters/mysql2/case_sensitivity_test.rb index 963116f08a..9cb05119a2 100644 --- a/activerecord/test/cases/adapters/mysql2/case_sensitivity_test.rb +++ b/activerecord/test/cases/adapters/mysql2/case_sensitivity_test.rb @@ -51,4 +51,13 @@ class Mysql2CaseSensitivityTest < ActiveRecord::Mysql2TestCase cs_uniqueness_query = queries.detect { |q| q.match(/string_cs_column/) } assert_no_match(/binary/i, cs_uniqueness_query) end + + def test_case_sensitive_comparison_for_binary_column + CollationTest.validates_uniqueness_of(:binary_column, case_sensitive: true) + CollationTest.create!(binary_column: 'A') + invalid = CollationTest.new(binary_column: 'A') + queries = assert_sql { invalid.save } + bin_uniqueness_query = queries.detect { |q| q.match(/binary_column/) } + assert_no_match(/\bBINARY\b/, bin_uniqueness_query) + end end diff --git a/activerecord/test/schema/mysql2_specific_schema.rb b/activerecord/test/schema/mysql2_specific_schema.rb index 101e657982..5a49b38457 100644 --- a/activerecord/test/schema/mysql2_specific_schema.rb +++ b/activerecord/test/schema/mysql2_specific_schema.rb @@ -33,6 +33,7 @@ ActiveRecord::Schema.define do create_table :collation_tests, id: false, force: true do |t| t.string :string_cs_column, limit: 1, collation: 'utf8_bin' t.string :string_ci_column, limit: 1, collation: 'utf8_general_ci' + t.binary :binary_column, limit: 1 end ActiveRecord::Base.connection.execute <<-SQL |