diff --git a/hc/api/migrations/0061_webhook_values.py b/hc/api/migrations/0061_webhook_values.py new file mode 100644 index 00000000..c7134b8d --- /dev/null +++ b/hc/api/migrations/0061_webhook_values.py @@ -0,0 +1,60 @@ +# Generated by Django 2.2.1 on 2019-05-29 20:37 + +import json + +from django.db import migrations + + +def normalize_webhook_values(apps, schema_editor): + Channel = apps.get_model("api", "Channel") + for ch in Channel.objects.filter(kind="webhook").only("value"): + # The old format of url_down, url_up, post_data separated by newlines: + if not ch.value.startswith("{"): + parts = ch.value.split("\n") + url_down = parts[0] + url_up = parts[1] if len(parts) > 1 else "" + post_data = parts[2] if len(parts) > 2 else "" + + ch.value = json.dumps( + { + "method_down": "POST" if post_data else "GET", + "url_down": url_down, + "body_down": post_data, + "headers_down": {}, + "method_up": "POST" if post_data else "GET", + "url_up": url_up, + "body_up": post_data, + "headers_up": {}, + } + ) + + ch.save() + continue + + doc = json.loads(ch.value) + # Legacy "post_data" in doc -- use the legacy fields + if "post_data" in doc: + ch.value = json.dumps( + { + "method_down": "POST" if doc["post_data"] else "GET", + "url_down": doc["url_down"], + "body_down": doc["post_data"], + "headers_down": doc["headers"], + "method_up": "POST" if doc["post_data"] else "GET", + "url_up": doc["url_up"], + "body_up": doc["post_data"], + "headers_up": doc["headers"], + } + ) + + ch.save() + continue + + +class Migration(migrations.Migration): + + dependencies = [("api", "0060_tokenbucket")] + + operations = [ + migrations.RunPython(normalize_webhook_values, migrations.RunPython.noop) + ]