rails/activerecord/test
Matthew Draper fadb6830f8 Harden the .current_transaction API
Based on https://github.com/rails/rails/pull/52017

One concern raised by Xavier is users holding on the return value
of `.current_transaction` beyond the point where it is committed /
rolled back / invalidated.

I believe this is an invalid use of the API, just like holding
`ActiveRecord::Base.connection` beyond the scope of a request is.

However we can be more explicit about it, so I changed the callback
registration methods to raise an error when called on a finalized
transaction.

Another concern was the usability of the null-object in the Active
Record notification payloads, and I agree that while the null-object
make sense when calling `Model.current_transaction`, it doesn't make
sense to include it in the payload of events. The goal of the
`.current_transaction` API is to allow implementing transaction aware
code in a streamlined way. The goal of the `:transaction` in events
however it to allow logging whether a query was inside a transaction
or not, so it's much more ergonomic for it to be nilable.
So I kept Matthew's change that passes `transaction: nil` in `sql.active_record` events
when not inside a transaction. I also added test coverage to make
sure it behaves consistently whether we're inside a transactional
test or not.

I also kept the separation between internal and "user" transaction
objects, as I think it's a nice way to limit the effectively exposed
API, and prevent users from abusing that API too much.

Co-Authored-By: Jean Boussier <jean.boussier@gmail.com>
2024-06-13 09:34:58 +02:00
..
active_record/connection_adapters Add deprecation and specific exceptions to warn when an adapter is using the legacy registration format in 7.2 2023-11-30 07:00:38 -05:00
activejob Merge pull request #51409 from fatkodima/fix-destroy_async-job-for-cpk 2024-05-02 16:57:50 +02:00
assets Replace test Man with Human 2020-08-14 11:37:09 -04:00
cases Harden the .current_transaction API 2024-06-13 09:34:58 +02:00
fixtures Add the ability to ignore counter cache columns while they are backfilling 2024-04-02 13:59:46 +03:00
migrations Sort migration ID as int in db:migrate:status for consistency. 2021-07-16 13:10:31 -05:00
models Make the Relation -> Model delegation stricter 2024-05-28 08:13:23 +02:00
schema Fix non-partial inserts for models with composite identity primary keys 2024-05-18 12:08:33 +03:00
support Deprecate ConnectionPool#connection 2024-03-01 14:32:55 +01:00
config.example.yml Remove support for SQLServerAdapter, OracleAdapter and JBDC adapters in tests 2024-05-14 14:56:16 -04:00
config.rb Delete AS::Dependencies.safe_constantize 2021-08-20 17:51:05 +02:00