]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Chore: Minor migration optimization for workflow titles (#11197)
authorTrenton H <797416+stumpylog@users.noreply.github.com>
Mon, 27 Oct 2025 20:24:57 +0000 (13:24 -0700)
committerGitHub <noreply@github.com>
Mon, 27 Oct 2025 20:24:57 +0000 (13:24 -0700)
* Makes the migration just a little more efficient

* Do it in batches, just in case

* Fixes the model klass name

src/documents/migrations/1073_migrate_workflow_title_jinja.py

index c3f929eff98c765855565fa8978585b8c75b83d6..9d80a277fde44b9a8aeb175bfec9ac0a58ba7ba9 100644 (file)
@@ -3,7 +3,6 @@ import logging
 
 from django.db import migrations
 from django.db import models
-from django.db import transaction
 
 from documents.templating.utils import convert_format_str_to_template_format
 
@@ -11,21 +10,34 @@ logger = logging.getLogger("paperless.migrations")
 
 
 def convert_from_format_to_template(apps, schema_editor):
-    WorkflowActions = apps.get_model("documents", "WorkflowAction")
-
-    with transaction.atomic():
-        for WorkflowAction in WorkflowActions.objects.all():
-            if not WorkflowAction.assign_title:
-                continue
-            WorkflowAction.assign_title = convert_format_str_to_template_format(
-                WorkflowAction.assign_title,
-            )
-            logger.debug(
-                "Converted WorkflowAction id %d title to template format: %s",
-                WorkflowAction.id,
-                WorkflowAction.assign_title,
-            )
-            WorkflowAction.save()
+    WorkflowAction = apps.get_model("documents", "WorkflowAction")
+
+    batch_size = 500
+    actions_to_update = []
+
+    queryset = (
+        WorkflowAction.objects.filter(assign_title__isnull=False)
+        .exclude(assign_title="")
+        .only("id", "assign_title")
+    )
+
+    for action in queryset:
+        action.assign_title = convert_format_str_to_template_format(
+            action.assign_title,
+        )
+        logger.debug(
+            "Converted WorkflowAction id %d title to template format: %s",
+            action.id,
+            action.assign_title,
+        )
+        actions_to_update.append(action)
+
+    if actions_to_update:
+        WorkflowAction.objects.bulk_update(
+            actions_to_update,
+            ["assign_title"],
+            batch_size=batch_size,
+        )
 
 
 class Migration(migrations.Migration):