From 10eb9845744ab8d4f471aef4d34a764816f221a2 Mon Sep 17 00:00:00 2001 From: Jesse Chavez Date: Fri, 24 Jan 2025 09:03:08 +1100 Subject: [PATCH] Monkey patch active record relation query attribute hash method to better compare objects This a important since arel and AR commonly use array - operator --- .../relation_query_attribute_monkey_patch.rb | 26 +++++++++++++++++++ lib/arjdbc/mysql/adapter.rb | 2 ++ lib/arjdbc/postgresql/adapter.rb | 2 ++ lib/arjdbc/sqlite3/adapter.rb | 2 ++ 4 files changed, 32 insertions(+) create mode 100644 lib/arjdbc/abstract/relation_query_attribute_monkey_patch.rb diff --git a/lib/arjdbc/abstract/relation_query_attribute_monkey_patch.rb b/lib/arjdbc/abstract/relation_query_attribute_monkey_patch.rb new file mode 100644 index 000000000..1ba572bc3 --- /dev/null +++ b/lib/arjdbc/abstract/relation_query_attribute_monkey_patch.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "active_model/attribute" + +module ActiveRecord + # NOTE: improved implementation for hash methods that is used to + # compare objects. AR and arel commonly use `[a, b] - [b]` operations and + # JRuby internally uses the hash method to implement that operation, + # on the other hand, CRuby does not use the hash method + # for small arrays (length <= 16). + class Relation + # monkey patch + module RelationQueryAttributeMonkeyPatch + private + + def hash + # [self.class, name, value_for_database, type].hash + [self.class, name, value_before_type_cast, type].hash + end + end + + class QueryAttribute + prepend RelationQueryAttributeMonkeyPatch + end + end +end diff --git a/lib/arjdbc/mysql/adapter.rb b/lib/arjdbc/mysql/adapter.rb index 1bf6ce612..b45b254ae 100644 --- a/lib/arjdbc/mysql/adapter.rb +++ b/lib/arjdbc/mysql/adapter.rb @@ -11,6 +11,8 @@ require 'arjdbc/abstract/statement_cache' require 'arjdbc/abstract/transaction_support' +require "arjdbc/abstract/relation_query_attribute_monkey_patch" + module ActiveRecord module ConnectionAdapters AbstractMysqlAdapter.class_eval do diff --git a/lib/arjdbc/postgresql/adapter.rb b/lib/arjdbc/postgresql/adapter.rb index 6c24393ca..d83d36d07 100644 --- a/lib/arjdbc/postgresql/adapter.rb +++ b/lib/arjdbc/postgresql/adapter.rb @@ -27,6 +27,8 @@ require 'active_model' +require "arjdbc/abstract/relation_query_attribute_monkey_patch" + module ArJdbc # Strives to provide Rails built-in PostgreSQL adapter (API) compatibility. module PostgreSQL diff --git a/lib/arjdbc/sqlite3/adapter.rb b/lib/arjdbc/sqlite3/adapter.rb index 913eb5dac..407e22017 100644 --- a/lib/arjdbc/sqlite3/adapter.rb +++ b/lib/arjdbc/sqlite3/adapter.rb @@ -18,6 +18,8 @@ require "active_support/core_ext/class/attribute" require "arjdbc/sqlite3/column" +require "arjdbc/abstract/relation_query_attribute_monkey_patch" + module SQLite3 module Constants module Open