Skip to content
Snippets Groups Projects
Commit 86ea11f4 authored by Manon Blanco's avatar Manon Blanco
Browse files

add a reverse migration

parent e040b89e
No related branches found
No related tags found
No related merge requests found
# Generated by Django 2.2 on 2019-12-13 10:28
from django.db import migrations
from arkindex_common.enums import EntityType
from enum import Enum
def remove_entity_metatype(apps, schema_editor):
MetaData = apps.get_model('documents', 'MetaData')
if not MetaData.objects.exists():
return
class OldMetaType(Enum):
Text = 'text'
......@@ -15,17 +18,25 @@ def remove_entity_metatype(apps, schema_editor):
Reference = 'reference'
Entity = 'entity'
values = set(item.value for item in OldMetaType)
# Don't try this at home
MetaData._meta.fields[2].enum = OldMetaType
for metadata in MetaData.objects.filter(type=OldMetaType.Entity).prefetch_related('entity'):
metadata_type = OldMetaType.Text
if metadata.entity and metadata.entity.type.value in values:
metadata_type = OldMetaType(metadata.entity.type.value)
metadata.type = metadata_type
metadata.save()
MetaData.objects.filter(type=OldMetaType.Entity, entity__type=EntityType.Location).update(type=OldMetaType.Location)
MetaData.objects.filter(type=OldMetaType.Entity, entity__type=EntityType.Date).update(type=OldMetaType.Date)
MetaData.objects.filter(type=OldMetaType.Entity).update(type=OldMetaType.Text)
def add_entity_metatype(apps, schema_editor):
MetaData = apps.get_model('documents', 'MetaData')
if not MetaData.objects.exists():
return
class OldMetaType(Enum):
Entity = 'entity'
# Don't try this at home
MetaData._meta.fields[2].enum = OldMetaType
MetaData.objects.exclude(entity__isnull=True).update(type=OldMetaType.Entity)
class Migration(migrations.Migration):
......@@ -35,8 +46,5 @@ class Migration(migrations.Migration):
]
operations = [
migrations.RunPython(
remove_entity_metatype,
reverse_code=migrations.RunPython.noop
),
migrations.RunPython(remove_entity_metatype, add_entity_metatype),
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment