Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

migrations: Add progress indicator for imageattachment backfill.

Tim Abbottcc110cd7e463
diff --git a/zerver/migrations/0576_backfill_imageattachment.py b/zerver/migrations/0576_backfill_imageattachment.py
index 9bcfaac3583..677c992a3ca 100644
--- a/zerver/migrations/0576_backfill_imageattachment.py
+++ b/zerver/migrations/0576_backfill_imageattachment.py
@@ -45,21 +45,24 @@ def backfill_imageattachment(apps: StateApps, schema_editor: BaseDatabaseSchemaE
extension_limits,
)
+ attachments_query = (
+ Attachment.objects.alias(
+ has_imageattachment=Exists(ImageAttachment.objects.filter(path_id=OuterRef("path_id")))
+ )
+ .filter(extension_limits, has_imageattachment=False)
+ .order_by("id")
+ )
+
+ already_processed = 0
+ total_to_process = attachments_query.count()
min_id: int | None = 0
while True:
- attachments = (
- Attachment.objects.alias(
- has_imageattachment=Exists(
- ImageAttachment.objects.filter(path_id=OuterRef("path_id"))
- )
- )
- .filter(extension_limits, has_imageattachment=False, id__gt=min_id)
- .order_by("id")
- )[:1000]
+ attachments = attachments_query.filter(id__gt=min_id)[:100]
min_id = None
for attachment in attachments:
min_id = attachment.id
+ already_processed += 1
if settings.LOCAL_UPLOADS_DIR is None:
try:
@@ -109,6 +112,7 @@ def backfill_imageattachment(apps: StateApps, schema_editor: BaseDatabaseSchemaE
except pyvips.Error:
pass
+ print(f"Processed {already_processed}/{total_to_process}")
if min_id is None:
break
diff --git a/zerver/migrations/0622_backfill_imageattachment_again.py b/zerver/migrations/0622_backfill_imageattachment_again.py
index fcc6f3dbce0..76c8763227d 100644
--- a/zerver/migrations/0622_backfill_imageattachment_again.py
+++ b/zerver/migrations/0622_backfill_imageattachment_again.py
@@ -49,21 +49,24 @@ def backfill_imageattachment(apps: StateApps, schema_editor: BaseDatabaseSchemaE
extension_limits,
)
+ attachments_query = (
+ Attachment.objects.alias(
+ has_imageattachment=Exists(ImageAttachment.objects.filter(path_id=OuterRef("path_id")))
+ )
+ .filter(extension_limits, has_imageattachment=False)
+ .order_by("id")
+ )
+
+ already_processed = 0
+ total_to_process = attachments_query.count()
min_id: int | None = 0
while True:
- attachments = (
- Attachment.objects.alias(
- has_imageattachment=Exists(
- ImageAttachment.objects.filter(path_id=OuterRef("path_id"))
- )
- )
- .filter(extension_limits, has_imageattachment=False, id__gt=min_id)
- .order_by("id")
- )[:1000]
+ attachments = attachments_query.filter(id__gt=min_id)[:100]
min_id = None
for attachment in attachments:
min_id = attachment.id
+ already_processed += 1
if settings.LOCAL_UPLOADS_DIR is None:
try:
@@ -113,6 +116,7 @@ def backfill_imageattachment(apps: StateApps, schema_editor: BaseDatabaseSchemaE
except pyvips.Error:
pass
+ print(f"Processed {already_processed}/{total_to_process}")
if min_id is None:
break