Skip to content

improvement(db): name chat uploads in one indexed read and index workspace payer lookups - #9208

Merged
waleedlatif1 merged 2 commits into
stagingfrom
improvement/chat-upload-names-billing-index
Oct 18, 2026
Merged

waleedlatif1 merged 2 commits into
stagingfrom
improvement/chat-upload-names-billing-index

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • trackChatUpload probed display names by writing: each candidate (image.png, image (2).png, …) was its own transaction + project lock + UPDATE/INSERT, and a taken name surfaced as a 23505 on workspace_files_chat_display_name_unique. A message with N same-named attachments cost 1+2+…+N writes (20 attachments → 210 writes, ~60ms per failed attempt, multi-second chat POSTs)
  • Now one read per attachment, inside the same transaction after the project lock, collects the chat's taken names (exact name or <stem> (%)<ext>, LIKE-escaped with escapeLikePattern) using the unique index's own predicate (context = 'mothership', chat set, soft-deleted rows included), excludes the row being re-linked, and writes the first free suffixedName. The 23505 retry stays for races, and a lost candidate is never retried
  • Same naming as before: the lowest free slot, soft-deleted siblings keep their slot, and re-linking a row to its own chat keeps its current name. withCopySuffix now shares its stem/extension split with the LIKE pattern so the two cannot drift
  • Add workspace_billed_account_user_id_idx (plain btree, CREATE INDEX CONCURRENTLY with the 0429 invalid-index recovery). workspace.billed_account_user_id had no index, so the GET /api/billing upgrade-target lookup and account deletion seq-scanned workspace, and so did the user FK's (no action) delete check. A plain index serves all of these: the equality lookup, the owner_id OR billed_account_user_id deletion queries (BitmapOr with workspace_owner_id_idx), and the FK check. A user has few workspaces, so a composite or partial index buys nothing

Type of Change

  • Improvement (performance)

Testing

  • file-names.integration.ts (real Postgres), new cases: 20 same-named attachments (10 re-links + 10 inserts) in one chat, a re-link that keeps its name while a soft-deleted sibling holds image.png, and names with %, _, \, parentheses, no extension, a leading dot, and multiple dots. A trigger counts every display-name write through a sequence, which a rollback does not undo
    • Red on pre-fix code: 210 writes for 20 attachments (expected 20); 2 writes for an idempotent re-link (expected 1); 6 writes for 3 uploads of each special name (expected 3). Names were already correct before the fix
    • Each guard reverted on its own: dropping the LIKE escaping fails the backslash case (4 writes); dropping the re-linked-row exclusion moves the row to image (3).png
    • Green: 26/26, under both push and INTEGRATION_DB_PROVISION=migrate (applies 0435)
  • Plain EXPLAIN of the taken-names read: Index Scan using workspace_files_chat_display_name_unique, Index Cond on chat_id. Payer lookup before the index: Seq Scan on workspace
  • track-chat-upload.test.ts, file-manager.test.ts; bun run lint; check:test-patterns; check:comment-hygiene; check:migrations origin/staging; drizzle-kit generate reports no changes

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing (new tests pass the test-audit authoring gate)
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

…space payer lookups

- trackChatUpload reads the chat's taken display names once (unique-index
  predicate, LIKE-escaped suffix pattern, excluding the re-linked row) and
  writes the first free name, instead of probing each candidate with a write
  that a unique violation rolls back
- add workspace_billed_account_user_id_idx (concurrent) for payer lookups
  and the user FK's delete check
@vercel

vercel Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
docs Ready Ready Preview Oct 18, 2026 11:18pm UTC

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 6 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge; no actionable defects were found.

Summary

The PR replaces repeated name-conflict writes with one lookup before each chat upload write.

  • Chat uploads find free display names with one indexed read.
  • Workspace payer searches get an index on the payer ID.

No actionable issues were found. Runtime tests were not run during this review.

Reviews (1) · Last reviewed commit: "improvement(db): name chat uploads in on..." · Reviewed by Greptile

@waleedlatif1
waleedlatif1 merged commit 282d952 into staging Oct 18, 2026
51 checks passed
@waleedlatif1
waleedlatif1 deleted the improvement/chat-upload-names-billing-index branch October 18, 2026 23:30

This branch was previously deployed

1 inactive deployment
Preview — 4cb39324 Deployed Oct 18, 2026 by vercel[bot]
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