diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-10-26 17:05:38 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-12-12 12:23:19 +0100 |
commit | 1c88b774262c698fe9d09528834b641a67ae91e3 (patch) | |
tree | 77bd5fdb5d731d9f27bb814d97a2afbc8a1143ba /archaeological_operations/models.py | |
parent | 47700369c77b35a59fcf38d3246e57e24e84964f (diff) | |
download | Ishtar-1c88b774262c698fe9d09528834b641a67ae91e3.tar.bz2 Ishtar-1c88b774262c698fe9d09528834b641a67ae91e3.zip |
Geodata post save: transactions and targeted post save to limit deadlocks
Diffstat (limited to 'archaeological_operations/models.py')
-rw-r--r-- | archaeological_operations/models.py | 83 |
1 files changed, 55 insertions, 28 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 78ade77f8..6cbac457e 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -188,26 +188,38 @@ class GeographicTownItem(GeoItem): if q_towns_nb != 1: # no simple town - clean for geo in q_geodata_town.all(): - self.geodata.remove(geo) - if self.main_geodata == geo: - self.main_geodata = None - changed = True + try: + with transaction.atomic(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + except IntegrityError: + pass if q_towns_nb < 2: # no area - clean for geo in q_geodata_area.all(): - self.geodata.remove(geo) - if self.main_geodata == geo: - self.main_geodata = None - changed = True + try: + with transaction.atomic(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + except IntegrityError: + pass current_town_geo = None if q_towns_nb == 1: current_town_geo = q_towns.all()[0] for geo in q_geodata_town.exclude(source_id=current_town_geo.pk).all(): - self.geodata.remove(geo) - if self.main_geodata == geo: - self.main_geodata = None - changed = True + try: + with transaction.atomic(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + except IntegrityError: + pass if not q_geodata_town.filter(source_id=current_town_geo.pk).count(): self.geodata.add(current_town_geo.main_geodata) changed = True @@ -220,35 +232,50 @@ class GeographicTownItem(GeoItem): and not q_geodata_area.filter(pk=current_geo_area.pk).count() ): for geo in q_geodata_area.all(): - self.geodata.remove(geo) - if self.main_geodata == geo: - self.main_geodata = None - self.geodata.add(current_geo_area) - changed = True + try: + with transaction.atomic(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + except IntegrityError: + pass + try: + with transaction.atomic(): + self.geodata.add(current_geo_area) + changed = True + except IntegrityError: + pass if current_town_geo: q_extra_geo_town = q_geodata_town.exclude(source_id=current_town_geo.pk) if q_extra_geo_town.count(): # should not occur but bad migrations, bad imports... for geo in q_extra_geo_town.all(): - self.geodata.remove(geo) - if self.main_geodata == geo: - self.main_geodata = None - changed = True + try: + with transaction.atomic(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + except IntegrityError: + pass if current_geo_area: q_extra_geo_area = q_geodata_area.exclude(pk=current_geo_area.pk) if q_extra_geo_area.count(): # should not occur but bad migrations, bad imports... for geo in q_extra_geo_area.all(): - self.geodata.remove(geo) - if self.main_geodata == geo: - self.main_geodata = None - changed = True + try: + with transaction.atomic(): + self.geodata.remove(geo) + if self.main_geodata == geo: + self.main_geodata = None + changed = True + except IntegrityError: + pass if changed and save: - self.skip_history_when_saving = True - self._no_move = True - self.save() + post_save_geo(self.__class__, instance=self, created=False, + update_fields=False, raw=False, using="default") class ArchaeologicalSite( |