Make copy operation atomic with explicit transaction handling

- Wrapped copy operation in try/except with explicit rollback
- Added comments explaining atomicity
- Ensures all-or-nothing behavior for copying to multiple dates
This commit is contained in:
counterweight 2025-12-21 17:57:42 +01:00
parent c24597edb4
commit 1a478f7583
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C

View file

@ -160,8 +160,10 @@ async def copy_availability(
detail=f"No availability found for source date {request.source_date}",
)
# Copy to each target date
# Copy to each target date within a single atomic transaction
# All deletes and inserts happen before commit, ensuring atomicity
copied_days: list[AvailabilityDay] = []
try:
for target_date in request.target_dates:
if target_date == request.source_date:
continue # Skip copying to self
@ -187,7 +189,12 @@ async def copy_availability(
copied_days.append(AvailabilityDay(date=target_date, slots=target_slots))
# Commit all changes atomically
await db.commit()
except Exception:
# Rollback on any error to maintain atomicity
await db.rollback()
raise
return AvailabilityResponse(days=copied_days)