add_subscriptions_backend: Add 'transaction.atomic' decorator.

This commit adds an 'transaction.atomic' decorator to the
'add_subscriptions_backend' view thus making the db operations
within the view atomic and helps to avoid race between events sent.

In tests where we make POST requests to this view, we have
wrapped the API calls with a transaction.atomic() context
manager. It helps us with NOT rolling back the entire test
transaction due to error responses.
This commit is contained in:
Prakhar Pratyush 2024-06-06 17:06:24 +05:30 committed by Tim Abbott
parent 9c614531fb
commit d92eb77c98
2 changed files with 13 additions and 8 deletions

View File

@ -32,7 +32,7 @@ from django.apps import apps
from django.conf import settings
from django.core.mail import EmailMessage
from django.core.signals import got_request_exception
from django.db import connection
from django.db import connection, transaction
from django.db.migrations.executor import MigrationExecutor
from django.db.migrations.state import StateApps
from django.db.utils import IntegrityError
@ -1458,6 +1458,10 @@ Output:
"invite_only": orjson.dumps(invite_only).decode(),
}
post_data.update(extra_post_data)
# We wrap the API call with a 'transaction.atomic()' context
# manager as it helps us with NOT rolling back the entire
# test transaction due to error responses.
with transaction.atomic():
result = self.api_post(
user,
"/api/v1/users/me/subscriptions",

View File

@ -533,6 +533,7 @@ RETENTION_DEFAULT: Union[str, int] = "realm_default"
EMPTY_PRINCIPALS: Union[Sequence[str], Sequence[int]] = []
@transaction.atomic(savepoint=False)
@require_non_guest_user
@has_request_variables
def add_subscriptions_backend(