Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
do_delete_all_realm_attachments: Always wrap in a transaction.
Mateusz Mandera406eec90cade
diff --git a/zerver/actions/data_import.py b/zerver/actions/data_import.pyindex de5652575fd..6eee530782f 100644--- a/zerver/actions/data_import.py+++ b/zerver/actions/data_import.py@@ -6,6 +6,7 @@ import tempfilefrom typing import Anyfrom django.conf import settings+from django.db import transactionfrom confirmation import settings as confirmation_settingsfrom zerver.actions.create_realm import get_email_address_visibility_default@@ -128,9 +129,10 @@ def import_slack_data(event: dict[str, Any]) -> None:preregistration_realm.data_import_metadata["invalid_file_error_message"] = str(e)preregistration_realm.save()- realm = Realm.objects.get(string_id=string_id)- do_delete_all_realm_attachments(realm)- realm.delete()+ with transaction.atomic(durable=True):+ realm = Realm.objects.select_for_update(no_key=False).get(string_id=string_id)+ do_delete_all_realm_attachments(realm)+ realm.delete()except Realm.DoesNotExist:passraisediff --git a/zerver/management/commands/delete_realm.py b/zerver/management/commands/delete_realm.pyindex 5d1c7ff7664..a86d5e1612b 100644--- a/zerver/management/commands/delete_realm.py+++ b/zerver/management/commands/delete_realm.py@@ -3,6 +3,7 @@ from typing import Anyfrom django.conf import settingsfrom django.core.management.base import CommandError+from django.db import transactionfrom typing_extensions import overridefrom zerver.actions.realm_settings import do_delete_all_realm_attachments@@ -56,13 +57,14 @@ realms used for testing; consider using deactivate_realm instead."""if confirmation != realm.string_id:raise CommandError("Aborting!")- # Explicitly remove the attachments and their files in backend- # storage; failing to do this leaves dangling files- do_delete_all_realm_attachments(realm)+ with transaction.atomic(durable=True):+ # Explicitly remove the attachments and their files in backend+ # storage; failing to do this leaves dangling files+ do_delete_all_realm_attachments(realm)- # TODO: This approach leaks Recipient and DirectMessageGroup- # objects, because those don't have a foreign key to the Realm- # or any other model it cascades to (Realm/Stream/UserProfile/etc.).- realm.delete()+ # TODO: This approach leaks Recipient and DirectMessageGroup+ # objects, because those don't have a foreign key to the Realm+ # or any other model it cascades to (Realm/Stream/UserProfile/etc.).+ realm.delete()print("Realm has been successfully permanently deleted.")