Skip to content

Commit

Permalink
Run migration with atomic transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
josokinas committed May 19, 2019
1 parent 55e9ae0 commit 6dfd189
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# -*- 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):
Observation = apps.get_model('contributions', 'Observation')

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

Expand Down

0 comments on commit 6dfd189

Please sign in to comment.