Skip to content

Commit

Permalink
Return row-count for pluck (#1171)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan authored May 14, 2024
1 parent b6257ad commit ae733fd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ 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) }
else
internal_raw_execute(sql, conn, perform_do: true)
end
verified!
notification_payload[:row_count] = result
result
end
end
Expand Down
11 changes: 11 additions & 0 deletions test/cases/coerced_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ae733fd

Please sign in to comment.