1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Do not mark Postgresql MAC address and UUID attributes as changed when the assigned value only varies by case.

Fix Rubocop warnings.

Fix merge conflict error.

Fix merge conflict error.
This commit is contained in:
Peter Fry 2020-06-03 11:21:12 -04:00
parent 8717744c56
commit 37f3b4dc05
No known key found for this signature in database
GPG key ID: 9CC6934BE328659E
7 changed files with 39 additions and 2 deletions

View file

@ -1,3 +1,7 @@
* Do not mark Postgresql MAC address and UUID attributes as changed when the assigned value only varies by case.
*Peter Fry*
* Resolve issue with insert_all unique_by option when used with expression index. * Resolve issue with insert_all unique_by option when used with expression index.
When the `:unique_by` option of `ActiveRecord::Persistence.insert_all` and When the `:unique_by` option of `ActiveRecord::Persistence.insert_all` and

View file

@ -12,6 +12,7 @@ require "active_record/connection_adapters/postgresql/oid/enum"
require "active_record/connection_adapters/postgresql/oid/hstore" require "active_record/connection_adapters/postgresql/oid/hstore"
require "active_record/connection_adapters/postgresql/oid/inet" require "active_record/connection_adapters/postgresql/oid/inet"
require "active_record/connection_adapters/postgresql/oid/jsonb" require "active_record/connection_adapters/postgresql/oid/jsonb"
require "active_record/connection_adapters/postgresql/oid/macaddr"
require "active_record/connection_adapters/postgresql/oid/money" require "active_record/connection_adapters/postgresql/oid/money"
require "active_record/connection_adapters/postgresql/oid/oid" require "active_record/connection_adapters/postgresql/oid/oid"
require "active_record/connection_adapters/postgresql/oid/point" require "active_record/connection_adapters/postgresql/oid/point"

View file

@ -0,0 +1,20 @@
# frozen_string_literal: true
module ActiveRecord
module ConnectionAdapters
module PostgreSQL
module OID # :nodoc:
class Macaddr < Type::String # :nodoc:
def type
:macaddr
end
private
def cast_value(value)
value.to_s.downcase
end
end
end
end
end
end

View file

@ -15,7 +15,7 @@ module ActiveRecord
private private
def cast_value(value) def cast_value(value)
casted = value.to_s casted = value.to_s.downcase
casted if casted.match?(ACCEPTABLE_UUID) casted if casted.match?(ACCEPTABLE_UUID)
end end
end end

View file

@ -535,7 +535,7 @@ module ActiveRecord
m.register_type "uuid", OID::Uuid.new m.register_type "uuid", OID::Uuid.new
m.register_type "xml", OID::Xml.new m.register_type "xml", OID::Xml.new
m.register_type "tsvector", OID::SpecializedString.new(:tsvector) m.register_type "tsvector", OID::SpecializedString.new(:tsvector)
m.register_type "macaddr", OID::SpecializedString.new(:macaddr) m.register_type "macaddr", OID::Macaddr.new
m.register_type "citext", OID::SpecializedString.new(:citext) m.register_type "citext", OID::SpecializedString.new(:citext)
m.register_type "ltree", OID::SpecializedString.new(:ltree) m.register_type "ltree", OID::SpecializedString.new(:ltree)
m.register_type "line", OID::SpecializedString.new(:line) m.register_type "line", OID::SpecializedString.new(:line)

View file

@ -93,4 +93,10 @@ class PostgresqlNetworkTest < ActiveRecord::PostgreSQLTestCase
assert_match %r{t\.cidr\s+"cidr_address",\s+default: "192\.168\.1\.0/24"}, output assert_match %r{t\.cidr\s+"cidr_address",\s+default: "192\.168\.1\.0/24"}, output
assert_match %r{t\.macaddr\s+"mac_address",\s+default: "ff:ff:ff:ff:ff:ff"}, output assert_match %r{t\.macaddr\s+"mac_address",\s+default: "ff:ff:ff:ff:ff:ff"}, output
end end
def test_mac_address_change_case_does_not_mark_dirty
model = PostgresqlNetworkAddress.create(mac_address: "Ab:Cd:Ef:01:02:03")
model.mac_address = model.mac_address.swapcase
assert_not_predicate model, :changed?
end
end end

View file

@ -120,6 +120,12 @@ class PostgresqlUUIDTest < ActiveRecord::PostgreSQLTestCase
assert_empty UUIDType.where(guid: "foobar") assert_empty UUIDType.where(guid: "foobar")
end end
def test_uuid_change_case_does_not_mark_dirty
model = UUIDType.create!(guid: "abcd-0123-4567-89ef-dead-beef-0101-1010")
model.guid = model.guid.swapcase
assert_not_predicate model, :changed?
end
class DuckUUID class DuckUUID
def initialize(uuid) def initialize(uuid)
@uuid = uuid @uuid = uuid