Browse Source

perf: fix migrate adhoc playbook

pull/14132/head
Bai 2 months ago committed by Bryan
parent
commit
542e64278f
  1. 21
      apps/ops/migrations/0003_alter_adhoc_unique_together_and_more.py

21
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): def migrate_ops_adhoc_and_playbook_name(apps, schema_editor):
Adhoc = apps.get_model('ops', 'adhoc') Adhoc = apps.get_model('ops', 'adhoc')
Playbook = apps.get_model('ops', 'playbook') 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) adhocs_to_update = Adhoc.objects.exclude(org_id=Organization.DEFAULT_ID)
for adhoc in adhocs_to_update: for adhoc in adhocs_to_update:
try: suffix = org_id_name_mapper.get(str(adhoc.org_id), str(adhoc.id)[:6])
org = Organization.objects.get(id=adhoc.org_id) adhoc.name = f'{adhoc.name} ({suffix})'
suffix = f'({org.name})'
except Exception as e:
suffix = f'({str(adhoc.id)[:6]})'
adhoc.name = f'{adhoc.name}{suffix}'
Adhoc.objects.bulk_update(adhocs_to_update, ['name']) Adhoc.objects.bulk_update(adhocs_to_update, ['name'])
playbooks_to_update = Playbook.objects.exclude(org_id=Organization.DEFAULT_ID) playbooks_to_update = Playbook.objects.exclude(org_id=Organization.DEFAULT_ID)
for playbook in playbooks_to_update: for playbook in playbooks_to_update:
try: suffix = org_id_name_mapper.get(str(playbook.org_id), str(playbook.id)[:6])
org = Organization.objects.get(id=playbook.org_id) playbook.name = f'{playbook.name} ({suffix})'
suffix = f'({org.name})'
except Exception as e:
suffix = f'({str(playbook.id)[:6]})'
playbook.name = f'{playbook.name}{suffix}'
playbook.save()
Playbook.objects.bulk_update(playbooks_to_update, ['name']) Playbook.objects.bulk_update(playbooks_to_update, ['name'])

Loading…
Cancel
Save