Skip to content

Records created outside of transaction block gets rolled back when transaction raises exception #2598

Closed
@sato11

Description

@sato11

What Ruby, Rails and RSpec versions are you using?

  • Ruby version: 3.1.2
  • Rails version: 7.0.2.3
  • RSpec versions:
    • rspec-core 3.11.0
    • rspec-expectations 3.11.0
    • rspec-mocks 3.11.1
    • rspec-rails 5.0.3
    • rspec-support 3.11.0

Observed behaviour

When the application code raises exception in .transaction, records created outside of its block, including test data, got rolled back.

Expected behaviour

Raising exception in .transaction block rolls back only the data created in the .transaction.

Background

Although I'm attaching the reproduction repo below, I bet it doesn't look very straightforward. So let me explain why here.

I came across this behavior while I was struggling with a method which looked like this (but far larger): sato11/rspec-rails-transaction-sample@169a98a. I wanted it to become this: sato11/rspec-rails-transaction-sample@5f8d90b. Because on detecting deadlock our innoDB rolls back the transaction (doc), we needed to not only rescue it but also explicitly BEGIN again. However when I wrapped .transaction in begin-end the test started to fail and that's what I'm reporting here.

I'm not really sure if it's somehow related to #use_transactional_fixtures, but I bet it's a very subtle one. I'd be glad if I could get any advice of how I can work on this or how I can write the test in a different way 🙏

Can you provide an example app?

Sure.
https://github.com/sato11/rspec-rails-transaction-sample

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions