Skip to content

Dont create students roles with null user_id - #1108

Open
zetter-rpf wants to merge 2 commits into
mainfrom
dont-create-students-with-null-user-id
Open

zetter-rpf wants to merge 2 commits into
mainfrom
dont-create-students-with-null-user-id

Conversation

@zetter-rpf

@zetter-rpf zetter-rpf commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Status

What's changed?

  • handle failure student creation responses from profile correctly
  • add an null: false constraint to user_id on the roles table

See commits for more

Steps to perform after deploying to production

Check that the migration will still work by running Role.unscoped.where(user_id: nil).count

@cla-bot cla-bot Bot added the cla-signed label Nov 12, 2026
@zetter-rpf
zetter-rpf marked this pull request as ready for review November 12, 2026 15:34
@zetter-rpf zetter-rpf changed the title Dont create students with null user Dont create students roles with null user_id Nov 12, 2026
@macroscopeapp

macroscopeapp Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Approvability

Verdict: Needs Human Approval

Macroscope's review found this PR requires human approval — The change prevents failed student creations from producing roles and adds a database-wide NOT NULL constraint. Confirm production roles—including archived records—contain no null user IDs before migration; the reported check excludes archived roles, leaving deployment readiness unresolved.

Needs human approval because:

  • approvability is in preview mode for this repository, so Macroscope does not approve PRs here

You can add or adjust custom eligibility rules. Learn more.

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Test coverage

94.18% line coverage reported by SimpleCov.
Run: https://github.com/RaspberryPiFoundation/editor-api/actions/runs/43251298450

zetter-rpf and others added 2 commits November 12, 2026 15:45
Previously we were incorrectly assuming that all records in the profile response where successful student creations. However, some records may be errors. These errors don't have a student id and have a success: false as well as an error row (see [1])

This caused the creation of students with a `null` user_id, which later caused errors.

Make sure we only log successful creations.

I'm logging the errors so we can see what caused them. There's a try/catch in profile with a wide scope so it's not clear what's gone wrong. I'm assuming it might be a failed creation due to a concurrency issue.

[1] - https://github.com/RaspberryPiFoundation/profile/blame/main/app/lib/createGoogleSSOStudents.js

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
We were incorrectly handling errors from creating students and trying to create students without an id (see previous commit for details. Since this was a bulk create, we bypassed the model validation

This is a extra safeguard to prevent this happening again.

I've checked and we have none in our database at the moment - `Role.where(user_id: null).count`

This branch was successfully deployed

1 active deployment
editor-api-p-dont-creat-s6kmic — b661415f Deployed Nov 12, 2026 by raspberrypiherokubot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants