Skip to content

fix(files,comments): prevent duplicate file rows and handle undefined fileId in comments#197

Draft
zjean wants to merge 1 commit into
Sync-in:mainfrom
zjean:upstream-contrib/fix-comments-file-id-mismatch
Draft

fix(files,comments): prevent duplicate file rows and handle undefined fileId in comments#197
zjean wants to merge 1 commit into
Sync-in:mainfrom
zjean:upstream-contrib/fix-comments-file-id-mismatch

Conversation

@zjean
Copy link
Copy Markdown
Contributor

@zjean zjean commented May 12, 2026

Summary

Two related bugs that cause a spurious "File id mismatch" error on comment operations.

getOrCreateSpaceFile could create duplicate rows

The fallthrough INSERT executed without first checking whether a record already existed at the given path. Because the files table has no UNIQUE constraint 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) and getSpaceFileId (LIMIT 1, takes the first) could return different ids from the duplicates, causing every subsequent comment operation on that file to throw File id mismatch.

Fix: call getSpaceFileId before the fallthrough INSERT; return the existing id if one is found.

updateComment / deleteComment mishandled undefined from getFileId

Both methods declared their local result as : number even though getFileId returns number | undefined when the file is not indexed. TypeScript silently accepted the mismatch; the equality check dto.fileId !== undefined always evaluated to true, so either operation would always throw File id mismatch for an unindexed file.

createComment had the same flaw on the positive-id path.

Fix: remove the : number annotation, add an explicit fileId === undefined guard, and have createComment delegate to getOrCreateSpaceFile (index on demand) instead of rejecting when getFileId returns undefined for a positive caller-supplied id.

Test plan

Three new cases added to comments-manager.service.spec.ts:

  • createComment with a positive fileId when the file is not yet indexed → uses getOrCreateSpaceFile
  • updateComment when getSpaceFileId returns undefined → throws BAD_REQUEST
  • deleteComment when getSpaceFileId returns undefined → throws BAD_REQUEST

Full backend test suite: 886/886 passing.

… 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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant