You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.8 KiB

  1. # Generated by Django 4.0.2 on 2022-02-22 06:30
  2. from django.db import migrations
  3. def copy_relation(apps, schema_editor):
  4. RelationTypeNew = apps.get_model("label_types", "RelationType")
  5. RelationOld = apps.get_model("labels", "RelationOld")
  6. RelationNew = apps.get_model("labels", "RelationNew")
  7. Span = apps.get_model("labels", "Span")
  8. for relation_old in RelationOld.objects.all():
  9. from_id = Span.objects.get(id=relation_old.annotation_id_1)
  10. to_id = Span.objects.get(id=relation_old.annotation_id_2)
  11. relation_type = RelationTypeNew.objects.get(project=relation_old.type.project, text=relation_old.type.name)
  12. RelationNew(
  13. from_id=from_id, to_id=to_id, user=relation_old.user, type=relation_type, example=from_id.example
  14. ).save()
  15. def delete_new_relation(apps, schema_editor):
  16. RelationNew = apps.get_model("labels", "RelationNew")
  17. RelationOld = apps.get_model("labels", "RelationOld")
  18. RelationTypeOld = apps.get_model("label_types", "RelationTypeOld")
  19. for relation in RelationNew.objects.all():
  20. relation_type, created = RelationTypeOld.objects.get_or_create(
  21. project=relation.type.project, name=relation.type.text, color=relation.type.background_color
  22. )
  23. RelationOld(
  24. annotation_id_1=relation.from_id.id,
  25. annotation_id_2=relation.to_id.id,
  26. timestamp=relation.created_at,
  27. user=relation.user,
  28. project=relation.example.project,
  29. type=relation_type,
  30. ).save()
  31. relation.delete()
  32. class Migration(migrations.Migration):
  33. dependencies = [
  34. ("labels", "0007_relationnew"),
  35. ]
  36. operations = [
  37. migrations.RunPython(code=copy_relation, reverse_code=delete_new_relation),
  38. ]