Skip to content

Add login.avatar_id FK for selected avatar#331

Merged
Brutus5000 merged 2 commits into
developfrom
feature/login-avatar-fk
Jun 15, 2026
Merged

Add login.avatar_id FK for selected avatar#331
Brutus5000 merged 2 commits into
developfrom
feature/login-avatar-fk

Conversation

@Brutus5000

@Brutus5000 Brutus5000 commented Jun 15, 2026

Copy link
Copy Markdown
Member

Partial solution of #102

Summary

  • Adds a nullable avatar_id column on login with FK to avatars_list(id) (ON DELETE SET NULL, ON UPDATE CASCADE).
  • Backfills login.avatar_id from existing avatars.selected = 1 rows.
  • The legacy avatars.selected flag is intentionally kept and remains user-writable for backwards compatibility. The faf-java-api change (separate PR) mirrors both columns on writes and exposes a reconciler to repair drift.

Test plan

  • Run Flyway against a snapshot of prod; verify login.avatar_id is populated for every user that previously had exactly one avatars.selected = 1 row.
  • Verify users with no selected avatar end up with avatar_id IS NULL.
  • Verify FK behavior: deleting an avatars_list row sets the referencing login.avatar_id to NULL.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Updated database architecture to establish new data relationships
    • Migrated existing data to align with restructured database organization

Adds a nullable foreign key on the login table referencing avatars_list,
making "currently selected avatar" a single authoritative value per user.
The legacy avatars.selected flag is kept (and still writable) for
backwards compatibility; the API mirrors both sides on writes and
provides a reconciler to repair drift.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Migration V143 adds a nullable avatar_id column to the login table with a foreign key constraint referencing avatars_list(id), configured with ON DELETE SET NULL and ON UPDATE CASCADE. It then backfills the new column by joining login to the legacy avatars table on idUser where avatars.selected = 1.

Changes

login.avatar_id Foreign Key Migration

Layer / File(s) Summary
Add and backfill login.avatar_id FK
migrations/V143__login_avatar_fk.sql
Adds avatar_id as a nullable foreign key column on login referencing avatars_list(id) with set-null-on-delete and cascade-on-update, then backfills it from avatars.idAvatar where avatars.selected = 1.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit hops through the login lane,
Linking avatars by their id and name.
The old selected flag lights the way,
Backfilling rows without dismay.
Foreign keys set, cascades in place—
The schema now wears a friendlier face! 🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add login.avatar_id FK for selected avatar' directly and clearly summarizes the main change in the migration: adding a foreign key column to track selected avatars.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/login-avatar-fk

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 and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 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 `@migrations/V143__login_avatar_fk.sql`:
- Around line 13-15: The UPDATE statement directly joins on avatars.selected = 1
without handling cases where a user has multiple selected avatars, resulting in
non-deterministic assignment of avatar_id. Refactor the UPDATE to use a subquery
that selects a single avatar per user using a deterministic rule (such as
MAX(idAvatar)) when multiple selected=1 rows exist for the same idUser. This
ensures that each user gets exactly one avatar_id assigned based on a consistent
tie-breaking rule rather than an arbitrary result from the JOIN.
- Around line 7-10: The avatar_id column definition uses mediumint(8) unsigned
but the referenced avatars_list.id column uses int(11) unsigned, causing a type
mismatch that will prevent the foreign key constraint creation. Change the
avatar_id column type from mediumint(8) unsigned to int(11) unsigned to match
the base type and size of the referenced primary key column in avatars_list.
🪄 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: b7fecc71-991c-4f47-b763-c9f4d25ff1ad

📥 Commits

Reviewing files that changed from the base of the PR and between 3020887 and 05ec55a.

📒 Files selected for processing (1)
  • migrations/V143__login_avatar_fk.sql

Comment thread migrations/V143__login_avatar_fk.sql Outdated
Comment thread migrations/V143__login_avatar_fk.sql
avatars_list.id is int(11) unsigned, not mediumint — the mismatch caused
errno 150 "Foreign key constraint is incorrectly formed".

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Brutus5000 Brutus5000 merged commit de989b4 into develop Jun 15, 2026
3 checks passed
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