Skip to content

fix(orgtoken): cast org_id to text in COALESCE (prevents /org/tokens 500) - #1696

Merged
HongmingWang-Rabbit merged 2 commits into
mainfrom
fix/orgtokens-uuid-coalesce
Apr 23, 2026
Merged

fix(orgtoken): cast org_id to text in COALESCE (prevents /org/tokens 500)#1696
HongmingWang-Rabbit merged 2 commits into
mainfrom
fix/orgtokens-uuid-coalesce

Conversation

@HongmingWang-Rabbit

Copy link
Copy Markdown
Contributor

Summary

  • orgtoken.List queries COALESCE(org_id,'') but org_id is a UUID column — Postgres can't cast '' to UUID, fails with pq: invalid input syntax for type uuid: ""
  • Fix: COALESCE(org_id::text,'') — operates on matching types
  • No Go-side changes: Token.OrgID is already string, scan still works

Symptom (prod, hongmingwang)

2026/04/22 23:33:14 orgtoken list: orgtoken: list: pq: invalid input syntax for type uuid: ""
[GIN] 2026/04/22 - 23:33:14 | 500 | 835.234µs | 127.0.0.1 | GET "/org/tokens"

Tenant Settings → Org Tokens page broken.

Why tests didn't catch it

sqlmock (used in tokens_test.go) doesn't model the pq driver's type coercion. It accepts any AddRow value for any declared column, so the query-level type mismatch is invisible. Real fix needs a real-Postgres integration harness; filed as follow-up.

Test plan

  • go test ./internal/orgtoken/ — existing tests still pass (regex SELECT id, prefix.*FROM org_api_tokens.*ORDER BY created_at DESC still matches)
  • Live prod Postgres verified: SELECT COALESCE(org_id::text,'') FROM org_api_tokens LIMIT 5 executes without error
  • After deploy: GET /org/tokens returns 200 on hongmingwang

Audit

Searched for sibling COALESCE(uuid_col, '') patterns across workspace-server. Only one other hit: terminal.go uses COALESCE(instance_id, '') but instance_id is text (EC2 IDs like i-xxx), not UUID — safe.

🤖 Generated with Claude Code

Symptom (prod tenant hongmingwang):
  GET /org/tokens → 500
  orgtoken list: orgtoken: list: pq: invalid input syntax for type uuid: ""

Postgres rejects COALESCE(uuid_col, '') because it can't cast the
empty string to UUID. Cast to ::text first so the COALESCE operates
on matching types. OrgID on the Go side is already string, so no
scan changes needed.

sqlmock doesn't exercise pq type coercion — it accepts any AddRow
value for any column — which is why the existing tests pass while
prod 500s. Real-Postgres integration coverage is the systemic fix
(tracked separately), but this PR unblocks the Settings → Org Tokens
page today.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@HongmingWang-Rabbit
HongmingWang-Rabbit changed the base branch from staging to main April 23, 2026 00:24
@HongmingWang-Rabbit
HongmingWang-Rabbit merged commit 470e824 into main Apr 23, 2026
9 of 10 checks passed
@HongmingWang-Rabbit
HongmingWang-Rabbit deleted the fix/orgtokens-uuid-coalesce branch April 24, 2026 00:11
HongmingWang-Rabbit pushed a commit that referenced this pull request Jun 12, 2026
#1698)

Co-authored-by: agent-dev-a <agent-dev-a@agents.moleculesai.app>
Co-committed-by: agent-dev-a <agent-dev-a@agents.moleculesai.app>
HongmingWang-Rabbit pushed a commit that referenced this pull request Jun 12, 2026
…nses (#1699)

Co-authored-by: agent-dev-b <agent-dev-b@agents.moleculesai.app>
Co-committed-by: agent-dev-b <agent-dev-b@agents.moleculesai.app>
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