From 6dfd189f6d3a39a355c54afb12e2fbc8dd2d4a0f Mon Sep 17 00:00:00 2001 From: Julius Osokinas Date: Sun, 19 May 2019 21:06:28 +0100 Subject: [PATCH] Run migration with atomic transactions --- .../migrations/0020_update_media_and_comments_count.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/geokey/contributions/migrations/0020_update_media_and_comments_count.py b/geokey/contributions/migrations/0020_update_media_and_comments_count.py index e38768c9..68d424c2 100644 --- a/geokey/contributions/migrations/0020_update_media_and_comments_count.py +++ b/geokey/contributions/migrations/0020_update_media_and_comments_count.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from django.db import migrations, IntegrityError +from django.db import migrations, transaction, IntegrityError def update_media_and_comments_count(apps, schema_editor): @@ -8,9 +8,10 @@ def update_media_and_comments_count(apps, schema_editor): for observation in Observation.objects.all(): try: - observation.num_media = observation.files_attached.count() - observation.num_comments = observation.comments.count() - observation.save() + with transaction.atomic(): + observation.num_media = observation.files_attached.count() + observation.num_comments = observation.comments.count() + observation.save() except IntegrityError: pass