transaction: Add durable=True to the outermost db transactions.

This commit adds `durable=True` to the outermost db transactions
created in the following:
* confirm_email_change
* handle_upload_pre_finish_hook
* deliver_scheduled_emails
* restore_data_from_archive
* do_change_realm_subdomain
* do_create_realm
* do_deactivate_realm
* do_reactivate_realm
* do_delete_user
* do_delete_user_preserving_messages
* create_stripe_customer
* process_initial_upgrade
* do_update_plan
* request_sponsorship
* upload_message_attachment
* register_remote_server
* do_soft_deactivate_users
* maybe_send_batched_emails

It helps to avoid creating unintended savepoints in the future.

This is as a part of our plan to explicitly mark all the
transaction.atomic calls with either 'savepoint=False' or
'durable=True' as required.

* 'savepoint=True' is used in special cases.
This commit is contained in:
Prakhar Pratyush
2024-11-04 10:49:11 +05:30
committed by Tim Abbott
parent 174a458928
commit 9c9866461a
12 changed files with 18 additions and 18 deletions

View File

@@ -1094,7 +1094,7 @@ class BillingSession(ABC):
metadata=stripe_customer_data.metadata,
)
event_time = timestamp_to_datetime(stripe_customer.created)
with transaction.atomic():
with transaction.atomic(durable=True):
self.write_to_audit_log(BillingSessionEventType.STRIPE_CUSTOMER_CREATED, event_time)
customer = self.update_or_create_customer(stripe_customer.id)
return customer
@@ -1793,7 +1793,7 @@ class BillingSession(ABC):
# TODO: The correctness of this relies on user creation, deactivation, etc being
# in a transaction.atomic() with the relevant RealmAuditLog entries
with transaction.atomic():
with transaction.atomic(durable=True):
# We get the current license count here in case the number of billable
# licenses has changed since the upgrade process began.
current_licenses_count = self.get_billable_licenses_for_customer(
@@ -2912,7 +2912,7 @@ class BillingSession(ABC):
if status is not None:
if status == CustomerPlan.ACTIVE:
assert plan.status < CustomerPlan.LIVE_STATUS_THRESHOLD
with transaction.atomic():
with transaction.atomic(durable=True):
# Switch to a different plan was cancelled. We end the next plan
# and set the current one as active.
if plan.status == CustomerPlan.SWITCH_PLAN_TIER_AT_PLAN_END:
@@ -3412,7 +3412,7 @@ class BillingSession(ABC):
raise BillingError("Form validation error", message=message)
request_context = self.get_sponsorship_request_session_specific_context()
with transaction.atomic():
with transaction.atomic(durable=True):
# Ensures customer is created first before updating sponsorship status.
self.update_customer_sponsorship_status(True)
sponsorship_request = ZulipSponsorshipRequest(