Repository navigation
fix(store): look up org token users by primary key - #2067
Conversation
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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
Coverage Report for CI Build 46486652282Coverage decreased (-0.006%) to 56.462%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
Summary
SearchOrganizationTokensjoins each billing transaction to the user who made it. The join rule was aCASE, 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
buildBaseQueryinorg_tokens_repository.go: the users join rule is nowusers.id = CAST(NULLIF(billing_transactions.user_id, '') AS uuid).users.idmust stay alone on its side of=so Postgres can use the primary key index (users_pkey), or a hash join. ACASEaround the match, or a cast onusers.id, hides it from both.NULLIFturns an emptyuser_idinto NULL, which matches no user, instead of failing the cast.CAST('' AS uuid)fails. The oldELSE falsealso matched no user.org_tokens_repository_test.goexpects the new SQL. The old rule bound two values (''andfalse) and the new one binds one, so every later placeholder moves down by one.Technical Details
''and NULL find no user. A soft-deleted user's name stays hidden. Auser_idthat 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.idis not safe: Postgres does not promise to check<> ''before the cast.NULLIFfixes the value itself, so the order does not matter.CAST(users.id AS TEXT) = user_id) cannot useusers_pkey, and it stops matching uppercase and no-dash uuids.Test Plan
TestUserIDInAnotherUUIDFormStillShowsTheNameinOrgTokensRepositoryPGTestSuiteruns against Postgres. An uppercase and a no-dashuser_idstill show the user's name. It passes on main too, and fails when the join compares as text.'', NULL and a soft-deleted user. With the cast but withoutNULLIF, 5 of its 6 tests fail with an error.TestOrgTokensRepository_prepareDataQueryexpects the new SQL.EXPLAIN ANALYZEon a local database with many transactions for one org. Before, a nested loop tested every (transaction, user) pair. After, each transaction gets oneusers_pkeylookup, or the planner picks a hash join.go test ./internal/store/postgres/...passes.golangci-lint run ./internal/store/postgres/...reports no issues.SQL Safety (if your PR touches
*_repository.goorgoqu.*)?placeholders,goqu.Ex{}, orgoqu.Record{}— neverfmt.Sprintfor+building a query that gets executed.ToSQL()callers capture and forward params (query, params, err := stmt.ToSQL(); db.…Context(ctx, …, query, params...)). Neverquery, _, err := ….?placeholders inside single-quoted SQL literals ingoqu.L(usemake_interval(hours => ?)-style functions instead).//nolint:forbidigoor// #nosec G20xannotation has a one-line justification on the same line that a reviewer can verify.