[Patchew-devel] [PATCH 2/5] models: change ad hoc deblobbing script to migration

Paolo Bonzini posted 5 patches 2 years, 8 months ago
[Patchew-devel] [PATCH 2/5] models: change ad hoc deblobbing script to migration
Posted by Paolo Bonzini 2 years, 8 months ago
The deblobbing script was written to be invoked while the server is running.
Change it to a migration for the sake of running it in the developer setup.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 .../migrations/0062_deblob_messages.py        | 25 +++++++++++++++----
 1 file changed, 20 insertions(+), 5 deletions(-)
 rename scripts/blob_migrate.py => api/migrations/0062_deblob_messages.py (51%)

diff --git a/scripts/blob_migrate.py b/api/migrations/0062_deblob_messages.py
similarity index 51%
rename from scripts/blob_migrate.py
rename to api/migrations/0062_deblob_messages.py
index ee7ff73..22ee12f 100644
--- a/scripts/blob_migrate.py
+++ b/api/migrations/0062_deblob_messages.py
@@ -1,9 +1,13 @@
 #! /usr/bin/env python3
+from __future__ import unicode_literals
 
-from api.models import Message, Project
-from django.db import transaction
+from django.db import migrations, transaction
 
-def doit(n=1000):
+from api import blobs
+
+def deblob_messages(apps, schema_editor):
+    Project = apps.get_model("api", "Project")
+    Message = apps.get_model("api", "Message")
     done = 0
     for p in Project.objects.all():
         start = Message.objects.filter(project=p, mbox_bytes=None).order_by("-date").first()
@@ -12,14 +16,25 @@ def doit(n=1000):
             print(done, p, first_date)
             with transaction.atomic():
                 previously = done
-                q = Message.objects.filter(project=p, date__lte=first_date, mbox_bytes=None).order_by("-date")[:n]
+                q = Message.objects.filter(project=p, date__lte=first_date, mbox_bytes=None).order_by("-date")[:1000]
                 for msg in q:
                     try:
-                        msg.save_mbox(msg.mbox)
+                        mbox_decoded = blobs.load_blob(msg.message_id)
+                        msg.mbox_bytes = mbox_decoded.encode("utf-8")
                         msg.save()
+                        blobs.delete_blob(msg.message_id)
                         done += 1
                     except Exception as e:
                         print(msg, type(e))
                     start = msg
                 if done == previously and start.date == first_date:
                     start = None
+
+class Migration(migrations.Migration):
+
+    dependencies = [("api", "0061_message_mbox_bytes")]
+
+    operations = [
+        migrations.RunPython(deblob_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