realm_export: Add transaction.atomic to export_realm.

This commit is contained in:
Sahil Batra 2022-12-05 16:52:50 +05:30 committed by Tim Abbott
parent 0030c8d115
commit 5635881664
3 changed files with 9 additions and 4 deletions

View File

@ -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:

View File

@ -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))

View File

@ -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)