mirror of
https://github.com/zulip/zulip.git
synced 2025-11-16 20:02:15 +00:00
import_realm: Create Stream, UserGroup and Realm in a transaction.
Make the import of `Realm`, `Stream` and `UserGroup` objects be done in single transaction, to make the import process in general more atomic. This also removes the need to temporarily unset the Stream references on the Realm object. Since Django creates foreign key constraints with `DEFERRABLE INITIALLY DEFERRED`, an insertion of a Realm row can reference not-yet-existing Stream rows as long as the row is created before the transaction commits. Discussion - https://chat.zulip.org/#narrow/stream/101-design/topic/New.20permissions.20model/near/1585274.
This commit is contained in:
@@ -12,7 +12,7 @@ from bs4 import BeautifulSoup
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.core.validators import validate_email
|
||||
from django.db import connection
|
||||
from django.db import connection, transaction
|
||||
from django.utils.timezone import now as timezone_now
|
||||
from psycopg2.extras import execute_values
|
||||
from psycopg2.sql import SQL, Identifier
|
||||
@@ -961,18 +961,9 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea
|
||||
# import the supporting data structures, which may take a bit.
|
||||
realm_properties = dict(**data["zerver_realm"][0])
|
||||
realm_properties["deactivated"] = True
|
||||
realm = Realm(**realm_properties)
|
||||
|
||||
if realm.notifications_stream_id is not None:
|
||||
notifications_stream_id: Optional[int] = int(realm.notifications_stream_id)
|
||||
else:
|
||||
notifications_stream_id = None
|
||||
realm.notifications_stream_id = None
|
||||
if realm.signup_notifications_stream_id is not None:
|
||||
signup_notifications_stream_id: Optional[int] = int(realm.signup_notifications_stream_id)
|
||||
else:
|
||||
signup_notifications_stream_id = None
|
||||
realm.signup_notifications_stream_id = None
|
||||
with transaction.atomic(durable=True):
|
||||
realm = Realm(**realm_properties)
|
||||
realm.save()
|
||||
|
||||
if "zerver_usergroup" in data:
|
||||
@@ -1001,10 +992,6 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea
|
||||
stream["rendered_description"] = render_stream_description(stream["description"], realm)
|
||||
bulk_import_model(data, Stream)
|
||||
|
||||
realm.notifications_stream_id = notifications_stream_id
|
||||
realm.signup_notifications_stream_id = signup_notifications_stream_id
|
||||
realm.save()
|
||||
|
||||
# Remap the user IDs for notification_bot and friends to their
|
||||
# appropriate IDs on this server
|
||||
internal_realm = get_realm(settings.SYSTEM_BOT_REALM)
|
||||
|
||||
Reference in New Issue
Block a user