Skip to content

Commit

Permalink
Include the current transaction in sql.active_record event payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed May 31, 2024
1 parent 70d153d commit ee3d663
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ def cache_notification_info(sql, name, binds)
type_casted_binds: -> { type_casted_binds(binds) },
name: name,
connection: self,
transaction: current_transaction,
cached: true
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,7 @@ def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name =
statement_name: statement_name,
async: async,
connection: self,
transaction: current_transaction,
row_count: 0,
&block
)
Expand Down
31 changes: 31 additions & 0 deletions activerecord/test/cases/instrumentation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,36 @@ def test_payload_connection_with_query_cache_enabled
ensure
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end

def test_payload_with_implicit_transaction
expected_transaction = Book.current_transaction

subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |event|
if event.payload[:name] == "Book Count"
assert_same expected_transaction, event.payload[:transaction]
end
end

Book.count
ensure
ActiveSupport::Notifications.unsubscribe(subscriber)
end

def test_payload_with_explicit_transaction
expected_transaction = nil

subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |event|
if event.payload[:name] == "Book Load"
assert_same expected_transaction, event.payload[:transaction]
end
end

Book.transaction do |transaction|
expected_transaction = transaction
Book.first
end
ensure
ActiveSupport::Notifications.unsubscribe(subscriber)
end
end
end
36 changes: 36 additions & 0 deletions activerecord/test/cases/query_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1067,4 +1067,40 @@ def test_query_cache_lru_eviction

assert_equal @connection_1, @connection_2
end

test "payload with implicit transaction" do
expected_transaction = Task.current_transaction

subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |event|
if event.payload[:cached]
assert_same expected_transaction, event.payload[:transaction]
end
end

Task.cache do
2.times { Task.count }
end
ensure
ActiveSupport::Notifications.unsubscribe(subscriber)
end

test "payload with explicit transaction" do
expected_transaction = nil

subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |event|
if event.payload[:cached]
assert_same expected_transaction, event.payload[:transaction]
end
end

Task.transaction do |transaction|
expected_transaction = transaction

Task.cache do
2.times { Task.count }
end
end
ensure
ActiveSupport::Notifications.unsubscribe(subscriber)
end
end
6 changes: 6 additions & 0 deletions guides/source/active_support_instrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ The `:cache_hits` key is only included if the collection is rendered with `cache
| `:sql` | SQL statement |
| `:name` | Name of the operation |
| `:connection` | Connection object |
| `:transaction` | Current transaction |
| `:binds` | Bind parameters |
| `:type_casted_binds` | Typecasted bind parameters |
| `:statement_name` | SQL Statement name |
Expand All @@ -374,13 +375,18 @@ Adapters may add their own data as well.
sql: "SELECT \"posts\".* FROM \"posts\" ",
name: "Post Load",
connection: <ActiveRecord::ConnectionAdapters::SQLite3Adapter:0x00007f9f7a838850>,
transaction: <ActiveRecord::ConnectionAdapters::RealTransaction:0x0000000121b5d3e0>
binds: [<ActiveModel::Attribute::WithCastValue:0x00007fe19d15dc00>],
type_casted_binds: [11],
statement_name: nil,
row_count: 5
}
```

If there is no transaction started at the moment, `:transaction` has a null
object with UUID `00000000-0000-0000-0000-000000000000` (the nil UUID). This may
happen, for example, issuing a `SELECT` not wrapped in a transaction.

#### `strict_loading_violation.active_record`

This event is only emitted when [`config.active_record.action_on_strict_loading_violation`][] is set to `:log`.
Expand Down

0 comments on commit ee3d663

Please sign in to comment.