Browse Source

A data migration to convert webhook values to the most recent format.

pull/230/head
Pēteris Caune 6 years ago
parent
commit
9dea24e937
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
1 changed files with 60 additions and 0 deletions
  1. +60
    -0
      hc/api/migrations/0061_webhook_values.py

+ 60
- 0
hc/api/migrations/0061_webhook_values.py View File

@ -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)
]

Loading…
Cancel
Save