Repository navigation
fix: rollback mail thread changes on patch creation failure #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JelteF
merged 5 commits into
postgres:main
from
jonasrochasilva-prog:fix/newpatch-thread-rollback
Oct 18, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
244cda5
fix: rollback thread changes when patch creation fails
jonasrochasilva-prog 7339eeb
Roll back the whole patch creation when attaching the thread fails
JelteF e6b1368
Trim newpatch transaction tests
FennemAI e30082c
Only turn thread attach failures into a form error
FennemAI 08716c3
Drop the underscore from ThreadAttachFailed
FennemAI File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
pgcommitfest/commitfest/tests/test_newpatch_transaction.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| from django.db import connection | ||
|
|
||
| from datetime import datetime, timezone | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from pgcommitfest.commitfest.models import MailThread, Patch | ||
|
|
||
| pytestmark = pytest.mark.django_db | ||
|
|
||
| MSGID = "test-thread@example.com" | ||
|
|
||
|
|
||
| def _fail_with_database_error(*args, **kwargs): | ||
| # A real failing query, not just a raised exception, so that PostgreSQL | ||
| # aborts the transaction and any further query in it would fail too. | ||
| with connection.cursor() as cursor: | ||
| cursor.execute("SELECT 1/0") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("fail", [False, True], ids=["success", "database-error"]) | ||
| def test_newpatch_thread_attach( | ||
| client, alice, open_cf, in_progress_cf, recent_closed_cf, fail | ||
| ): | ||
| client.force_login(alice) | ||
| message = { | ||
| "msgid": MSGID, | ||
| "date": datetime(2025, 1, 10, 12, tzinfo=timezone.utc), | ||
| "from": "author@example.com", | ||
| "subj": "[PATCH] Test", | ||
| "atts": [], | ||
| } | ||
|
|
||
| with ( | ||
| patch("pgcommitfest.commitfest.forms._archivesAPI", return_value=[message]), | ||
| patch("pgcommitfest.commitfest.ajax._archivesAPI", return_value=[message]), | ||
| patch( | ||
| "pgcommitfest.commitfest.ajax.parse_and_add_attachments", | ||
| side_effect=_fail_with_database_error if fail else None, | ||
| ) as attachments_mock, | ||
| ): | ||
| response = client.post( | ||
| f"/{open_cf.pk}/new/", | ||
| { | ||
| "name": "Transaction test", | ||
| "threadmsgid": MSGID, | ||
| "no_tags_apply": "on", | ||
| "authors": [str(alice.pk)], | ||
| }, | ||
| ) | ||
|
|
||
| attachments_mock.assert_called_once() | ||
| if fail: | ||
| # The form is re-rendered with an error and nothing is left behind | ||
| assert response.status_code == 200 | ||
| assert b"An error occurred looking up the thread" in response.content | ||
| assert not Patch.objects.filter(name="Transaction test").exists() | ||
| assert not MailThread.objects.filter(messageid=MSGID).exists() | ||
| else: | ||
| created_patch = Patch.objects.get(name="Transaction test") | ||
| assert response.status_code == 302 | ||
| assert response["Location"] == f"/patch/{created_patch.pk}/" | ||
| assert ( | ||
| MailThread.objects.get(messageid=MSGID) | ||
| .patches.filter(pk=created_patch.pk) | ||
| .exists() | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole function already has
transaction.atomic. So I'm unsure why this is needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm, I understand it now.