From 6bf4ca72c92e696724108d92eeb71fd672fa86fc Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Thu, 12 Oct 2023 11:45:13 +0100 Subject: [PATCH] Implement message-pack methods on Data class (#1115) --- .../connection_adapters/sqlserver/type/data.rb | 10 ++++++++++ test/cases/coerced_tests.rb | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/active_record/connection_adapters/sqlserver/type/data.rb b/lib/active_record/connection_adapters/sqlserver/type/data.rb index 1cf241450..90104d8ad 100644 --- a/lib/active_record/connection_adapters/sqlserver/type/data.rb +++ b/lib/active_record/connection_adapters/sqlserver/type/data.rb @@ -36,6 +36,16 @@ def eql?(other) self.class == other.class && value == other.value end alias :== :eql? + + def self.from_msgpack_ext(string) + type, value = string.chomp!("msgpack_ext").split(',') + + Data.new(value, type.constantize) + end + + def to_msgpack_ext + [type.class.to_s, value].join(',') + "msgpack_ext" + end end end end diff --git a/test/cases/coerced_tests.rb b/test/cases/coerced_tests.rb index 4d4ff1b7d..7cdaa0d99 100644 --- a/test/cases/coerced_tests.rb +++ b/test/cases/coerced_tests.rb @@ -2586,6 +2586,19 @@ def thread_encrypting_and_decrypting(thread_label) end end +# Need to use `install_unregistered_type_fallback` instead of `install_unregistered_type_error` so that message-pack +# can read and write `ActiveRecord::ConnectionAdapters::SQLServer::Type::Data` objects. +class ActiveRecordMessagePackTest < ActiveRecord::TestCase + private + def serializer + @serializer ||= ::MessagePack::Factory.new.tap do |factory| + ActiveRecord::MessagePack::Extensions.install(factory) + ActiveSupport::MessagePack::Extensions.install(factory) + ActiveSupport::MessagePack::Extensions.install_unregistered_type_fallback(factory) + end + end +end + # TODO: Need to uncoerce the 'SerializedAttributeTest' tests before releasing adapter for Rails 7.1 class SerializedAttributeTest < ActiveRecord::TestCase coerce_all_tests!