Speed up AddGameToLibrary, cover/avatar removal, and the navbar avatar fetch #5047
Reviewed all five commits. No blocking issues found, and CI is green.
Ratings (Game#update_ratings)
GamePurchase after_create / after_update / after_destroy, and GameMergeService#handle_purchases. Game has no save callbacks of its own (Searchable/GlobalSearchable/CachedRowCount add none), and has_paper_trail already ignores avg_rating, ratings_count and updated_at, so update_columns drops nothing the old update did besides validations.BigDecimal#round(1) result. I checked every 0.0 to 100.0 step at one decimal place in Ruby and Float == BigDecimal matches for all of them, so it won't miss a write or loop.update dirty check did, so a stale instance behaves the same as before.AddGameToLibrary: new + save is right. The old create + save ran after_update (so update_ratings) a second time even with nothing changed. The error path is unchanged.
Uniqueness / migration: the uniqueness validation is still there and now uses the index. algorithm: :concurrently + disable_ddl_transaction! is right, and the migrate job doesn't get DB_STATEMENT_TIMEOUT (only Puma sets it), so the build can't be cut short. If a concurrent build ever fails it leaves an INVALID index that blocks a rerun, but at this table size that's very unlikely. Non-unique is the safer choice; a unique index could follow after checking for duplicates.
purge_later: the attachment row is deleted synchronously, so attached? is false and the response shows no cover/avatar straight away. One trade-off: with the :async adapter the job lives in Puma's memory, so a deploy or restart between the mutation and the job can leave an orphaned blob in R2. That's rare and the orphan cleanup task already handles it.
Avatar throttle
avatarUrl: null (keeping avatarCheckedAt), so both update the navbar straight away.canFollow excludes it), so it can't leave the navbar stale.setAuth replaces the stored user, so avatarCheckedAt doesn't carry over to a different account.NavAvatar falls back to the initial. Fine as designed.avatarUrl but doesn't set avatarCheckedAt, so the navbar still fetches once right after sign-in. Setting it in useAuth (password and passkey paths) would save that request. It's one request per sign-in, so I didn't push it.Looks good to merge.
---
_Generated by Claude Code_
Thanks for the review. I'm leaving the optional nit about setting avatarCheckedAt at sign-in for now. It only saves one request per sign-in, and pushing it would re-run CI on a PR that's already green. If we touch useAuth again, I'll fold it in then.
---
_Generated by Claude Code_
connorshea
<!-- claude-projects-attribution: {"github_login":"connorshea"} -->
_Requested by Connor · project thread_
Fixes the root causes Sentry showed for the three slow requests left after the sfo3 move. Numbers are sampled transactions from the last 3 days.
| Request | p50 | p95 |
| --- | --- | --- |
| AddGameToLibrary | 2.0s | 3.9s |
| RemoveGameCover | 3.7s | 5.6s |
| GetCurrentUserAvatar | 0.7s | 2.4s |
AddGameToLibrary
In trace
6307b543, twoSELECT 1 FROM games WHERE pcgamingwiki_id = $1 AND id != $2queries took 422ms and 249ms. They came from Game's uniqueness validation, run byGame#update_ratingsafter each library change, and it ran twice because the mutation didGamePurchase.createand thensave.games.pcgamingwiki_id. It was the only uniquely-validated external ID column without an index, so every game save did a sequential scan. The index is non-unique and built concurrently, so it can't fail on duplicates that may already be in prod.update_ratingswrites its columns directly. It now usesupdate_columns, skipping the per-external-ID uniqueness queries, the blocklist check and the search-document lookup, none of which a rating change can affect. It also skips the write when nothing changed. It still bumpsupdated_atlike before.GamePurchase.new+saveinstead ofcreate+save.RemoveGameCover / RemoveUserAvatar
In trace
6beeba4a, the request waited 562ms + 1103ms on R2 deletes (purgeis synchronous) plus the variant purges. Both mutations now usepurge_later, which detaches right away and deletes the files inActiveStorage::PurgeJob.GetCurrentUserAvatar
The server work here is tiny: 6 queries, about 15ms when warm (the fastest samples are 14-50ms). The slow samples are wait time. In trace
8748e732, 850ms passed before the controller started, and there were 100-500ms gaps between queries. That's because the navbar fires the request on every full page load, alongside the page's 5-8 other queries, and they all compete for the same Puma worker. The avatar is already stored with the signed-in user, so this request never blocked rendering.The navbar now records when it last fetched the avatar (
avatarCheckedAton the stored user) and skips the request for an hour. Uploading an avatar in Settings forces a refresh. This takes one request out of most page-load bursts. The requests that still happen will still be as slow as whatever they run alongside.Tests
PurgeJob. Each one fails without its fix. Vitest covers the avatar throttle and the force flag.After deploy
The migration runs automatically. Nothing else to do.
🤖 Generated with Claude Code
https://claude.ai/code/session_0151yw9VkWFPsgxpvvGU42gs