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.

do_change_realm_subdomain: Use transaction.atomic.

Mateusz Mandera74f4e3e9147e
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index 1a5c695bd96..9c15c34a249 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -1138,21 +1138,25 @@ def do_change_realm_subdomain(
# deleting, clear that state.
realm.demo_organization_scheduled_deletion_date = None
realm.string_id = new_subdomain
- realm.save(update_fields=["string_id", "demo_organization_scheduled_deletion_date"])
- RealmAuditLog.objects.create(
- realm=realm,
- event_type=RealmAuditLog.REALM_SUBDOMAIN_CHANGED,
- event_time=timezone_now(),
- acting_user=acting_user,
- extra_data={"old_subdomain": old_subdomain, "new_subdomain": new_subdomain},
- )
+ with transaction.atomic():
+ realm.save(update_fields=["string_id", "demo_organization_scheduled_deletion_date"])
+ RealmAuditLog.objects.create(
+ realm=realm,
+ event_type=RealmAuditLog.REALM_SUBDOMAIN_CHANGED,
+ event_time=timezone_now(),
+ acting_user=acting_user,
+ extra_data={"old_subdomain": old_subdomain, "new_subdomain": new_subdomain},
+ )
- # If a realm if being renamed multiple times, we should find all the placeholder
- # realms and reset their deactivated_redirect field to point to the new realm uri
- placeholder_realms = Realm.objects.filter(deactivated_redirect=old_uri, deactivated=True)
- for placeholder_realm in placeholder_realms:
- do_add_deactivated_redirect(placeholder_realm, realm.uri)
+ # If a realm if being renamed multiple times, we should find all the placeholder
+ # realms and reset their deactivated_redirect field to point to the new realm uri
+ placeholder_realms = Realm.objects.filter(deactivated_redirect=old_uri, deactivated=True)
+ for placeholder_realm in placeholder_realms:
+ do_add_deactivated_redirect(placeholder_realm, realm.uri)
+ # The below block isn't executed in a transaction with the earlier code due to
+ # the functions called below being complex and potentially sending events,
+ # which we don't want to do in atomic blocks.
# When we change a realm's subdomain the realm with old subdomain is basically
# deactivated. We are creating a deactivated realm using old subdomain and setting
# it's deactivated redirect to new_subdomain so that we can tell the users that