[GH-ISSUE #753] [Feature]: wrap chat-folders POST /reorder-chats loop in db.transaction() (requires chatsStorage tx-param refactor) #360

Closed
opened 2026-10-20 17:28:00 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Romuromylus on GitHub (May 12, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/753

What problem does this solve?

POST /api/chat-folders/reorder-chats (in packages/server/src/routes/chat-folders.routes.ts) loops over orderedChatIds and runs two storage calls per chat without transaction boundaries:

for (let i = 0; i < orderedChatIds.length; i++) {
  const id = orderedChatIds[i]!;
  await chatsStorage.update(id, { sortOrder: i + 1 });
  await chatsStorage.setFolderForChat(id, folderId);
}

A partial failure mid-loop (DB write error, container restart, etc.) leaves chats in a mixed state — some moved into the new folder, others still in their old one — and the user has to manually re-sort.

This was the one remaining bullet from #748 that PR #749 deliberately did not address, because wrapping it requires a non-trivial storage refactor (see Proposed solution).

Proposed solution

Wrap the loop in db.transaction(). This is more involved than the storage-side wraps in #749 because:

  • chatsStorage.setFolderForChat(id, folderId) does branch-sibling propagation against groupId (so all branches of a chat move together). To run it inside a transaction, chatsStorage needs to accept an optional tx parameter for at least update and setFolderForChat, threading it through to the per-row UPDATEs.
  • That tx plumbing is the actual scope of the work. Once in place, the route handler becomes:
    await app.db.transaction(async (tx) => {
      for (let i = 0; i < orderedChatIds.length; i++) {
        const id = orderedChatIds[i]!;
        await chatsStorage.update(id, { sortOrder: i + 1 }, { tx });
        await chatsStorage.setFolderForChat(id, folderId, { tx });
      }
    });
    

The same tx-param convention can apply to any future storage call that needs to participate in a larger transaction, so the cost is amortised.

Alternatives considered

  • Inline the per-row logic into the route handler (skip chatsStorage entirely so we can run pure Drizzle inside db.transaction). Possible but reproduces the branch-propagation logic in a second location — easy to drift.
  • A new bulk-method on chatsStorage like reorderInFolder(orderedIds, folderId) that does its own internal transaction. Cleaner separation of concerns but adds another bespoke method just for this route; the optional-tx approach is more generally useful.
  • Leave the loop as-is. Status quo. Acceptable because folder counts and chats-per-folder are both O(dozens) so partial-failure windows are small in practice — but the consistency win with the rest of the folder storage (now atomic post-#749) justifies closing the last gap.

Additional context

Spun out from #748 / PR #749's "What was deliberately not changed" section. PR #749 wraps create, reorder, reorderConnections in both folder storage modules atomically; this is the last remaining per-row loop in the folder code paths.

Independent review of PR #749 flagged that Closes #748 would auto-close #748 on merge and silently drop this remaining work, hence carving it out into its own issue.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (May 12, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/753 ## What problem does this solve? `POST /api/chat-folders/reorder-chats` (in `packages/server/src/routes/chat-folders.routes.ts`) loops over `orderedChatIds` and runs two storage calls per chat without transaction boundaries: ```ts for (let i = 0; i < orderedChatIds.length; i++) { const id = orderedChatIds[i]!; await chatsStorage.update(id, { sortOrder: i + 1 }); await chatsStorage.setFolderForChat(id, folderId); } ``` A partial failure mid-loop (DB write error, container restart, etc.) leaves chats in a mixed state — some moved into the new folder, others still in their old one — and the user has to manually re-sort. This was the one remaining bullet from #748 that PR #749 deliberately did not address, because wrapping it requires a non-trivial storage refactor (see Proposed solution). ## Proposed solution Wrap the loop in `db.transaction()`. This is more involved than the storage-side wraps in #749 because: - `chatsStorage.setFolderForChat(id, folderId)` does branch-sibling propagation against `groupId` (so all branches of a chat move together). To run it inside a transaction, `chatsStorage` needs to accept an optional `tx` parameter for at least `update` and `setFolderForChat`, threading it through to the per-row UPDATEs. - That `tx` plumbing is the actual scope of the work. Once in place, the route handler becomes: ```ts await app.db.transaction(async (tx) => { for (let i = 0; i < orderedChatIds.length; i++) { const id = orderedChatIds[i]!; await chatsStorage.update(id, { sortOrder: i + 1 }, { tx }); await chatsStorage.setFolderForChat(id, folderId, { tx }); } }); ``` The same `tx`-param convention can apply to any future storage call that needs to participate in a larger transaction, so the cost is amortised. ## Alternatives considered - **Inline the per-row logic into the route handler** (skip `chatsStorage` entirely so we can run pure Drizzle inside `db.transaction`). Possible but reproduces the branch-propagation logic in a second location — easy to drift. - **A new bulk-method on `chatsStorage` like `reorderInFolder(orderedIds, folderId)`** that does its own internal transaction. Cleaner separation of concerns but adds another bespoke method just for this route; the optional-`tx` approach is more generally useful. - **Leave the loop as-is.** Status quo. Acceptable because folder counts and chats-per-folder are both O(dozens) so partial-failure windows are small in practice — but the consistency win with the rest of the folder storage (now atomic post-#749) justifies closing the last gap. ## Additional context Spun out from #748 / PR #749's "What was deliberately not changed" section. PR #749 wraps `create`, `reorder`, `reorderConnections` in both folder storage modules atomically; this is the last remaining per-row loop in the folder code paths. Independent review of PR #749 flagged that `Closes #748` would auto-close #748 on merge and silently drop this remaining work, hence carving it out into its own issue. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-20 17:28:00 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#360
No description provided.