summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
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
commite14b4f67e20b5ea8d6a3fd4d4b67064a7251abb4 (patch)
tree77bd5fdb5d731d9f27bb814d97a2afbc8a1143ba
parent13a3311ff045b53dbdced2b03dce7295d3c1a398 (diff)
downloadIshtar-e14b4f67e20b5ea8d6a3fd4d4b67064a7251abb4.tar.bz2
Ishtar-e14b4f67e20b5ea8d6a3fd4d4b67064a7251abb4.zip
Geodata post save: transactions and targeted post save to limit deadlocks
-rw-r--r--archaeological_context_records/models.py15
-rw-r--r--archaeological_operations/models.py83
2 files changed, 64 insertions, 34 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py
index 97a629d05..55b101acc 100644
--- a/archaeological_context_records/models.py
+++ b/archaeological_context_records/models.py
@@ -23,10 +23,9 @@ import uuid
from django.apps import apps
from django.conf import settings
from django.contrib.gis.db import models
-from django.contrib.gis.geos import Point
from django.contrib.postgres.indexes import GinIndex
from django.contrib.sites.models import Site
-from django.db import connection
+from django.db import connection, IntegrityError, transaction
from django.db.models import Q
from django.db.models.signals import post_delete, post_save, m2m_changed
from django.urls import reverse
@@ -458,11 +457,15 @@ class GeographicSubTownItem(GeoItem):
return
if not q_geodata_current_town.filter(source_id=town.id).count():
- self.geodata.add(town.main_geodata)
+ try:
+ # multiple save, post treatments can cause synchronous add
+ with transaction.atomic():
+ self.geodata.add(town.main_geodata)
+ except IntegrityError:
+ pass
if 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")
else:
return True
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(