mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #748] [Feature]: wrap folder storage per-row UPDATE loops in db.transaction() across both folder modules #359
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#359
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.tsandpackages/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'ssortOrderby +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 newsortOrder. If the loop crashes halfway, the folder list is left in a mixed state.reorderConnections(orderedIds, folderId)(connection-folders only) — same shape asreorder, but onapi_connections. Already exposed by the route, not yet wired to UI (drag-within-folder is a deferred follow-up).chat-folders.routes.tsPOST /reorder-chats— runschatsStorage.update(id, { sortOrder })+chatsStorage.setFolderForChat(id, folderId)in aforloop 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 increate()) commit atomically. This is the canonical pattern in this codebase — seelorebooks.storage.ts:231,285,295andagents.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 toSTORAGE_BACKEND=files(seepackages/server/src/config/runtime-config.ts:245), which routes all DB calls throughpackages/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'srun()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:
@cha1latte commented on GitHub (May 12, 2026):
Triage note: labeling this as
normal-scopebecause 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.