Read & write login.avatar_id alongside legacy avatars.selected#1095
Read & write login.avatar_id alongside legacy avatars.selected#1095Brutus5000 wants to merge 3 commits into
Conversation
The DB now has login.avatar_id as the authoritative single-avatar FK (added in faf/db PR #331). Until all writers move over, both columns must stay consistent. This change: - Adds avatar_id to the login Table definition. - Player fetch picks the avatar via COALESCE(login.avatar_id, avatars.idAvatar) so the new column wins, with fallback to the legacy selected=1 row. - command_avatar's select action now also writes login.avatar_id whenever it updates avatars.selected, so the lobby never produces drift. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughA new ChangesAuthoritative avatar_id on login
Database migration version update
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
server/lobbyconnection.py (1)
976-984: ⚡ Quick winExtend avatar selection tests to assert
login.avatar_idsynchronization.At Line 978, the new authoritative column is written, but existing coverage (per
tests/unit_tests/test_lobbyconnection.pysnippet) only checksavatars.selected. Add assertions forlogin.avatar_idon both select and clear paths to lock the migration contract.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@server/lobbyconnection.py` around lines 976 - 984, The avatar selection logic in the lobbyconnection module now synchronizes the authoritative login.avatar_id column when avatars are selected. Update the tests in tests/unit_tests/test_lobbyconnection.py to add assertions that verify login.avatar_id is correctly synchronized on both the select avatar and clear avatar (if applicable) paths. For each test case that exercises avatar selection, assert that the login record's avatar_id matches the selected avatar_id to ensure the migration contract between the legacy avatars.selected flag and the new authoritative login.avatar_id column is properly maintained.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@server/player_service.py`:
- Around line 111-116: The outerjoin using coalesce with login.avatar_id does
not validate that the avatar is owned by the player. Add an EXISTS or join guard
condition to the outerjoin onclause in the coalesce block to ensure that
non-null login.avatar_id values are only matched to avatars_list rows when there
is a corresponding row where both avatars.idUser equals login.id and
avatars.idAvatar equals login.avatar_id, enforcing ownership validation
consistent with the legacy path validated in the join above it.
---
Nitpick comments:
In `@server/lobbyconnection.py`:
- Around line 976-984: The avatar selection logic in the lobbyconnection module
now synchronizes the authoritative login.avatar_id column when avatars are
selected. Update the tests in tests/unit_tests/test_lobbyconnection.py to add
assertions that verify login.avatar_id is correctly synchronized on both the
select avatar and clear avatar (if applicable) paths. For each test case that
exercises avatar selection, assert that the login record's avatar_id matches the
selected avatar_id to ensure the migration contract between the legacy
avatars.selected flag and the new authoritative login.avatar_id column is
properly maintained.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: d92c8323-d992-4909-81a0-59f76f19d2a2
📒 Files selected for processing (3)
server/db/models.pyserver/lobbyconnection.pyserver/player_service.py
v143 adds login.avatar_id, which the avatar fetch and command_avatar write paths in this PR now reference. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
CodeRabbit flagged two issues on the previous commit: 1. The COALESCE-based avatar join trusted login.avatar_id even if the underlying grant in `avatars` had been revoked. Restructure the join to route through the grant table in both cases: when avatar_id is set, match on idUser+idAvatar (= ownership); otherwise fall back to selected=1. A stale avatar_id pointing at a no-longer-granted avatar now yields no row. 2. test_command_avatar_select only checked avatars.selected. Add an assertion that login.avatar_id mirrors the selected idAvatar, and a new test_command_avatar_select_clear covering the null path (avatars.selected cleared AND login.avatar_id set to NULL). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Summary
login.avatar_idFK column (introduced in faf/db#331) to theloginTable.PlayerService.fetch_player_data) now joinsavatars_listonCOALESCE(login.avatar_id, avatars.idAvatar)— preferring the new authoritative column with a fallback to the legacyavatars.selected = 1row so legacy data still works.command_avatarselect action now also writeslogin.avatar_idwhenever it touchesavatars.selected, so the lobby never produces drift between the two columns going forward.Why
The DB schema is migrating away from "exactly one of N selected rows" toward a single FK on
login. Until all writers are on the new column, both columns must stay aligned. This is the lobby's half — the API will follow oncelogin.avatar_idbecomes its authoritative write path.Test plan
login.avatar_idset → fetch returns that avatar's url/tooltip.login.avatar_idNULL but a legacyavatars.selected = 1row → fetch returns the legacy avatar (backwards compatibility).{"command": "avatar", "action": "select", "avatar": <url>}→ bothavatars.selected = 1ANDlogin.avatar_idare updated to point at that avatar.{"command": "avatar", "action": "select", "avatar": null}→avatars.selectedcleared ANDlogin.avatar_idset to NULL.🤖 Generated with Claude Code
Summary by CodeRabbit