Skip to content

Simplify error behaviour of transaction_if_not_already#156

Merged
meshy merged 4 commits intomainfrom
meshy/transaction_if_not_already-simplification
May 6, 2026
Merged

Simplify error behaviour of transaction_if_not_already#156
meshy merged 4 commits intomainfrom
meshy/transaction_if_not_already-simplification

Conversation

@meshy
Copy link
Copy Markdown
Collaborator

@meshy meshy commented May 1, 2026

Before this change, errors raised through transaction_if_not_already would invalidate the state of any outer transaction (other than a test's transaction).

That was a side-effect of the fact that we used atomic(savepoint=False) in that case.

This avoids that pitfall by using contextlib.nullcontext when a transaction is already open. The logic of transaction_if_not_already becomes easier to understand as a result.

Because this changes production behaviour, this change will require a 2.0 release.
Edit: As per review comments, we will not make a 2.0 release as a result of this change.

meshy and others added 3 commits May 1, 2026 17:43
The targets of these comments were not previously clear.

Co-authored-by: Lily <code@lilyf.org>
Before this change, `transaction_if_not_already` repeated the call to
`_execute_on_commit_callbacks_in_tests` which could be found in
`transaction`.

This now delegates transaction-closing emulation to `transaction`.

Co-authored-by: Lily <code@lilyf.org>
Before this change, errors raised through `transaction_if_not_already`
would invalidate the state of any outer transaction (other than a test's
transaction). This was a side-effect of the fact that we used
`atomic(savepoint=False)` in that case.

By using `contextlib.nullcontext` when a transaction is already open, we
avoid that pitfall.

Co-authored-by: Lily <code@lilyf.org>
@meshy meshy marked this pull request as ready for review May 1, 2026 17:16
@meshy meshy requested a review from a team as a code owner May 1, 2026 17:16
Comment thread src/django_subatomic/db.py Outdated
This is functionally the same, but avoids a function call.

Co-authored-by: Lily <code@lilyf.org>
Copy link
Copy Markdown
Collaborator

@samueljsb samueljsb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a sensible bugfix to me.

I don't think this justifies a major version bump: we're not changing the API, nor the expectations about what this function will do. All changes to the code will change behaviour -- that doesn't mean every release needs to bump the major version.

@meshy meshy merged commit 989254e into main May 6, 2026
8 checks passed
@meshy meshy deleted the meshy/transaction_if_not_already-simplification branch May 6, 2026 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants