Skip to content

Commit ae733fd

Browse files
authored
Return row-count for pluck (#1171)
1 parent b6257ad commit ae733fd

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ def write_query?(sql) # :nodoc:
1414
end
1515

1616
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
17-
log(sql, name, async: async) do
17+
log(sql, name, async: async) do |notification_payload|
1818
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
1919
result = if id_insert_table_name = query_requires_identity_insert?(sql)
2020
with_identity_insert_enabled(id_insert_table_name, conn) { internal_raw_execute(sql, conn, perform_do: true) }
2121
else
2222
internal_raw_execute(sql, conn, perform_do: true)
2323
end
2424
verified!
25+
notification_payload[:row_count] = result
2526
result
2627
end
2728
end

test/cases/coerced_tests.rb

+11
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,17 @@ def test_payload_row_count_on_select_all_coerced
363363
Book.lease_connection.add_index(:books, [:author_id, :name], unique: true)
364364
end
365365

366+
# Need to remove index as SQL Server considers NULLs on a unique-index to be equal unlike PostgreSQL/MySQL/SQLite.
367+
coerce_tests! :test_payload_row_count_on_pluck
368+
def test_payload_row_count_on_pluck_coerced
369+
connection.remove_index(:books, column: [:author_id, :name])
370+
371+
original_test_payload_row_count_on_pluck
372+
ensure
373+
Book.where(author_id: nil, name: 'row count book 2').delete_all
374+
Book.lease_connection.add_index(:books, [:author_id, :name], unique: true)
375+
end
376+
366377
# Need to remove index as SQL Server considers NULLs on a unique-index to be equal unlike PostgreSQL/MySQL/SQLite.
367378
coerce_tests! :test_payload_row_count_on_raw_sql
368379
def test_payload_row_count_on_raw_sql_coerced

0 commit comments

Comments
 (0)