Skip to content

test(auth): check the API key verifier index in pg_index, not a plan - #2824

Closed
blurbery wants to merge 1 commit into
Silo-Server:mainfrom
blurbery:test/api-key-index-check
Closed

blurbery wants to merge 1 commit into
Silo-Server:mainfrom
blurbery:test/api-key-index-check

Conversation

@blurbery

@blurbery blurbery commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #2814
Related issue: N/A
Validation tasks: none

TestAPIKeyVerifierUpgrade fails the ci / Go DB pins job whenever another package's test has a write transaction open at the moment it asks for a plan, and passes when run alone. Nothing is wrong with the index. It failed twice in a row on #2822, where the Go version change leaves the build cache cold and shifts when packages start.

The test runs the verifier migration and then expects EXPLAIN of the key_hash lookup, with enable_seqscan off, to use an index. The migration drops api_keys_api_key_key, rewrites api_key, and adds the constraint back on key_hash. With no index on the column during the rewrite, those updates are HOT, so the rebuilt index has indcheckxmin set. Postgres then won't use it in any transaction whose snapshot is older than the migration's own transaction, so while an older transaction is open anywhere on the server, the plan is a disabled sequential scan. In a deployment this is harmless and clears as soon as those older transactions end.

Approach

Check pg_index instead of a plan: api_keys must have a valid, unique, non-partial B-tree whose only key column is key_hash. That keeps the test's intent (the existing unique B-tree still supports a bounded lookup) without depending on what else the database server is doing. The migration is unchanged.

Validation

On a local PostgreSQL 18.6 database, running only TestAPIKeyVerifierUpgrade:

  • Before the change it passes alone, and fails with verifier lookup has no usable index (the same line as in CI) while another session holds a transaction that called txid_current() before the test started.
  • After the change it passes in both cases.
  • After the change it still fails when the migration's ADD CONSTRAINT api_keys_api_key_key UNIQUE (key_hash) line is removed.
  • A plain SQL reproduction shows the mechanism: after the same drop, update, rename and add sequence, pg_index.indcheckxmin is true, the plan is Seq Scan with Disabled: true while an older write transaction is open, and Index Scan using api_keys_api_key_key once it ends.
  • gofmt and go vet ./internal/auth/ are clean.
  • This branch's CI: every check passes on a53074d, including ci / Go DB pins. ci: use the latest Go 1.26 patch, not the runner's cached one #2822 carries the same commit so its own DB pins job can pass.

Evidence

Evidence: none, no user-visible change

Risks

None identified. Only the test changes.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • The Evidence section shows every change a user can see, or says there is none.

AI Disclosure

  • Harness: Claude Code (Claude desktop app)
  • Tool(s): Claude Code
  • Model(s): claude-opus-5-5
  • Involvement: AI-assisted with Claude Opus. I directed the task and designed the work.
  • Adversarial review: n/a, a test-only change; the reproduction and the missing-index check above cover both directions

TestAPIKeyVerifierUpgrade asked the planner for a plan with
enable_seqscan off and expected an index scan on key_hash. The verifier
migration rewrites api_key while its unique constraint is dropped, so
those updates are HOT, and the constraint it adds afterwards gets a
B-tree with indcheckxmin set. The planner ignores that index while any
transaction older than the migration is still open. In the database
contract run another package's test often holds one, so the test failed
there and passed alone.

Read pg_index for a valid, unique, non-partial B-tree on key_hash
instead. The test still fails when the migration leaves key_hash without
that index.
@silo-triage silo-triage Bot added the size/S 0–49 added + deleted lines (all files); change volume, not priority. label Oct 21, 2026
@silo-kody

silo-kody Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8bff3d75-3c8f-4975-9213-6d81f9c5c2fc
📥 Commits

Reviewing files that changed from the base of the PR and between 174fb9a and a53074d.

📒 Files selected for processing: 1
  • internal/auth/api_key_storage_test.go

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

📝 Walkthrough

The verifier migration test replaces an EXPLAIN plan check with a direct query of PostgreSQL index metadata. It requires a valid, unique, single-column B-tree index on api_keys.key_hash.

Changes: Verifier index check

Layer / File(s) Summary
Check index metadata
internal/auth/api_key_storage_test.go
The test queries PostgreSQL catalog metadata for a valid, unique, single-column B-tree index on api_keys.key_hash. It fails if the query errors or no qualifying index exists.

Priority: ⬇️ Low

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

Change: Other · Severity of issue fixed: Low

Suggested reviewers: quick104

Merge Risk: ⚪ Minimal · up to a5307

The PR only improves a database contract test and introduces no established production risk.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly identifies the main change: the auth test now checks the API key verifier index in pg_index instead of using an EXPLAIN plan.
Linked Issues check Passed Issue #2814 requires TestAPIKeyVerifierUpgrade to pass reliably in the database contract run and to fail when the verifier lookup lacks its index. The test now checks pg_index for a valid, unique, non…
Out of Scope Changes check Passed The diff changes only internal/auth/api_key_storage_test.go. The change supports issue #2814 by replacing the intermittent planner check with direct catalog validation. No production behavior or unrel…
Description check Passed The pull request description directly explains the test failure, the change from planner-based validation to pg_index inspection, and the reported validation results. It matches the changeset.
Full details: Linked Issues check

Explanation

Issue #2814 requires TestAPIKeyVerifierUpgrade to pass reliably in the database contract run and to fail when the verifier lookup lacks its index. The test now checks pg_index for a valid, unique, non-partial, single-column B-tree index on api_keys.key_hash. It removes the planner-dependent EXPLAIN check. The migration remains unchanged.

Full details: Out of Scope Changes check

Explanation

The diff changes only internal/auth/api_key_storage_test.go. The change supports issue #2814 by replacing the intermittent planner check with direct catalog validation. No production behavior or unrelated scope is changed.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • 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

Comment @coderabbitai help to get the list of available commands.

@silo-triage silo-triage Bot added bug Something isn't working go Pull requests that update go code priority: P2 Limited scope, workaround exists, or polish labels Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code priority: P2 Limited scope, workaround exists, or polish size/S 0–49 added + deleted lines (all files); change volume, not priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] TestAPIKeyVerifierUpgrade fails in the DB contract run but passes alone

1 participant