Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass metadata in ActiveRecord::Result #38660

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,14 @@ def arel_visitor

def build_statement_pool
end

def metadata
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those who wanted to leverage metadata support would patch or subclass from another adapter, for instance:

class MyAdapter < ActiveRecord::ConnectionAdapters::Mysql2Adapter
  def metadata
    system_variables = @connection.session_track(Mysql2::Client::SESSION_TRACK_SYSTEM_VARIABLES)
    return unless system_variables
    JSON.parse(system_variables)
  end
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the abstract implementation default to returning an empty hash?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea. Would you prefer that we freeze an empty hash and use that as a default to reduce allocations?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I'd do, but I'm not familiar enough with the design of this part of Rails to know wether it would be expected or not.

Also if the default is frozen, it would be preferable that the actual implementation is frozen as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated 👍

ActiveRecord::Result::DEFAULT_EMPTY_METADATA
end

def build_result(columns, rows, column_types = {})
ActiveRecord::Result.new(columns, rows, column_types, metadata)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ def exec_query(sql, name = "SQL", binds = [], prepare: false)
if without_prepared_statement?(binds)
execute_and_free(sql, name) do |result|
if result
ActiveRecord::Result.new(result.fields, result.to_a)
build_result(result.fields, result.to_a)
else
ActiveRecord::Result.new([], [])
build_result([], [])
end
end
else
exec_stmt_and_free(sql, name, binds, cache_stmt: prepare) do |_, result|
if result
ActiveRecord::Result.new(result.fields, result.to_a)
build_result(result.fields, result.to_a)
else
ActiveRecord::Result.new([], [])
build_result([], [])
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def exec_query(sql, name = "SQL", binds = [], prepare: false)
fmod = result.fmod i
types[fname] = get_oid_type(ftype, fmod, fname)
end
ActiveRecord::Result.new(fields, result.values, types)
build_result(fields, result.values, types)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def exec_query(sql, name = nil, binds = [], prepare: false)
records = stmt.to_a
end

ActiveRecord::Result.new(cols, records)
build_result(cols, records)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/querying.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def find_by_sql(sql, binds = [], preparable: nil, &block)

payload = {
record_count: result_set.length,
class_name: name
class_name: name,
result_set: result_set
}

message_bus.instrument("instantiation.active_record", payload) do
Expand Down
7 changes: 5 additions & 2 deletions activerecord/lib/active_record/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ module ActiveRecord
class Result
include Enumerable

attr_reader :columns, :rows, :column_types
DEFAULT_EMPTY_METADATA = {}.freeze

def initialize(columns, rows, column_types = {})
attr_reader :columns, :rows, :column_types, :metadata

def initialize(columns, rows, column_types = {}, metadata = DEFAULT_EMPTY_METADATA)
@columns = columns
@rows = rows
@hash_rows = nil
@column_types = column_types
@metadata = metadata.freeze
end

# Returns true if this result set includes the column named +name+
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ def test_exec_query_returns_an_empty_result
assert_instance_of(ActiveRecord::Result, result)
end

def test_exec_query_returns_result_with_empty_metadata
result = @connection.exec_query "SELECT 1"
assert_instance_of(ActiveRecord::Result, result)
assert_equal({}, result.metadata)
assert_predicate(result.metadata, :frozen?)
end

if current_adapter?(:Mysql2Adapter)
def test_charset
assert_not_nil @connection.charset
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/cases/associations/eager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ def test_base_messages

assert_equal Developer.all.to_a.count, payload[:record_count]
assert_equal Developer.name, payload[:class_name]
assert_instance_of ActiveRecord::Result, payload[:result_set]
end

def messages_for(name)
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/result_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,15 @@ def result

assert_equal [[1.1, 2.2], [3.3, 4.4]], result.cast_values("col1" => Type::Float.new)
end

test "accepts optional metadata" do
result = Result.new([], [])
assert_equal({}, result.metadata)
assert_predicate(result.metadata, :frozen?)

result = Result.new([], [], {}, { foo: :bar })
assert_equal({ foo: :bar }, result.metadata)
assert_predicate(result.metadata, :frozen?)
end
end
end