提交 a983e1e8 编写于 作者: S Sean Griffin

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
上级 aff03e71
......@@ -16,7 +16,7 @@ module Format
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 @@ class SpecializedString < Type::String # :nodoc:
def initialize(type)
@type = type
end
def text?
false
end
end
end
end
......
......@@ -21,6 +21,10 @@ def type_cast_for_database(value)
end
end
def text?
true
end
private
def cast_value(value)
......
......@@ -50,6 +50,10 @@ def type_cast_for_schema(value) # :nodoc:
# 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 @@ def build_relation(klass, table, attribute, value) #:nodoc:
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 @@ def test_schema_dump_with_shorthand
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册