Merge pull request #5145 from jsangmeister/unique-error-fix

Error handling for race-based integrity errors
This commit is contained in:
Finn Stutzenstein 2019-11-26 14:07:34 +01:00 committed by GitHub
commit 8f9e3a2b03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,6 +17,7 @@ from django.contrib.sites.shortcuts import get_current_site
from django.core import mail from django.core import mail
from django.core.exceptions import ValidationError as DjangoValidationError from django.core.exceptions import ValidationError as DjangoValidationError
from django.db import transaction from django.db import transaction
from django.db.utils import IntegrityError
from django.http.request import QueryDict from django.http.request import QueryDict
from django.utils.encoding import force_bytes, force_text from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode
@ -99,6 +100,13 @@ class UserViewSet(ModelViewSet):
result = False result = False
return result return result
# catch IntegrityError, probably being caused by a race condition
def perform_create(self, serializer):
try:
super().perform_create(serializer)
except IntegrityError as e:
raise ValidationError({"detail": str(e)})
def update(self, request, *args, **kwargs): def update(self, request, *args, **kwargs):
""" """
Customized view endpoint to update an user. Customized view endpoint to update an user.
@ -360,7 +368,11 @@ class UserViewSet(ModelViewSet):
del data["groups_id"] del data["groups_id"]
db_user = User(**data) db_user = User(**data)
db_user.save(skip_autoupdate=True) try:
db_user.save(skip_autoupdate=True)
except IntegrityError:
# race condition may happen, so skip double users here again
continue
db_user.groups.add(*groups) db_user.groups.add(*groups)
created_users.append(db_user) created_users.append(db_user)
if "importTrackId" in user: if "importTrackId" in user: