diff options
Diffstat (limited to 'activerecord/lib/active_record')
6 files changed, 27 insertions, 24 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index c2087b8216..202187a047 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -522,7 +522,7 @@ module ActiveRecord end def default_uniqueness_comparison(attribute, value, klass) # :nodoc: - case_sensitive_comparison(attribute, value) + attribute.eq(value) end def case_sensitive_comparison(attribute, value) # :nodoc: 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 d99c9b9d02..a518b897a0 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -461,15 +461,16 @@ module ActiveRecord def default_uniqueness_comparison(attribute, value, klass) # :nodoc: column = column_for_attribute(attribute) - if column.collation && !column.case_sensitive? + if column.collation && !column.case_sensitive? && !value.nil? ActiveSupport::Deprecation.warn(<<~MSG.squish) Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :#{attribute.name} attribute in #{klass} model, pass `case_sensitive: true` option explicitly to the uniqueness validator. MSG + attribute.eq(Arel::Nodes::Bin.new(value)) + else + super end - - super end def case_sensitive_comparison(attribute, value) # :nodoc: diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 72ccf7739f..a09b5f0e96 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -93,7 +93,7 @@ module ActiveRecord # record or pass <tt>returning: false</tt> to omit the clause. # # [:unique_by] - # (Postgres and SQLite only) In a table with more than one unique constaint or index, + # (Postgres and SQLite only) In a table with more than one unique constraint or index, # new records may considered duplicates according to different criteria. By default, # new rows will be skipped if they violate _any_ unique constraint/index. By defining # <tt>:unique_by</tt>, you can skip rows that would create duplicates according to the given @@ -218,7 +218,7 @@ module ActiveRecord # record or pass <tt>returning: false</tt> to omit the clause. # # [:unique_by] - # (Postgres and SQLite only) In a table with more than one unique constaint or index, + # (Postgres and SQLite only) In a table with more than one unique constraint or index, # new records may considered duplicates according to different criteria. For MySQL, # an upsert will take place if a new record violates _any_ unique constraint. For # Postgres and SQLite, new rows will replace existing rows when the new row has the diff --git a/activerecord/lib/active_record/querying.rb b/activerecord/lib/active_record/querying.rb index 79af51bdba..36e2134c46 100644 --- a/activerecord/lib/active_record/querying.rb +++ b/activerecord/lib/active_record/querying.rb @@ -2,19 +2,23 @@ module ActiveRecord module Querying - delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, :none?, :one?, to: :all - delegate :second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!, :forty_two, :forty_two!, :third_to_last, :third_to_last!, :second_to_last, :second_to_last!, to: :all - delegate :first_or_create, :first_or_create!, :first_or_initialize, to: :all - delegate :find_or_create_by, :find_or_create_by!, :create_or_find_by, :create_or_find_by!, :find_or_initialize_by, to: :all - delegate :find_by, :find_by!, to: :all - delegate :destroy_all, :delete_all, :update_all, :destroy_by, :delete_by, to: :all - delegate :find_each, :find_in_batches, :in_batches, to: :all - delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins, :left_joins, :left_outer_joins, :or, - :where, :rewhere, :preload, :eager_load, :includes, :from, :lock, :readonly, :extending, - :having, :create_with, :distinct, :references, :none, :unscope, :merge, - :reselect, to: :all - delegate :count, :average, :minimum, :maximum, :sum, :calculate, to: :all - delegate :pluck, :pick, :ids, to: :all + QUERYING_METHODS = [ + :find, :find_by, :find_by!, :take, :take!, :first, :first!, :last, :last!, + :second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!, + :forty_two, :forty_two!, :third_to_last, :third_to_last!, :second_to_last, :second_to_last!, + :exists?, :any?, :many?, :none?, :one?, + :first_or_create, :first_or_create!, :first_or_initialize, + :find_or_create_by, :find_or_create_by!, :find_or_initialize_by, + :create_or_find_by, :create_or_find_by!, + :destroy_all, :delete_all, :update_all, :destroy_by, :delete_by, + :find_each, :find_in_batches, :in_batches, + :select, :reselect, :order, :reorder, :group, :limit, :offset, :joins, :left_joins, :left_outer_joins, + :where, :rewhere, :preload, :eager_load, :includes, :from, :lock, :readonly, :extending, :or, + :having, :create_with, :distinct, :references, :none, :unscope, :merge, :except, + :count, :average, :minimum, :maximum, :sum, :calculate, + :pluck, :pick, :ids + ].freeze # :nodoc: + delegate(*QUERYING_METHODS, to: :all) # Executes a custom SQL query against your database and returns all the results. The results will # be returned as an array, with the requested columns encapsulated as attributes of the model you call diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 9425332321..0f2cff4c9c 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -243,10 +243,10 @@ module ActiveRecord # Allows you to change a previously set select statement. # # Post.select(:title, :body) - # # SELECT `posts.title`, `posts.body` FROM `posts` + # # SELECT `posts`.`title`, `posts`.`body` FROM `posts` # # Post.select(:title, :body).reselect(:created_at) - # # SELECT `posts.created_at` FROM `posts` + # # SELECT `posts`.`created_at` FROM `posts` # # This is short-hand for <tt>unscope(:select).select(fields)</tt>. # Note that we're unscoping the entire select statement. diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 7451c15938..2c3a2fb797 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -60,9 +60,7 @@ module ActiveRecord comparison = relation.bind_attribute(attribute, value) do |attr, bind| return relation.none! if bind.unboundable? - if bind.nil? - attr.eq(bind) - elsif !options.key?(:case_sensitive) + if !options.key?(:case_sensitive) || bind.nil? klass.connection.default_uniqueness_comparison(attr, bind, klass) elsif options[:case_sensitive] klass.connection.case_sensitive_comparison(attr, bind) |