testing migration of relation metadata for CompositeResource only
import logging
from django.contrib.contenttypes.models import ContentType
-from django.db import migrations, transaction
+from django.db import migrations
# from hs_core.models import BaseResource
from hs_core.hydroshare.utils import set_dirty_bag_flag
@@ -10,15 +10,15 @@ from hs_core.hydroshare.utils import set_dirty_bag_flag
def migrate_relation_meta(apps, schema_editor):
"""Migrates the Source metadata element to the Relation metadata element using
relation type as 'source'. Migrates some old relation types to new types"""
-
log = logging.getLogger()
- BaseResource = apps.get_model("hs_core", "BaseResource")
+
+ def migrate(app_name, resource_cls_name):
+ Resource = apps.get_model(app_name, resource_cls_name)
Relation = apps.get_model("hs_core", "Relation")
Source = apps.get_model("hs_core", "Source")
- transaction.set_autocommit(False)
+
try:
- for res in BaseResource.objects.all().iterator():
- res = res.get_content_model()
+ for res in Resource.objects.all().iterator():
type_migrated = False
# migrate relation types
metadata_type = ContentType.objects.get_for_model(res.metadata)
@@ -99,12 +99,13 @@ def migrate_relation_meta(apps, schema_editor):
log_msg = log_msg.format(res.short_id, res.resource_type)
log.info(log_msg)
print(log_msg)
- transaction.commit()
except Exception as err:
err_msg = "Custom migration of relation metadata failed. Error:{}".format(str(err))
print(err_msg)
log.error(err_msg)
+ migrate(app_name="hs_composite_resource", resource_cls_name="CompositeResource")
+
class Migration(migrations.Migration):
