Skip to content

Keep one user row per person, as sections does - #40

Merged
pancakereport merged 5 commits into
mainfrom
one-user-per-person
Nov 25, 2026
Merged

pancakereport merged 5 commits into
mainfrom
one-user-per-person

Conversation

@pancakereport

@pancakereport pancakereport commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replaces the account table and the per-course user rows with one users row per person, copied from sections' User (and its 0002_single_user migration).

  • users (oh_queue/models.py): email and Canvas ID unique (Canvas ID empty for people moved from the per-course sites who haven't signed in since), is_global_admin, canvas_courses, and roles as sets of course keys (staff_offerings, admin_offerings, student_offerings). Inside a course, is_staff and is_admin answer for that course, as in sections. set_role, is_member_of, canvas_roles_in, absorb and with_email are sections'.
  • Sign-in (oh_queue/login.py): as in sections. A user is found by Canvas ID. A first sign-in claims a moved user with the same email. A changed email merges a moved user under the new email. An email that belongs to another bCourses account is refused. Opening a course syncs the person's role there from Canvas (sync_offering_role). Session ids are prefixed user-, so old account- cookies sign people out instead of matching the wrong row.
  • Online Setup links (call_url, doc_url) are now per person, not per course. The Online Setup page says so: a note above the form, read with each field, and "(all courses)" in both labels.
  • Heartbeats get their own table, one per person per course, so having one course's queue open doesn't show you as online in another. Before this, connect set the heartbeat but never committed it, so the online counts only included staff helping a ticket. It commits now. Two tabs creating the first heartbeat at the same moment don't cause an error.
  • Leaving a group (which joining or starting one does first) only looks at the current course's groups, so a group in one course doesn't pull the person out of their group in another.
  • list_users lists the course's members. get_user shows anyone in the course, or anyone who was helped or helped in it (global admins have no role in a course of their own).
  • logged_in no longer reads current_user.course, which no longer exists. It now just requires a signed-in user; people outside the course are already turned away at sign-in.

Migration (5b9e2c7d1f30)

  • Merges the old user rows by email, ignoring case. The newest name and Online Setup links win, and a signed-in account's Canvas name and details win over both.
  • Each person keeps the id of their oldest row, so only references to their other rows change, in one UPDATE … FROM per column (ticket.user_id/helper_id, ticket_event, group_attendance, chat_message). Rows are inserted in batches.
  • Rows that refer to a user row that no longer exists get a placeholder person under that id ("Unknown user ", an @oh-queue.invalid email), so their history stays.
  • Rows with no email that nothing refers to are skipped.
  • Before changing anything, it stops with a list of what to fix if a row with no email is referred to, if one email belongs to two bCourses accounts, or if another table references user.
  • Drops account and user. It can't be undone (downgrade raises), as in sections.

Rolled-back run on staging's data (84,689 user rows, 544k tickets, 2.1M ticket events): it finished in 174s, with 31,789 people, nothing left dangling, and all foreign keys on users. Then it rolled back; staging is unchanged. Staging's Cloud Run startup probe allows 240s, and the migration runs before the server listens, so it fits but without much margin. That run also found what the placeholders and the email-less skip handle: 34 cs161 tickets (2020–22) belonging to missing user 14132 (ticket.user_id's foreign key is NOT VALID), and one 2020 staff row with no email. Prod likely has both.

Also tested on SQLite (tests/test_migrations.py) and on a local Postgres with copy-style tables.

Not copied from sections

These belong in separate PRs if we want them:

  • the sign-in max age and dropping the remember cookie
  • server-side Canvas tokens
  • API keys and canvas_synced_at
  • pronouns (Add pronouns to OH queue #3)

Test plan

  • pytest (167 passed), covering every changed line and branch in oh_queue/ and the migration, including tests/test_users.py (roles, lookups, presence per course, group cleanup, merging) and the migration's refusals and edge cases
  • pytest tests/e2e tests/a11y (96 passed)
  • flake8
  • Migration run on Postgres, locally and rolled back on staging

🤖 Generated with Claude Code

Replace the account table and the per-course user rows with one `users`
row per person, copied from sections' User: email and Canvas ID unique,
roles in each course as sets of course keys (staff, admin, student), and
the Canvas courses and global admin flag that were on the account.
Signing in claims a user moved from the per-course sites by email, and
merges one under a changed email, as sections does.

Heartbeats move to their own table, one per person per course, so being
online in one course doesn't count in another. The connect handler now
commits them; it didn't before, so nobody showed as online.

The migration merges the old rows by email (ignoring case), the newest
name and Online Setup links winning and a signed-in account's Canvas
details over both, and re-points tickets, ticket events, group attendance
and chat messages at the merged rows. Adapted from sections'
0002_single_user; it can't be undone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.80%. Comparing base (27fc08d) to head (f3a7cf3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #40      +/-   ##
==========================================
+ Coverage   78.56%   80.80%   +2.23%     
==========================================
  Files          14       14              
  Lines        1418     1500      +82     
  Branches      198      209      +11     
==========================================
+ Hits         1114     1212      +98     
+ Misses        246      237       -9     
+ Partials       58       51       -7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown

🚀 Staging Preview Deployed!

URL: https://preview-oh-pr-40-137456085712.us-west1.run.app

pancakereport and others added 4 commits November 24, 2026 16:39
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With one row per person, joining or starting a group in one course took
the person out of their group in another course, which could delete that
group's ticket. Leaving now only looks at the current course's groups.

The migration now refuses, before changing anything, references to user
rows that don't exist (which it would have blanked, or stopped partway
on), and one email shared by two bCourses accounts (where keeping either
would lock the other out), listing what to fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds tests/test_users.py: roles in each course, global admins, looking
people up (list_users, get_user), who's online in each course, old and
racing heartbeats, closing groups everyone has left, and merging two
rows' heartbeats and roles.

The migration's tests now cover a row with no email, a blank name,
tables without foreign keys, an empty database, an unknown table
referencing users, and the downgrade refusing. The unknown-table check
moves before anything changes, as SQLite can't roll back a half-done
migration, and two branches that can't happen here go (old rows always
have a course, and the account table always exists at this revision).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… tickets

A rolled-back run on staging's database (84,689 user rows, 544k tickets,
2.1M ticket events) took over 30 minutes before it was stopped: it rewrote
every reference twice and inserted rows one statement at a time. Each
person now keeps their oldest row's id, so only references to their other
rows change, in one UPDATE ... FROM per column, and rows go in in batches.
The same run now takes about 3 minutes, inside Cloud Run's 240-second
startup window.

The run also found two things staging (and likely prod) has:
- 34 cs161 tickets whose user row doesn't exist (the foreign key was added
  NOT VALID). They get a placeholder person, "Unknown user <id>", under the
  missing id, so their history stays.
- One staff row from 2020 with no email, which nothing refers to. Rows
  like it are skipped; one that something refers to still stops the
  migration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pancakereport
pancakereport merged commit 04b9f8f into main Nov 25, 2026
14 checks passed
@pancakereport
pancakereport deleted the one-user-per-person branch November 25, 2026 02:05
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.

2 participants