From 20acbd136d42f322b27fd0d1c88f0d850757b382 Mon Sep 17 00:00:00 2001 From: Florian Bergmann Date: Thu, 30 Jun 2022 10:30:29 +0200 Subject: [PATCH] Accept ASCII_8BIT encoded strings as valid values on serialize Increases support for temporary tables and join tables. We experienced for example bugs with pagy (gem) since it sends the value ASCII_8BIT encoded. --- lib/mysql-binuuid/type.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/mysql-binuuid/type.rb b/lib/mysql-binuuid/type.rb index 8273fa6..66f2841 100644 --- a/lib/mysql-binuuid/type.rb +++ b/lib/mysql-binuuid/type.rb @@ -28,7 +28,11 @@ def cast(value) # it to the database. def serialize(value) return if value.nil? - undashed_uuid = strip_dashes(value) + if value.is_a?(String) && value.encoding == Encoding::ASCII_8BIT && strip_dashes(value).length != 32 + undashed_uuid = value.unpack1('H*') + else + undashed_uuid = strip_dashes(value) + end # To avoid SQL injection, verify that it looks like a UUID. ActiveRecord # does not explicity escape the Binary data type. escaping is implicit as