Skip to content

Commit

Permalink
Merge pull request #227 from KerkhoffTechnologies/3728-integrityerror…
Browse files Browse the repository at this point in the history
…-update-or-delete-on-table-users_user-violates-foreign-key-constraint

[ISSUE-3728] IntegrityError update or delete on table users_user vi
  • Loading branch information
kti-sam authored Oct 11, 2024
2 parents b488fbc + dc92bcf commit a159a22
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion djautotask/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
VERSION = (1, 6, 4, 'final')
VERSION = (1, 6, 5, 'final')

# pragma: no cover
if VERSION[-1] != "final":
Expand Down
20 changes: 17 additions & 3 deletions djautotask/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dateutil.parser import parse
from decimal import Decimal

from django.db import transaction, IntegrityError
from django.db import transaction, IntegrityError, DatabaseError
from django.db.models import Q
from django.utils import timezone

Expand Down Expand Up @@ -243,6 +243,7 @@ def get(self, results):
class Synchronizer:
lookup_key = 'id'
last_updated_field = 'lastActivityDate'
bulk_prune = True

def __init__(self, full=False, *args, **kwargs):
self.client = self.client_class(
Expand Down Expand Up @@ -450,8 +451,20 @@ def prune_stale_records(self, initial_ids, synced_ids):
len(stale_ids), self.model_class.__bases__[0].__name__,
)
)
delete_qset.delete()

if self.bulk_prune:
delete_qset.delete()
else:
for instance in delete_qset:
try:
instance.delete()
except IntegrityError as e:
logger.exception(
'A database error occurred while attempting to '
'delete {} records. Error: {}'.format(
self.model_class.__bases__[0].__name__,
e.__cause__
)
)
return deleted_count

def get_delete_qset(self, stale_ids):
Expand Down Expand Up @@ -1836,6 +1849,7 @@ class ResourceSynchronizer(Synchronizer):
client_class = api.ResourcesAPIClient
model_class = models.ResourceTracker
last_updated_field = None
bulk_prune = False

related_meta = {
'licenseType': (models.LicenseType, 'license_type'),
Expand Down

0 comments on commit a159a22

Please sign in to comment.