Fix migration for long names and do not include deleted (#2528)

This commit is contained in:
Michael Derynck 2023-07-13 16:32:30 -06:00 committed by GitHub
parent 0bef6efde6
commit aef162652e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,11 +16,12 @@ def convert_custom_button_to_webhook(apps, schema_editor):
Webhooks = apps.get_model("webhooks", "Webhook")
EscalationPolicies = apps.get_model("alerts", "EscalationPolicy")
for cb in CustomButton.objects.all():
# CustomButtonManager isn't used
for cb in CustomButton.objects.filter(deleted_at=None):
webhook, _ = Webhooks.objects.get_or_create(
organization=cb.organization,
team=cb.team,
name=cb.name + LEGACY_SUFFIX,
name=cb.name[:100 - len(LEGACY_SUFFIX)] + LEGACY_SUFFIX,
is_legacy=True,
defaults=dict(
created_at=cb.created_at,
@ -50,7 +51,7 @@ def undo_custom_button_to_webhook(apps, schema_editor):
for webhook in Webhooks.objects.filter(is_legacy=True):
try:
cb = CustomButton.objects.get(name=webhook.name.removesuffix(LEGACY_SUFFIX), team=webhook.team, organization=webhook.organization)
cb = CustomButton.objects.get(name__startswith=webhook.name.removesuffix(LEGACY_SUFFIX), team=webhook.team, organization=webhook.organization)
except ObjectDoesNotExist:
logger.warning(f"Did not find matching custom button to revert {webhook.name} {webhook.organization.stack_slug}, skipping")
continue