From ebd4c60fb3d7ea1230bcec9fb522fcc6cce66dfe Mon Sep 17 00:00:00 2001 From: Zeryab Khan Date: Thu, 10 Oct 2024 12:22:28 +0500 Subject: [PATCH 1/5] [ISSUE-3728] IntegrityError update or delete on table users_user vi olates foreign key constraint social_auth_usersocialaut --- djautotask/__init__.py | 2 +- djautotask/sync.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/djautotask/__init__.py b/djautotask/__init__.py index 3ffb321..a9788a3 100644 --- a/djautotask/__init__.py +++ b/djautotask/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -VERSION = (1, 6, 3, 'final') +VERSION = (1, 6, 4, 'final') # pragma: no cover if VERSION[-1] != "final": diff --git a/djautotask/sync.py b/djautotask/sync.py index efe7392..1f58449 100644 --- a/djautotask/sync.py +++ b/djautotask/sync.py @@ -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( @@ -450,7 +451,11 @@ 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: + instance.delete() return deleted_count @@ -1866,6 +1871,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'), From 1b972614c031e73c52bd87dd43ae9e6c24022352 Mon Sep 17 00:00:00 2001 From: Zeryab Khan Date: Fri, 11 Oct 2024 15:21:29 +0500 Subject: [PATCH 2/5] Improve Error Handling --- djautotask/sync.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/djautotask/sync.py b/djautotask/sync.py index 1f58449..5959b7f 100644 --- a/djautotask/sync.py +++ b/djautotask/sync.py @@ -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 @@ -452,11 +452,22 @@ def prune_stale_records(self, initial_ids, synced_ids): ) ) if self.bulk_prune: - delete_qset.delete() + try: + delete_qset.delete() + except IntegrityError as e: + logger.error( + 'IntegrityError while attempting to delete {} records. ' + 'Error: {}'.format(self.model_class.__bases__[0].__name__, e) + ) else: for instance in delete_qset: - instance.delete() - + try: + instance.delete() + except DatabaseError as e: + logger.error( + '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): From 05b175324e17e035968fcf84ce14f81cd84371d4 Mon Sep 17 00:00:00 2001 From: Zeryab Khan Date: Fri, 11 Oct 2024 15:32:50 +0500 Subject: [PATCH 3/5] Fix pep8 issues --- djautotask/sync.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/djautotask/sync.py b/djautotask/sync.py index 5959b7f..6d9573f 100644 --- a/djautotask/sync.py +++ b/djautotask/sync.py @@ -456,8 +456,9 @@ def prune_stale_records(self, initial_ids, synced_ids): delete_qset.delete() except IntegrityError as e: logger.error( - 'IntegrityError while attempting to delete {} records. ' - 'Error: {}'.format(self.model_class.__bases__[0].__name__, e) + 'IntegrityError while attempting to ' + 'delete {} records. Error: {}'.format( + self.model_class.__bases__[0].__name__, e) ) else: for instance in delete_qset: @@ -465,8 +466,11 @@ def prune_stale_records(self, initial_ids, synced_ids): instance.delete() except DatabaseError as e: logger.error( - 'A database error occurred while attempting to delete {} records. ' - 'Error: {}'.format(self.model_class.__bases__[0].__name__, e.__cause__) + 'A database error occurred while attempting to ' + 'delete {} records. Error: {}'.format( + self.model_class.__bases__[0].__name__, + e.__cause__ + ) ) return deleted_count From 4c88126bc06471562c050ec69b2c965516b0d2b3 Mon Sep 17 00:00:00 2001 From: Sam Wolfe Date: Fri, 11 Oct 2024 15:30:59 -0700 Subject: [PATCH 4/5] increase log level, remove trycatch block --- djautotask/sync.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/djautotask/sync.py b/djautotask/sync.py index 6d9573f..835723b 100644 --- a/djautotask/sync.py +++ b/djautotask/sync.py @@ -452,20 +452,13 @@ def prune_stale_records(self, initial_ids, synced_ids): ) ) if self.bulk_prune: - try: - delete_qset.delete() - except IntegrityError as e: - logger.error( - 'IntegrityError while attempting to ' - 'delete {} records. Error: {}'.format( - self.model_class.__bases__[0].__name__, e) - ) + delete_qset.delete() else: for instance in delete_qset: try: instance.delete() - except DatabaseError as e: - logger.error( + except IntegrityError as e: + logger.exception( 'A database error occurred while attempting to ' 'delete {} records. Error: {}'.format( self.model_class.__bases__[0].__name__, From 6035e41e384be255709ef621be7807a7df0ae452 Mon Sep 17 00:00:00 2001 From: kti-sam <34463040+kti-sam@users.noreply.github.com> Date: Fri, 11 Oct 2024 15:36:31 -0700 Subject: [PATCH 5/5] Update __init__.py --- djautotask/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/djautotask/__init__.py b/djautotask/__init__.py index a9788a3..ff3472e 100644 --- a/djautotask/__init__.py +++ b/djautotask/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -VERSION = (1, 6, 4, 'final') +VERSION = (1, 6, 5, 'final') # pragma: no cover if VERSION[-1] != "final":