diff --git a/apps/ops/migrations/0003_alter_adhoc_unique_together_and_more.py b/apps/ops/migrations/0003_alter_adhoc_unique_together_and_more.py index cbe7233a1..98087c86a 100644 --- a/apps/ops/migrations/0003_alter_adhoc_unique_together_and_more.py +++ b/apps/ops/migrations/0003_alter_adhoc_unique_together_and_more.py @@ -8,26 +8,19 @@ from orgs.models import Organization def migrate_ops_adhoc_and_playbook_name(apps, schema_editor): Adhoc = apps.get_model('ops', 'adhoc') Playbook = apps.get_model('ops', 'playbook') + Organization = apps.get_model('orgs', 'Organization') + org_id_name_mapper = {str(org.id): org.name for org in Organization.objects.all()} + adhocs_to_update = Adhoc.objects.exclude(org_id=Organization.DEFAULT_ID) for adhoc in adhocs_to_update: - try: - org = Organization.objects.get(id=adhoc.org_id) - suffix = f'({org.name})' - except Exception as e: - suffix = f'({str(adhoc.id)[:6]})' - - adhoc.name = f'{adhoc.name}{suffix}' + suffix = org_id_name_mapper.get(str(adhoc.org_id), str(adhoc.id)[:6]) + adhoc.name = f'{adhoc.name} ({suffix})' Adhoc.objects.bulk_update(adhocs_to_update, ['name']) playbooks_to_update = Playbook.objects.exclude(org_id=Organization.DEFAULT_ID) for playbook in playbooks_to_update: - try: - org = Organization.objects.get(id=playbook.org_id) - suffix = f'({org.name})' - except Exception as e: - suffix = f'({str(playbook.id)[:6]})' - playbook.name = f'{playbook.name}{suffix}' - playbook.save() + suffix = org_id_name_mapper.get(str(playbook.org_id), str(playbook.id)[:6]) + playbook.name = f'{playbook.name} ({suffix})' Playbook.objects.bulk_update(playbooks_to_update, ['name'])