[PATCH 3/5] parser: prevent IntegrityErrors

Jeremy Kerr jk at ozlabs.org
Thu Apr 16 11:29:26 AEST 2020


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 at ozlabs.org>
---
 patchwork/parser.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/patchwork/parser.py b/patchwork/parser.py
index a09fd75..e03634a 100644
--- a/patchwork/parser.py
+++ b/patchwork/parser.py
@@ -1062,7 +1062,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,
@@ -1077,8 +1080,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:
-- 
2.17.1



More information about the Patchwork mailing list