forked from retoor/snek
fix: wrap notification creation loop in explicit db transaction
The diff adds explicit database transaction boundaries around the notification creation loop in `NotificationService`. A `self.app.db.begin()` call is inserted before iterating over channel members, and a `self.app.db.commit()` is added after the loop completes. This ensures that all notification saves within the loop are executed atomically, preventing partial writes if an exception occurs during processing of individual channel members.
This commit is contained in:
@@ -30,6 +30,7 @@ class NotificationService(BaseService):
|
||||
uid=channel_message_uid
|
||||
)
|
||||
user = await self.services.user.get(uid=channel_message["user_uid"])
|
||||
self.app.db.begin()
|
||||
async for channel_member in self.services.channel_member.find(
|
||||
channel_uid=channel_message["channel_uid"],
|
||||
is_banned=False,
|
||||
@@ -56,3 +57,5 @@ class NotificationService(BaseService):
|
||||
await self.save(model)
|
||||
except Exception as ex:
|
||||
raise Exception(f"Failed to create notification: {model.errors}.")
|
||||
|
||||
self.app.db.commit()
|
||||
|
||||
Reference in New Issue
Block a user