fix(files,comments): prevent duplicate file rows and handle undefined fileId in comments#197
Draft
zjean wants to merge 1 commit into
Draft
Conversation
… fileId Two related bugs in the comments flow: 1. getOrCreateSpaceFile (files-queries.service.ts) could create duplicate rows in the `files` table. When a file was not yet indexed, repeated calls with a non-positive fileId always fell through to INSERT without first checking whether a record already existed at that path. Because the table has no unique constraint on the path columns, this silently accumulated duplicates. On the next directory browse, browseFiles and getSpaceFileId could disagree on which row to return (iteration order vs LIMIT 1), producing a spurious "File id mismatch" on subsequent comment operations. Fix: call getSpaceFileId before the fallthrough INSERT and return the existing id if one is found, so the insert only happens when no record exists at that path. 2. updateComment and deleteComment declared their local fileId as `number` even though getFileId can return undefined when the file is not indexed. TypeScript silently accepted the mistyped value, making the mismatch check always true for an unindexed file and throwing "File id mismatch" instead of an accurate error. Fix: remove the `: number` annotation so TypeScript infers `number | undefined`, and add an explicit `fileId === undefined` guard before the mismatch comparison — matching the pattern already present in createComment. Also fix createComment to handle getFileId returning undefined when the caller supplies a positive fileId: instead of throwing mismatch, delegate to getOrCreateSpaceFile to index the file on demand. Tests: add three new cases to comments-manager.service.spec.ts covering each of the fixed branches (createComment with unindexed positive id, updateComment with undefined fileId, deleteComment with undefined fileId).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two related bugs that cause a spurious "File id mismatch" error on comment operations.
getOrCreateSpaceFilecould create duplicate rowsThe fallthrough
INSERTexecuted without first checking whether a record already existed at the given path. Because thefilestable has noUNIQUEconstraint on(ownerId/spaceId, path, name, isDir, inTrash), repeated calls for the same path silently accumulated duplicate rows. On the next browse,browseFiles(iterates all matches, takes the last) andgetSpaceFileId(LIMIT 1, takes the first) could return different ids from the duplicates, causing every subsequent comment operation on that file to throwFile id mismatch.Fix: call
getSpaceFileIdbefore the fallthroughINSERT; return the existing id if one is found.updateComment/deleteCommentmishandledundefinedfromgetFileIdBoth methods declared their local result as
: numbereven thoughgetFileIdreturnsnumber | undefinedwhen the file is not indexed. TypeScript silently accepted the mismatch; the equality checkdto.fileId !== undefinedalways evaluated totrue, so either operation would always throwFile id mismatchfor an unindexed file.createCommenthad the same flaw on the positive-id path.Fix: remove the
: numberannotation, add an explicitfileId === undefinedguard, and havecreateCommentdelegate togetOrCreateSpaceFile(index on demand) instead of rejecting whengetFileIdreturnsundefinedfor a positive caller-supplied id.Test plan
Three new cases added to
comments-manager.service.spec.ts:createCommentwith a positive fileId when the file is not yet indexed → usesgetOrCreateSpaceFileupdateCommentwhengetSpaceFileIdreturnsundefined→ throwsBAD_REQUESTdeleteCommentwhengetSpaceFileIdreturnsundefined→ throwsBAD_REQUESTFull backend test suite: 886/886 passing.