mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Correctly ignore case_sensitive
for UUID uniqueness validation
I think we should deprecate this behavior and just error if you tell us
to do a case insensitive comparison for types which are not case
sensitive. Partially reverts 35592307
Fixes #18195
This commit is contained in:
parent
aff03e71b4
commit
a983e1e89c
6 changed files with 31 additions and 2 deletions
|
@ -16,7 +16,7 @@ module ActiveRecord
|
|||
attr_reader :name, :cast_type, :null, :sql_type, :default, :default_function
|
||||
|
||||
delegate :type, :precision, :scale, :limit, :klass, :accessor,
|
||||
:number?, :binary?, :changed?,
|
||||
:text?, :number?, :binary?, :changed?,
|
||||
:type_cast_from_user, :type_cast_from_database, :type_cast_for_database,
|
||||
:type_cast_for_schema,
|
||||
to: :cast_type
|
||||
|
|
|
@ -8,6 +8,10 @@ module ActiveRecord
|
|||
def initialize(type)
|
||||
@type = type
|
||||
end
|
||||
|
||||
def text?
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -21,6 +21,10 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def text?
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def cast_value(value)
|
||||
|
|
|
@ -50,6 +50,10 @@ module ActiveRecord
|
|||
|
||||
# These predicates are not documented, as I need to look further into
|
||||
# their use, and see if they can be removed entirely.
|
||||
def text? # :nodoc:
|
||||
false
|
||||
end
|
||||
|
||||
def number? # :nodoc:
|
||||
false
|
||||
end
|
||||
|
|
|
@ -65,7 +65,7 @@ module ActiveRecord
|
|||
value = value.to_s[0, column.limit]
|
||||
end
|
||||
|
||||
if !options[:case_sensitive] && value.is_a?(String)
|
||||
if !options[:case_sensitive] && value && column.text?
|
||||
# will use SQL LOWER function before comparison, unless it detects a case insensitive collation
|
||||
klass.connection.case_insensitive_comparison(table, attribute, column, value)
|
||||
else
|
||||
|
|
|
@ -116,6 +116,23 @@ class PostgresqlUUIDTest < ActiveRecord::TestCase
|
|||
output = dump_table_schema "uuid_data_type"
|
||||
assert_match %r{t.uuid "guid"}, output
|
||||
end
|
||||
|
||||
def test_uniqueness_validation_ignores_uuid
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
self.table_name = "uuid_data_type"
|
||||
validates :guid, uniqueness: { case_sensitive: false }
|
||||
|
||||
def self.name
|
||||
"UUIDType"
|
||||
end
|
||||
end
|
||||
|
||||
record = klass.create!(guid: "a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11")
|
||||
duplicate = klass.new(guid: record.guid)
|
||||
|
||||
assert record.guid.present? # Ensure we actually are testing a UUID
|
||||
assert_not duplicate.valid?
|
||||
end
|
||||
end
|
||||
|
||||
class PostgresqlLargeKeysTest < ActiveRecord::TestCase
|
||||
|
|
Loading…
Reference in a new issue