diff mbox series

[3/5] parser: prevent IntegrityErrors

Message ID 20200416012928.23893-4-jk@ozlabs.org
State Accepted
Headers show
Series Prevent database error logs on duplicate mail | expand

Commit Message

Jeremy Kerr April 16, 2020, 1:29 a.m. UTC
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>
---
 patchwork/parser.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
diff mbox series

Patch

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: