]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
parser: prevent IntegrityErrors
authorJeremy Kerr <jk@ozlabs.org>
Thu, 16 Apr 2020 01:29:26 +0000 (09:29 +0800)
committerStephen Finucane <stephen@that.guru>
Sat, 18 Apr 2020 11:30:35 +0000 (12:30 +0100)
Currently, the parser relies on causing (and catching) IntegrityErrors
on patch insert to catch duplicate (msgid,project) mails.

This change performs an atomic select -> insert instead.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Signed-off-by: Stephen Finucane <stephen@that.guru>
[stephenfin: Remove 'expectedFailure' marker again]

patchwork/parser.py
patchwork/tests/test_parser.py

index dce03a4ff827426e2da81bb0f71934f91070aa36..021c034eb8cd2eea8bbeee54df0c0c95f481728c 100644 (file)
@@ -1069,7 +1069,10 @@ def parse_mail(mail, list_id=None):
             filenames = find_filenames(diff)
             delegate = find_delegate_by_filename(project, filenames)
 
-        try:
+        with transaction.atomic():
+            if Patch.objects.filter(project=project, msgid=msgid):
+                raise DuplicateMailError(msgid=msgid)
+
             patch = Patch.objects.create(
                 msgid=msgid,
                 project=project,
@@ -1084,8 +1087,6 @@ def parse_mail(mail, list_id=None):
                 delegate=delegate,
                 state=find_state(mail))
             logger.debug('Patch saved')
-        except IntegrityError:
-            raise DuplicateMailError(msgid=msgid)
 
         for attempt in range(1, 11):  # arbitrary retry count
             try:
index 0122fb8cfaa7ab581c03aea45b14b67b59d417cc..b640a3a1709b710b53fb92880090a866c0d80d57 100644 (file)
@@ -1138,7 +1138,6 @@ class DuplicateMailTest(TestCase):
 
         self.assertEqual(errors, [])
 
-    @unittest.expectedFailure
     def test_duplicate_patch(self):
         diff = read_patch('0001-add-line.patch')
         m = create_email(diff, listid=self.listid, msgid='1@example.com')