[Patchew-devel] [PATCH 4/5] models: remove messages for which the blobs were lost

Paolo Bonzini posted 5 patches 2 years, 6 months ago
[Patchew-devel] [PATCH 4/5] models: remove messages for which the blobs were lost
Posted by Paolo Bonzini 2 years, 6 months ago
Messages that do not have an associated blob (typically because
their message-id included a slash) have never worked.  Remove
them from the database.

This migration can also be performed directly on the database
while the server is running:

update api_topic set latest_id = null where latest_id in (select id from api_message where mbox_bytes is null);
delete from api_messageresult where message_id in (select id from api_message where mbox_bytes is null);
delete from api_message where mbox_bytes is null;
delete from api_topic where not exists (select * from api_message where api_message.topic_id = api_topic.id);

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 api/migrations/0063_remove_broken_messages.py | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 api/migrations/0063_remove_broken_messages.py

diff --git a/api/migrations/0063_remove_broken_messages.py b/api/migrations/0063_remove_broken_messages.py
new file mode 100644
index 0000000..d160e01
--- /dev/null
+++ b/api/migrations/0063_remove_broken_messages.py
@@ -0,0 +1,19 @@
+#! /usr/bin/env python3
+from __future__ import unicode_literals
+
+from django.db import migrations, transaction
+
+def remove_null_mbox_messages(apps, schema_editor):
+    Message = apps.get_model("api", "Message")
+    Topic = apps.get_model("api", "Topic")
+    Topic.objects.filter(latest__mbox_bytes=None).update(latest=None)
+    Message.objects.filter(mbox_bytes=None).delete()
+
+class Migration(migrations.Migration):
+
+    dependencies = [("api", "0062_deblob_messages")]
+
+    operations = [
+        migrations.RunPython(remove_null_mbox_messages, reverse_code=migrations.RunPython.noop)
+    ]
+
-- 
2.35.1


_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/patchew-devel