From f4eb840b82fced6fccb4e78e4122841adf2b376e Mon Sep 17 00:00:00 2001 From: Jeff Ortel Date: Tue, 8 Jan 2019 13:05:38 -0600 Subject: [PATCH] Fix BulkCreateManager.bulk_get_or_create() closes #4306 --- pulpcore/app/models/content.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pulpcore/app/models/content.py b/pulpcore/app/models/content.py index 8c1de41bf9..234d4182df 100644 --- a/pulpcore/app/models/content.py +++ b/pulpcore/app/models/content.py @@ -4,7 +4,7 @@ import hashlib from django.core import validators -from django.db import IntegrityError, models +from django.db import IntegrityError, models, transaction from django.forms.models import model_to_dict from itertools import chain @@ -39,19 +39,21 @@ def bulk_get_or_create(self, objs, batch_size=None): """ objs = list(objs) try: - return super().bulk_create(objs, batch_size=batch_size) + with transaction.atomic(): + return super().bulk_create(objs, batch_size=batch_size) except IntegrityError: for i in range(len(objs)): try: - objs[i].save() + with transaction.atomic(): + objs[i].save() except IntegrityError: objs[i] = objs[i].__class__.objects.get(objs[i].q()) return objs -class QueryMixin(): +class QueryMixin: """ - A mixin that provides models with querying utilities + A mixin that provides models with querying utilities. """ def q(self):