From ae733fd403c7ad1e7547df0a98b34f213ece5879 Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Tue, 14 May 2024 20:14:04 +0100 Subject: [PATCH] Return row-count for pluck (#1171) --- .../sqlserver/database_statements.rb | 3 ++- test/cases/coerced_tests.rb | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/active_record/connection_adapters/sqlserver/database_statements.rb b/lib/active_record/connection_adapters/sqlserver/database_statements.rb index c2463eb92..e1259792c 100644 --- a/lib/active_record/connection_adapters/sqlserver/database_statements.rb +++ b/lib/active_record/connection_adapters/sqlserver/database_statements.rb @@ -14,7 +14,7 @@ def write_query?(sql) # :nodoc: end def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true) - log(sql, name, async: async) do + log(sql, name, async: async) do |notification_payload| with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn| result = if id_insert_table_name = query_requires_identity_insert?(sql) with_identity_insert_enabled(id_insert_table_name, conn) { internal_raw_execute(sql, conn, perform_do: true) } @@ -22,6 +22,7 @@ def raw_execute(sql, name, async: false, allow_retry: false, materialize_transac internal_raw_execute(sql, conn, perform_do: true) end verified! + notification_payload[:row_count] = result result end end diff --git a/test/cases/coerced_tests.rb b/test/cases/coerced_tests.rb index 04c258cd1..9b850d295 100644 --- a/test/cases/coerced_tests.rb +++ b/test/cases/coerced_tests.rb @@ -363,6 +363,17 @@ def test_payload_row_count_on_select_all_coerced Book.lease_connection.add_index(:books, [:author_id, :name], unique: true) end + # Need to remove index as SQL Server considers NULLs on a unique-index to be equal unlike PostgreSQL/MySQL/SQLite. + coerce_tests! :test_payload_row_count_on_pluck + def test_payload_row_count_on_pluck_coerced + connection.remove_index(:books, column: [:author_id, :name]) + + original_test_payload_row_count_on_pluck + ensure + Book.where(author_id: nil, name: 'row count book 2').delete_all + Book.lease_connection.add_index(:books, [:author_id, :name], unique: true) + end + # Need to remove index as SQL Server considers NULLs on a unique-index to be equal unlike PostgreSQL/MySQL/SQLite. coerce_tests! :test_payload_row_count_on_raw_sql def test_payload_row_count_on_raw_sql_coerced