From ebc88762ba91f89bb68d3fa1f6ce2332543968c8 Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Fri, 4 Apr 2025 13:31:08 +0100 Subject: [PATCH 1/3] Redefine isolation level method as MSSSQL resets isolation after transaction --- test/cases/coerced_tests.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/cases/coerced_tests.rb b/test/cases/coerced_tests.rb index 520980672..65f98d46e 100644 --- a/test/cases/coerced_tests.rb +++ b/test/cases/coerced_tests.rb @@ -1842,6 +1842,16 @@ class TransactionIsolationTest < ActiveRecord::TestCase # I really need some help understanding this one. coerce_tests! %r{repeatable read} + + private + + # The isolation level is set twice. Once by the transaction and once when the connection is reset + # by `SQLServerRealTransaction#commit`. MySQL & PostgreSQL do not reset the connection and SQLite does support + # transaction isolation. + undef_method :assert_begin_isolation_level_event + def assert_begin_isolation_level_event(events) + assert_equal 2, events.select { _1.match(/SET TRANSACTION ISOLATION LEVEL READ COMMITTED/) }.size + end end class ViewWithPrimaryKeyTest < ActiveRecord::TestCase From 9ccf8a3df415fac2244ae5802478195c068b777d Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Fri, 4 Apr 2025 15:38:09 +0100 Subject: [PATCH 2/3] Method updated --- test/cases/coerced_tests.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/cases/coerced_tests.rb b/test/cases/coerced_tests.rb index 65f98d46e..7bffa00a0 100644 --- a/test/cases/coerced_tests.rb +++ b/test/cases/coerced_tests.rb @@ -1845,12 +1845,17 @@ class TransactionIsolationTest < ActiveRecord::TestCase private - # The isolation level is set twice. Once by the transaction and once when the connection is reset - # by `SQLServerRealTransaction#commit`. MySQL & PostgreSQL do not reset the connection and SQLite does support - # transaction isolation. + # Need to handle the resetting of the isolation level in the adapter by `SQLServerRealTransaction#commit`. + # MySQL & PostgreSQL do not reset the connection and SQLite does support transaction isolation. After that we + # can assert the number of expected isolation level events. undef_method :assert_begin_isolation_level_event - def assert_begin_isolation_level_event(events) - assert_equal 2, events.select { _1.match(/SET TRANSACTION ISOLATION LEVEL READ COMMITTED/) }.size + def assert_begin_isolation_level_event(events, isolation: "READ COMMITTED") + isolation_events = events.select { _1.match(/SET TRANSACTION ISOLATION LEVEL/) } + + reset_starting_isolation_level_event = isolation_events.delete("SET TRANSACTION ISOLATION LEVEL READ COMMITTED") + assert reset_starting_isolation_level_event.present? + + assert_equal 1, isolation_events.select { _1.match(/SET TRANSACTION ISOLATION LEVEL #{isolation}/) }.size end end From 561d605cef4b95ee3c41a43ca2955cd1d008b1f3 Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Fri, 4 Apr 2025 16:45:33 +0100 Subject: [PATCH 3/3] Fix --- test/cases/coerced_tests.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/cases/coerced_tests.rb b/test/cases/coerced_tests.rb index 7bffa00a0..0d4891a96 100644 --- a/test/cases/coerced_tests.rb +++ b/test/cases/coerced_tests.rb @@ -1852,8 +1852,9 @@ class TransactionIsolationTest < ActiveRecord::TestCase def assert_begin_isolation_level_event(events, isolation: "READ COMMITTED") isolation_events = events.select { _1.match(/SET TRANSACTION ISOLATION LEVEL/) } - reset_starting_isolation_level_event = isolation_events.delete("SET TRANSACTION ISOLATION LEVEL READ COMMITTED") - assert reset_starting_isolation_level_event.present? + index_of_reset_starting_isolation_level_event = isolation_events.index("SET TRANSACTION ISOLATION LEVEL READ COMMITTED") + assert index_of_reset_starting_isolation_level_event.present? + isolation_events.delete_at(index_of_reset_starting_isolation_level_event) assert_equal 1, isolation_events.select { _1.match(/SET TRANSACTION ISOLATION LEVEL #{isolation}/) }.size end