From 5635881664d5e54f7c986a5b655f1310424c9458 Mon Sep 17 00:00:00 2001 From: Sahil Batra Date: Mon, 5 Dec 2022 16:52:50 +0530 Subject: [PATCH] realm_export: Add transaction.atomic to export_realm. --- zerver/actions/realm_export.py | 3 ++- zerver/tests/test_realm_export.py | 6 ++++-- zerver/views/realm_export.py | 4 +++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/zerver/actions/realm_export.py b/zerver/actions/realm_export.py index f59c009bcf..7fe18aa719 100644 --- a/zerver/actions/realm_export.py +++ b/zerver/actions/realm_export.py @@ -1,4 +1,5 @@ import orjson +from django.db import transaction from django.utils.timezone import now as timezone_now from zerver.lib.export import get_realm_exports_serialized @@ -10,7 +11,7 @@ from zerver.tornado.django_api import send_event def notify_realm_export(user_profile: UserProfile) -> None: # In the future, we may want to send this event to all realm admins. event = dict(type="realm_export", exports=get_realm_exports_serialized(user_profile)) - send_event(user_profile.realm, event, [user_profile.id]) + transaction.on_commit(lambda: send_event(user_profile.realm, event, [user_profile.id])) def do_delete_realm_export(user_profile: UserProfile, export: RealmAuditLog) -> None: diff --git a/zerver/tests/test_realm_export.py b/zerver/tests/test_realm_export.py index 5d8dc93b31..1dcfd687a9 100644 --- a/zerver/tests/test_realm_export.py +++ b/zerver/tests/test_realm_export.py @@ -48,7 +48,8 @@ class RealmExportTest(ZulipTestCase): with self.settings(LOCAL_UPLOADS_DIR=None), stdout_suppressed(), self.assertLogs( level="INFO" ) as info_logs: - result = self.client_post("/json/export/realm") + with self.captureOnCommitCallbacks(execute=True): + result = self.client_post("/json/export/realm") self.assertTrue("INFO:root:Completed data export for zulip in " in info_logs.output[0]) self.assert_json_success(result) self.assertFalse(os.path.exists(tarball_path)) @@ -118,7 +119,8 @@ class RealmExportTest(ZulipTestCase): # Test the export logic. with patch("zerver.lib.export.do_export_realm", return_value=tarball_path) as mock_export: with stdout_suppressed(), self.assertLogs(level="INFO") as info_logs: - result = self.client_post("/json/export/realm") + with self.captureOnCommitCallbacks(execute=True): + result = self.client_post("/json/export/realm") self.assertTrue("INFO:root:Completed data export for zulip in " in info_logs.output[0]) self.assert_json_success(result) self.assertFalse(os.path.exists(tarball_path)) diff --git a/zerver/views/realm_export.py b/zerver/views/realm_export.py index bae5b12036..136c41815e 100644 --- a/zerver/views/realm_export.py +++ b/zerver/views/realm_export.py @@ -2,6 +2,7 @@ from datetime import timedelta import orjson from django.conf import settings +from django.db import transaction from django.http import HttpRequest, HttpResponse from django.utils.timezone import now as timezone_now from django.utils.translation import gettext as _ @@ -17,6 +18,7 @@ from zerver.lib.utils import assert_is_not_none from zerver.models import RealmAuditLog, UserProfile +@transaction.atomic(durable=True) @require_realm_admin def export_realm(request: HttpRequest, user: UserProfile) -> HttpResponse: # Currently only supports public-data-only exports. @@ -83,7 +85,7 @@ def export_realm(request: HttpRequest, user: UserProfile) -> HttpResponse: "user_profile_id": user.id, "id": row.id, } - queue_json_publish("deferred_work", event) + transaction.on_commit(lambda: queue_json_publish("deferred_work", event)) return json_success(request)