From f45dd74b67ac3c08f3433a229bc89f6073a810eb Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Wed, 3 Jul 2024 15:03:59 +0100 Subject: [PATCH 1/3] Remove ActiveRecord::Relation#calculate patch (#1198) --- CHANGELOG.md | 4 ++ .../sqlserver/core_ext/calculations.rb | 45 ------------------- 2 files changed, 4 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c58a1cf7..d8d83f968 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - [#1164](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1164) Fix composite primary key with different data type with triggers +#### Changed + +- []() Remove ActiveRecord::Relation#calculate patch + ## v7.1.3 #### Fixed diff --git a/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb b/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb index 34355ef53..2282c2819 100644 --- a/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb +++ b/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb @@ -8,51 +8,6 @@ module ConnectionAdapters module SQLServer module CoreExt module Calculations - def calculate(operation, column_name) - if klass.connection.sqlserver? - _calculate(operation, column_name) - else - super - end - end - - private - - # Same as original `calculate` method except we don't perform PostgreSQL hack that removes ordering. - def _calculate(operation, column_name) - operation = operation.to_s.downcase - - if @none - case operation - when "count", "sum" - result = group_values.any? ? Hash.new : 0 - return @async ? Promise::Complete.new(result) : result - when "average", "minimum", "maximum" - result = group_values.any? ? Hash.new : nil - return @async ? Promise::Complete.new(result) : result - end - end - - if has_include?(column_name) - relation = apply_join_dependency - - if operation == "count" - unless distinct_value || distinct_select?(column_name || select_for_count) - relation.distinct! - relation.select_values = [ klass.primary_key || table[Arel.star] ] - end - # PostgreSQL: ORDER BY expressions must appear in SELECT list when using DISTINCT - # Start of monkey-patch - # relation.order_values = [] if group_values.empty? - # End of monkey-patch - end - - relation.calculate(operation, column_name) - else - perform_calculation(operation, column_name) - end - end - def build_count_subquery(relation, column_name, distinct) return super unless klass.connection.adapter_name == "SQLServer" From 1372153a24c9515595914f113a9830183b28b6d5 Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Wed, 3 Jul 2024 15:05:30 +0100 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8d83f968..3d56b088a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ #### Changed -- []() Remove ActiveRecord::Relation#calculate patch +- [#1199](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1199) Remove ActiveRecord::Relation#calculate patch ## v7.1.3 From 3b24ac3c608666d08d6a6fdae56cf6955e0b7641 Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Thu, 4 Jul 2024 14:52:11 +0100 Subject: [PATCH 3/3] Update calculations.rb --- .../connection_adapters/sqlserver/core_ext/calculations.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb b/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb index 2282c2819..b7b529262 100644 --- a/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb +++ b/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb @@ -8,6 +8,9 @@ module ConnectionAdapters module SQLServer module CoreExt module Calculations + + private + def build_count_subquery(relation, column_name, distinct) return super unless klass.connection.adapter_name == "SQLServer"