fix: prevent duplicate moodle_token insert when Moodle rotates token …#400
Merged
Merged
Conversation
…399) Surfaced by the new error log page on staging — login for `ucmn-t-67092` (and `harvie`) was failing with duplicate key value violates unique constraint "moodle_token_moodle_user_id_unique" Key (moodle_user_id)=(5) already exists The "rotated token" branch in `MoodleTokenRepository.UpsertFromMoodle` kept the existing row alive (just flipping `isValid = false`) and then called `this.create(...)` to insert a *second* row carrying the same `moodleUserId`. The column-level UNIQUE constraint on `moodle_user_id` rejected the insert at flush time → unhandled 500. Users whose token had not yet rotated (string-equal to the stored one) hit the second branch which updated in place and worked, which is why this was intermittent. Two compounding factors: 1. The find was keyed by `user.id`. When the local `User` row was recreated with a fresh UUID (or the existing token was soft-deleted), the find returned `null`, the create-path ran, and the unique constraint still saw the orphaned row. 2. The global soft-delete filter hid candidate rows that would have matched, so an in-place mutation never happened. Fix: - Look up by `moodleUserId` (the unique key) with `filters: { softDelete: false }` so the find is resilient to rotated tokens, soft-deleted rows, and re-created local users. - On hit, mutate the row in place: new token string, refreshed validation timestamps, rebind `user` to the current local user, clear `deletedAt`. Never create a second row — the unique constraint forbids it and the semantic intent ("one current token per Moodle user") is preserved. - Defensive precondition: throw if `user.moodleUserId` is missing, mirroring `MoodleToken.Create`. Adds repo-level unit tests covering: first-login, validation refresh (same token re-presented), rotated token (the failing case), and soft-deleted row revival. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…(#399)
Surfaced by the new error log page on staging — login for
ucmn-t-67092(andharvie) was failing withduplicate key value violates unique constraint
"moodle_token_moodle_user_id_unique"
Key (moodle_user_id)=(5) already exists
The "rotated token" branch in
MoodleTokenRepository.UpsertFromMoodlekept the existing row alive (just flippingisValid = false) and then calledthis.create(...)to insert a second row carrying the samemoodleUserId. The column-level UNIQUE constraint onmoodle_user_idrejected the insert at flush time → unhandled 500. Users whose token had not yet rotated (string-equal to the stored one) hit the second branch which updated in place and worked, which is why this was intermittent.Two compounding factors:
The find was keyed by
user.id. When the localUserrow was recreated with a fresh UUID (or the existing token was soft-deleted), the find returnednull, the create-path ran, and the unique constraint still saw the orphaned row.The global soft-delete filter hid candidate rows that would have matched, so an in-place mutation never happened.
Fix:
moodleUserId(the unique key) withfilters: { softDelete: false }so the find is resilient to rotated tokens, soft-deleted rows, and re-created local users.userto the current local user, cleardeletedAt. Never create a second row — the unique constraint forbids it and the semantic intent ("one current token per Moodle user") is preserved.user.moodleUserIdis missing, mirroringMoodleToken.Create.Adds repo-level unit tests covering: first-login, validation refresh (same token re-presented), rotated token (the failing case), and soft-deleted row revival.