mirror of
https://github.com/healthchecks/healthchecks.git
synced 2025-04-07 22:25:35 +00:00
Add data migration to move Check.body -> Check.body_raw
We used "body" to store request body as text. In 2022 we added "body_raw" and started to use it to store request body as bytes. In python code we currently need to inspect both fields, because the data could be in "body" (for old pings) or in "body_raw" (for newer pings). My plan is to eventually get rid of the "body" field, and have "body_raw" only. This data migration is a step towards that: for any Ping objects that have non-empty "body" field, it moves the data to the "body_raw" field. After applying this migration, the "body" field should be empty (empty string or null) for all Ping objects.
This commit is contained in:
parent
bc8fb90fed
commit
daaee30c88
1 changed files with 28 additions and 0 deletions
28
hc/api/migrations/0108_move_body_to_body_raw.py
Normal file
28
hc/api/migrations/0108_move_body_to_body_raw.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
# Generated by Django 5.0.7 on 2024-07-11 10:43
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from django.apps.registry import Apps
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def move_body_to_body_raw(apps: Apps, schema_editor: Any) -> None:
|
||||
Ping = apps.get_model("api", "Ping")
|
||||
i = 0
|
||||
for ping in Ping.objects.exclude(body="").exclude(body=None):
|
||||
Ping.objects.filter(id=ping.id).update(body_raw=ping.body.encode(), body=None)
|
||||
i += 1
|
||||
if i % 1000 == 0:
|
||||
print(i)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("api", "0107_fix_legacy_timezones"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(move_body_to_body_raw, migrations.RunPython.noop)
|
||||
]
|
Loading…
Add table
Reference in a new issue