Skip to content

fix(store): look up org token users by primary key - #2067

Merged
AmanGIT07 merged 1 commit into
mainfrom
fix/org-tokens-user-join
Dec 1, 2026
Merged

AmanGIT07 merged 1 commit into
mainfrom
fix/org-tokens-user-join

Conversation

@AmanGIT07

Copy link
Copy Markdown
Contributor

Summary

SearchOrganizationTokens joins each billing transaction to the user who made it. The join rule was a CASE, so Postgres could not use the users primary key or a hash join for it. It tested every transaction of the org against every user, and orgs with many transactions hit the query timeout. The join now looks each user up by primary key. It matches the same users as before.

Changes

  • buildBaseQuery in org_tokens_repository.go: the users join rule is now users.id = CAST(NULLIF(billing_transactions.user_id, '') AS uuid).
  • users.id must stay alone on its side of = so Postgres can use the primary key index (users_pkey), or a hash join. A CASE around the match, or a cast on users.id, hides it from both.
  • NULLIF turns an empty user_id into NULL, which matches no user, instead of failing the cast. CAST('' AS uuid) fails. The old ELSE false also matched no user.
  • org_tokens_repository_test.go expects the new SQL. The old rule bound two values ('' and false) and the new one binds one, so every later placeholder moves down by one.

Technical Details

  • Matching is unchanged for every stored value. A lowercase, uppercase or no-dash uuid finds the user. '' and NULL find no user. A soft-deleted user's name stays hidden. A user_id that is not a uuid makes the search fail, as before. feat(frontier): check that Usage.user_id is a uuid when set proton#506 makes the usage API reject such values.
  • user_id <> '' AND CAST(user_id AS uuid) = users.id is not safe: Postgres does not promise to check <> '' before the cast. NULLIF fixes the value itself, so the order does not matter.
  • Comparing as text (CAST(users.id AS TEXT) = user_id) cannot use users_pkey, and it stops matching uppercase and no-dash uuids.
  • No timeout or index changes. Every other join and filter in this query already has an index.

Test Plan

  • New TestUserIDInAnotherUUIDFormStillShowsTheName in OrgTokensRepositoryPGTestSuite runs against Postgres. An uppercase and a no-dash user_id still show the user's name. It passes on main too, and fails when the join compares as text.
  • The suite's existing tests cover '', NULL and a soft-deleted user. With the cast but without NULLIF, 5 of its 6 tests fail with an error.
  • TestOrgTokensRepository_prepareDataQuery expects the new SQL.
  • Manual testing completed: EXPLAIN ANALYZE on a local database with many transactions for one org. Before, a nested loop tested every (transaction, user) pair. After, each transaction gets one users_pkey lookup, or the planner picks a hash join.
  • Build and type checking passes
  • go test ./internal/store/postgres/... passes.
  • golangci-lint run ./internal/store/postgres/... reports no issues.

SQL Safety (if your PR touches *_repository.go or goqu.*)

  • Values flow through ? placeholders, goqu.Ex{}, or goqu.Record{} — never fmt.Sprintf or + building a query that gets executed.
  • ToSQL() callers capture and forward params (query, params, err := stmt.ToSQL(); db.…Context(ctx, …, query, params...)). Never query, _, err := ….
  • No ? placeholders inside single-quoted SQL literals in goqu.L (use make_interval(hours => ?)-style functions instead).
  • Any //nolint:forbidigo or // #nosec G20x annotation has a one-line justification on the same line that a reviewer can verify.

The users join hid its match inside a CASE, so Postgres compared every transaction of the org with every user. The join now reads users.id = CAST(NULLIF(user_id, '') AS uuid) and matches the same users.
@vercel

vercel Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
frontier Ready Ready Preview Nov 30, 2026 6:39pm UTC

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Organization token records now correctly resolve user details when billing transaction IDs use uppercase letters or omit UUID dashes.

The organization token query now joins users by casting the transaction user ID after excluding empty values. SQL expectations and PostgreSQL tests cover the updated query and UUIDs in uppercase or without dashes.

Changes: Organization token user lookup

Layer / File(s) Summary
Update and validate the user join
internal/store/postgres/org_tokens_repository.go, internal/store/postgres/org_tokens_repository*_test.go
The query uses NULLIF before casting the transaction user ID to UUID for the user join. SQL expectations reflect the updated placeholders. A PostgreSQL test checks that uppercase and dash-free UUIDs resolve to the user title “Uma.”

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: rohilsurana

Merge Risk: Minimal · up to cf6df

The change rewrites the organization token user join so Postgres can use the user primary key. User matching and hiding of deleted users are unchanged. No merge-blocking risk was found.

Pre-merge checks: Passed 2

Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4e33f29 and cf6df30.

Files selected for processing: 3

  • internal/store/postgres/org_tokens_repository.go
  • internal/store/postgres/org_tokens_repository_pg_test.go
  • internal/store/postgres/org_tokens_repository_test.go

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 46486652282

Coverage decreased (-0.006%) to 56.462%

Details

  • Coverage decreased (-0.006%) from the base build.
  • Patch coverage: 1 of 1 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 42492
Covered Lines: 23992
Line Coverage: 56.46%
Coverage Strength: 22.13 hits per line

💛 - Coveralls

@AmanGIT07
AmanGIT07 merged commit e7ccd43 into main Dec 1, 2026
8 checks passed
@AmanGIT07
AmanGIT07 deleted the fix/org-tokens-user-join branch December 1, 2026 06:01

This branch was successfully deployed

1 active deployment
Preview — cf6df30b Deployed Nov 30, 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.

3 participants