[GH-ISSUE #748] [Feature]: wrap folder storage per-row UPDATE loops in db.transaction() across both folder modules #359

Closed
opened 2026-10-20 17:27:58 +07:00 by bsakaguchi · 1 comment
Owner

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

Originally assigned to: @Romuromylus on GitHub.

What problem does this solve?

The folder storage modules (packages/server/src/services/storage/chat-folders.storage.ts and packages/server/src/services/storage/connection-folders.storage.ts) and the chat-folders reorder route (packages/server/src/routes/chat-folders.routes.ts — POST /reorder-chats) all contain per-row UPDATE loops that lack atomicity:

  • create() — shifts every existing folder's sortOrder by +1, then inserts the new folder. If the insert fails partway through the shift, the DB is left with shifted rows but no new folder.
  • reorder(orderedIds) — issues one UPDATE per ID to set the new sortOrder. If the loop crashes halfway, the folder list is left in a mixed state.
  • reorderConnections(orderedIds, folderId) (connection-folders only) — same shape as reorder, but on api_connections. Already exposed by the route, not yet wired to UI (drag-within-folder is a deferred follow-up).
  • chat-folders.routes.ts POST /reorder-chats — runs chatsStorage.update(id, { sortOrder }) + chatsStorage.setFolderForChat(id, folderId) in a for loop without transaction boundaries.

Same pattern lives in both folder systems, so this is the natural moment to fix them in lockstep.

Proposed solution

Wrap each of the above operations in db.transaction(async (tx) => { ... }) so the per-row UPDATEs (or the shift + insert in create()) commit atomically. This is the canonical pattern in this codebase — see lorebooks.storage.ts:231,285,295 and agents.storage.ts:388.

Applied to both folder modules in the same PR so the chat-folder and connection-folder storage stay consistent.

Alternatives considered

  • Single-query bulk UPDATE with a SQL expression (e.g. tx.update(folders).set({ sortOrder: sql\${folders.sortOrder} + 1` })). **Rejected — incompatible with the file-native storage backend.** Marinara v1.5.7+ defaults to STORAGE_BACKEND=files(seepackages/server/src/config/runtime-config.ts:245), which routes all DB calls through packages/server/src/db/file-backed-store.ts. That backend treats .set()patches as plain row literals and.run()is an intentional no-op (file-backed-store.ts:932 — "Raw SQL is intentionally unsupported by the file-native runtime"). A Drizzlesql` template would silently break for any user not on opt-in SQLite mode.

  • CASE-based bulk UPDATE via db.run(sql.raw(...)) — same blocker as above. The file-native runtime's run() is a no-op.

  • Leave the loops as-is — status quo. Acceptable because folder counts in a single-user Marinara install are O(dozens), so the practical impact of the missing atomicity is small. But the consistency win (matching lorebooks.storage.ts's transaction pattern) justifies the small change.

  • Chunked transactions to avoid the libSQL Windows crash (see chats.storage.ts:450-453, issue #73). Not needed at folder/connection scale — the entire reorder fits in a single small transaction.

Additional context

Raised during CodeRabbit review of PR #745 across three threads — storage.ts:41 (create), storage.ts:70 (reorder), storage.ts:86 (reorderConnections).

Filed as a lockstep follow-up so both folder storage modules pick up the transaction wrap at the same time, rather than letting them diverge.

To be implemented together with the null-guard refactor in #747, since both touch the same files and target the same two folder modules.

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/748 Originally assigned to: @Romuromylus on GitHub. ## What problem does this solve? The folder storage modules (`packages/server/src/services/storage/chat-folders.storage.ts` and `packages/server/src/services/storage/connection-folders.storage.ts`) and the chat-folders reorder route (`packages/server/src/routes/chat-folders.routes.ts` — `POST /reorder-chats`) all contain per-row UPDATE loops that lack atomicity: - `create()` — shifts every existing folder's `sortOrder` by +1, then inserts the new folder. If the insert fails partway through the shift, the DB is left with shifted rows but no new folder. - `reorder(orderedIds)` — issues one UPDATE per ID to set the new `sortOrder`. If the loop crashes halfway, the folder list is left in a mixed state. - `reorderConnections(orderedIds, folderId)` (connection-folders only) — same shape as `reorder`, but on `api_connections`. Already exposed by the route, not yet wired to UI (drag-within-folder is a deferred follow-up). - `chat-folders.routes.ts` `POST /reorder-chats` — runs `chatsStorage.update(id, { sortOrder })` + `chatsStorage.setFolderForChat(id, folderId)` in a `for` loop without transaction boundaries. Same pattern lives in both folder systems, so this is the natural moment to fix them in lockstep. ## Proposed solution Wrap each of the above operations in `db.transaction(async (tx) => { ... })` so the per-row UPDATEs (or the shift + insert in `create()`) commit atomically. This is the canonical pattern in this codebase — see `lorebooks.storage.ts:231,285,295` and `agents.storage.ts:388`. Applied to both folder modules in the same PR so the chat-folder and connection-folder storage stay consistent. ## Alternatives considered - **Single-query bulk UPDATE with a SQL expression** (e.g. `tx.update(folders).set({ sortOrder: sql\`${folders.sortOrder} + 1\` })`). **Rejected — incompatible with the file-native storage backend.** Marinara v1.5.7+ defaults to `STORAGE_BACKEND=files` (see `packages/server/src/config/runtime-config.ts:245`), which routes all DB calls through `packages/server/src/db/file-backed-store.ts`. That backend treats `.set()` patches as plain row literals and `.run()` is an intentional no-op (file-backed-store.ts:932 — "Raw SQL is intentionally unsupported by the file-native runtime"). A Drizzle `sql` template would silently break for any user not on opt-in SQLite mode. - **CASE-based bulk UPDATE via `db.run(sql.raw(...))`** — same blocker as above. The file-native runtime's `run()` is a no-op. - **Leave the loops as-is** — status quo. Acceptable because folder counts in a single-user Marinara install are O(dozens), so the practical impact of the missing atomicity is small. But the consistency win (matching `lorebooks.storage.ts`'s transaction pattern) justifies the small change. - **Chunked transactions to avoid the libSQL Windows crash** (see `chats.storage.ts:450-453`, issue #73). Not needed at folder/connection scale — the entire reorder fits in a single small transaction. ## Additional context Raised during CodeRabbit review of PR #745 across three threads — [storage.ts:41 (create)](https://github.com/Pasta-Devs/Marinara-Engine/pull/745#discussion_r3226725943), [storage.ts:70 (reorder)](https://github.com/Pasta-Devs/Marinara-Engine/pull/745#discussion_r3226725959), [storage.ts:86 (reorderConnections)](https://github.com/Pasta-Devs/Marinara-Engine/pull/745#discussion_r3226725970). Filed as a lockstep follow-up so both folder storage modules pick up the transaction wrap at the same time, rather than letting them diverge. To be implemented together with the null-guard refactor in #747, since both touch the same files and target the same two folder modules. ## 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:27:58 +07:00
Author
Owner

@cha1latte commented on GitHub (May 12, 2026):

Triage note: labeling this as normal-scope because it is a focused consistency and atomicity improvement for existing folder storage operations.

The least risky next step is to wrap the listed per-row update loops in the existing transaction pattern while keeping chat-folder and connection-folder behavior in lockstep.

<!-- gh-comment-id:4432576520 --> @cha1latte commented on GitHub (May 12, 2026): Triage note: labeling this as `normal-scope` because it is a focused consistency and atomicity improvement for existing folder storage operations. The least risky next step is to wrap the listed per-row update loops in the existing transaction pattern while keeping chat-folder and connection-folder behavior in lockstep.
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#359
No description provided.