Skip to content

Commit

Permalink
Update migration
Browse files Browse the repository at this point in the history
  • Loading branch information
yasakova-anastasia committed Feb 22, 2023
1 parent 4ea1c07 commit 0f3cba0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
14 changes: 7 additions & 7 deletions cvat/apps/engine/migrations/0064_delete_wrong_labels.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

from django.db import migrations
from cvat.apps.engine.log import get_migration_logger

Expand All @@ -15,16 +13,18 @@ def delete_wrong_labels(apps, schema_editor):

log.info('\nDeleting duplicate Labels...')
for name, parent, project, task in Label.objects.values_list("name", "parent", "project", "task").distinct():
duplicate_labels = Label.objects.filter(name=name, parent=parent, project=project).values_list('id', "parent")
duplicate_labels = Label.objects.filter(name=name, parent=parent, project=project)
if task is not None:
duplicate_labels = Label.objects.filter(name=name, parent=parent, task=task).values_list('id', "parent")
duplicate_labels = Label.objects.filter(name=name, parent=parent, task=task)

if len(duplicate_labels) > 1:
label = duplicate_labels[0]
if label[1] is not None:
Label.objects.get(pk=label[1]).delete()
if label.parent is not None:
label.delete()
else:
Label.objects.get(pk=label[0]).delete()
for i, label in enumerate(duplicate_labels[1:]):
label.name = f"{label.name}_duplicate_{i + 1}"
label.save()

class Migration(migrations.Migration):

Expand Down
5 changes: 2 additions & 3 deletions cvat/apps/engine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,9 @@ def __str__(self):
def has_parent_label(self):
return bool(self.parent)

def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
def save(self, *args, **kwargs):
try:
super().save(force_insert, force_update, using, update_fields)
super().save(*args, **kwargs)
except IntegrityError:
raise InvalidLabel("All label names must be unique")

Expand Down

0 comments on commit 0f3cba0

Please sign in to comment.