mirror of https://github.com/zulip/zulip.git
actions: Use transaction.atomic properly when removing subscriptions.
Previously, the transaction.atomic() was not properly scoped to ensure that RealmAuditLog entries were created in the same transaction, making it possible for state changes to not be properly recorded in RealmAuditLog.
This commit is contained in:
parent
0b91526f28
commit
caa939d2d5
|
@ -3064,8 +3064,8 @@ def bulk_remove_subscriptions(users: Iterable[UserProfile],
|
||||||
|
|
||||||
our_realm = users[0].realm
|
our_realm = users[0].realm
|
||||||
|
|
||||||
# TODO: XXX: This transaction really needs to be done at the serializeable
|
# We do all the database changes in a transaction to ensure
|
||||||
# transaction isolation level.
|
# RealmAuditLog entries are atomically created when making changes.
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
occupied_streams_before = list(get_occupied_streams(our_realm))
|
occupied_streams_before = list(get_occupied_streams(our_realm))
|
||||||
Subscription.objects.filter(
|
Subscription.objects.filter(
|
||||||
|
@ -3073,20 +3073,22 @@ def bulk_remove_subscriptions(users: Iterable[UserProfile],
|
||||||
) .update(active=False)
|
) .update(active=False)
|
||||||
occupied_streams_after = list(get_occupied_streams(our_realm))
|
occupied_streams_after = list(get_occupied_streams(our_realm))
|
||||||
|
|
||||||
# Log Subscription Activities in RealmAuditLog
|
# Log Subscription Activities in RealmAuditLog
|
||||||
event_time = timezone_now()
|
event_time = timezone_now()
|
||||||
event_last_message_id = get_last_message_id()
|
event_last_message_id = get_last_message_id()
|
||||||
all_subscription_logs: (List[RealmAuditLog]) = []
|
all_subscription_logs: (List[RealmAuditLog]) = []
|
||||||
for (sub, stream) in subs_to_deactivate:
|
for (sub, stream) in subs_to_deactivate:
|
||||||
all_subscription_logs.append(RealmAuditLog(realm=sub.user_profile.realm,
|
audit_log_entry = RealmAuditLog(
|
||||||
acting_user=acting_user,
|
realm=sub.user_profile.realm,
|
||||||
modified_user=sub.user_profile,
|
acting_user=acting_user,
|
||||||
modified_stream=stream,
|
modified_user=sub.user_profile,
|
||||||
event_last_message_id=event_last_message_id,
|
modified_stream=stream,
|
||||||
event_type=RealmAuditLog.SUBSCRIPTION_DEACTIVATED,
|
event_last_message_id=event_last_message_id,
|
||||||
event_time=event_time))
|
event_type=RealmAuditLog.SUBSCRIPTION_DEACTIVATED,
|
||||||
# Now since we have all log objects generated we can do a bulk insert
|
event_time=event_time)
|
||||||
RealmAuditLog.objects.bulk_create(all_subscription_logs)
|
all_subscription_logs.append(audit_log_entry)
|
||||||
|
# Now since we have all log objects generated we can do a bulk insert
|
||||||
|
RealmAuditLog.objects.bulk_create(all_subscription_logs)
|
||||||
|
|
||||||
altered_user_dict: Dict[int, Set[int]] = defaultdict(set)
|
altered_user_dict: Dict[int, Set[int]] = defaultdict(set)
|
||||||
streams_by_user: Dict[int, List[Stream]] = defaultdict(list)
|
streams_by_user: Dict[int, List[Stream]] = defaultdict(list)
|
||||||
|
|
Loading…
Reference in New Issue