@@ -0,0 +1,1441 @@
+from django.core.management.base import BaseCommand%0Afrom hs_core.models import Relation, BaseResource%0A%0A%0Aclass Command(BaseCommand):%0A    help = %22Cleanup broken/dangling resources resulting from versioning issues, e.g., #3276 and #4028%22%0A%0A    def handle(self, *args, **options):%0A        replace_by_qs = Relation.objects.filter(type='isReplacedBy')%0A        msg = f'Before cleanup, there are currently %7Breplace_by_qs.count()%7D isReplacedBy relations'%0A        print(msg, flush=True)%0A%0A        for obj in replace_by_qs:%0A            res_id = obj.value.split('/')%5B-1%5D%0A            if not BaseResource.objects.filter(short_id=res_id).exists():%0A                obj.delete()%0A%0A        replace_by_qs = Relation.objects.filter(type='isReplacedBy')%0A        msg = f'After cleanup, there are currently %7Breplace_by_qs.count()%7D isReplacedBy relations'%0A        print(msg, flush=True)%0A%0A        version_of_qs = Relation.objects.filter(type='isVersionOf')%0A        msg = f'Before cleanup, there are currently %7Breplace_by_qs.count()%7D isVersionOf relations'%0A        print(msg, flush=True)%0A%0A        for obj in version_of_qs:%0A            res_id = obj.value.split('/')%5B-1%5D%0A            if not BaseResource.objects.filter(short_id=res_id).exists():%0A                obj.delete()%0A%0A        version_of_qs = Relation.objects.filter(type='isVersionOf')%0A        msg = f'After cleanup, there are currently %7Bversion_of_qs.count()%7D isVersionOf relations'%0A        print(msg, flush=True)%0A

remove nonexistent resources from versioning metadata elements