aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2019-01-29 13:29:27 +0100
committerJean Boussier <jean.boussier@gmail.com>2019-01-29 16:10:47 +0100
commit74dbce0fcad322ae958abc0a1e00e78a519710a1 (patch)
tree03034feaa2bca5ab5e92812fa3cdc1e213fb899a /activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
parent8309cd2c68f548987b8447475c7735a19714baaa (diff)
downloadrails-74dbce0fcad322ae958abc0a1e00e78a519710a1.tar.gz
rails-74dbce0fcad322ae958abc0a1e00e78a519710a1.tar.bz2
rails-74dbce0fcad322ae958abc0a1e00e78a519710a1.zip
Eagerly materialize the fixtures transaction
The transaction used to restore fixtures is an implementation detail that should be abstracted away. Idealy a test should behave the same wether or not transactional fixtures are enabled. However since transactions have been made lazy, the fixture transaction started leaking into tests case. e.g. consider the following (oversimplified) test: ```ruby class SQLSubscriber attr_accessor :sql def initialize @sql = [] end def call(*, event) sql << event[:sql] end end subscriber = SQLSubscriber.new ActiveSupport::Notifications.subscribe("sql.active_record", subscriber) User.connection.execute('SELECT 1', 'Generic name') assert_equal ['SELECT 1'], subscriber.sql ``` On Rails 6 it starts to break because the `sql` array will be `['BEGIN', 'SELECT 1']`. Several things are wrong here: - That transaction is not generated by the tested code, so it shouldn't be visible. - The transaction is not even closed yet, which again doesn't reflect the reality.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract/transaction.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/transaction.rb7
1 files changed, 5 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
index 112f376d0a..c9e84e48cc 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
@@ -205,9 +205,12 @@ module ActiveRecord
run_commit_callbacks: run_commit_callbacks)
end
- transaction.materialize! unless @connection.supports_lazy_transactions? && lazy_transactions_enabled?
+ if @connection.supports_lazy_transactions? && lazy_transactions_enabled? && options[:_lazy] != false
+ @has_unmaterialized_transactions = true
+ else
+ transaction.materialize!
+ end
@stack.push(transaction)
- @has_unmaterialized_transactions = true if @connection.supports_lazy_transactions?
transaction
end
end