Skip to content

One User per person across all offerings, like seating - #16

Merged
pancakereport merged 6 commits into
mainfrom
single-user-model
Oct 26, 2026
Merged

pancakereport merged 6 commits into
mainfrom
single-user-model

Conversation

@pancakereport

@pancakereport pancakereport commented Oct 9, 2026 •

Copy link
Copy Markdown

Summary

Removes Account and makes User one person across all offerings, like seating's User.

Before After
A person account row (if they logged in) + one user row per offering one users row
Role in an offering is_staff / is_admin on that offering's user row staff_offerings, admin_offerings, student_offerings sets on the user
current_user inside an offering that offering's user row the person; is_staff / is_admin answer for the current offering

The role sets hold offering keys (the course column value), not Canvas IDs as in seating, because offerings moved from the monorepo aren't all linked to Canvas. Login still records the user's Canvas courses, and each offering's role is synced from them on the first request inside that offering, as before.

Limited to the current offering: a person's sections and attendance now span offerings, so everything that reads or changes them is limited to the offering being viewed. That covers refresh, join/switch section, leave all, remove students (all or tutoring), reset, attendance export, the drop list and attendance history. Without this, switching labs in one course would remove a lab in another, and "leave all" would clear every course. Tests cover each case.

Reset Sections Tool no longer deletes people. It deletes the offering's sections, their sessions and attendance, and every student and staff assignment to them; everyone stays in the offering with their roles. The confirmation text says so.

Identity: people are matched by Canvas user ID. Email is only used to claim a record that has never logged in (added by staff or imported from a spreadsheet). If a logged-in person's email changes to one held by an unclaimed record, the two are merged. A new login whose email belongs to a different Canvas account gets a 403 rather than taking that record over; fixing that rare case (someone with a new bCourses account) is a manual database edit, by decision.

Migrations

Adds Flask-Migrate.

  • 0001_baseline: the schema as of Serve every bCourses course from one deployment, with seating's routes #14. A no-op on databases that already have it, which includes staging.

  • 0002_single_user:

    • creates users and merges the per-offering rows by email; the newest row's name wins, or the Canvas name for anyone who has logged in
    • re-points enrollments, attendance and section staff
    • drops account, the old user, and the unused course, slot, survey, user_section_junction and session.slot_id

    It's one transaction on Postgres, and can't be downgraded.

The container runs flask db upgrade on start only with RUN_MIGRATIONS=true (staging and production; never PR previews, which share the staging database). A Postgres advisory lock makes concurrent instances wait instead of racing.

Rehearsed on Postgres 18 using staging's exact schema (from pg_dump --schema-only, no data) with generated data at staging's scale: 8,529 rows → 6,954 people, 1,400 in several courses. Every person's roles and name, and every enrollment (10,955), attendance record (19,543) and section staff link, matched independently computed expectations. It took about 2 seconds. The rehearsal found and fixed two problems before they reached staging:

  • session.slot_id (a foreign key to slot) would have blocked dropping slot.
  • Re-pointing user_section could briefly collide with its primary key, because new person ids overlap old row ids. Re-pointing now goes through negative ids.

flask db check against the result shows users matches the models. Other differences (pgloader index names, ON DELETE RESTRICT, a few missing indexes, flask_sessions) predate this PR and are left for a separate cleanup.

Rollout

  1. Back up the staging database: gcloud sql backups create --instance staging-cdss-web-apps-2 --project staging-cdss-web-apps.
  2. Set RUN_MIGRATIONS=true on sections-staging (the current image ignores it).
  3. Merge. The staging deploy runs the migration on start.

This PR's preview won't work until staging is migrated, because the preview expects the users table. Use the Postgres rehearsal above, or check the preview after merging.

Test plan

  • 22 tests: the existing ones plus cross-offering cases (separate state, join, leave all, remove, history), reset keeping people and their roles, claiming an imported person on first login, old account- cookies ignored, and SQLite migration tests (merge plus fresh database)
  • Postgres 18 rehearsal on staging's schema at staging's scale, plus concurrent flask db upgrade on an empty database
  • After merge: staging migrates on deploy; log in and check an offering you're in

🤖 Generated with Claude Code

Replace the Account table and the per-offering user rows with a single
`users` table: one row per person, with their roles in each offering as
sets of offering keys (seating stores Canvas IDs; keys also cover offerings
not linked to Canvas). Inside an offering, is_staff and is_admin answer for
that offering, and login syncs the role from Canvas as before.

Since a person's sections and attendance now span offerings, every
operation that reads or changes them is limited to the current offering:
refresh, join/switch, leave all, remove students, reset, exports, history.

Add Flask-Migrate. 0001 is the schema as of #14 (a no-op on existing
databases); 0002 merges per-offering rows by email, re-points enrollments,
attendance and section staff, and drops account, the old user table and
the unused course, slot, survey and user_section_junction tables. Deploys
with RUN_MIGRATIONS=true run `flask db upgrade` on start, under a Postgres
advisory lock; PR previews never migrate the shared staging database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

🚀 Staging Preview Deployed!

URL: https://preview-sections-pr-16-137456085712.us-west1.run.app

pancakereport and others added 3 commits October 9, 2026 12:40
Reset now deletes the offering's sections, their sessions and attendance,
and every student and staff assignment to them, but leaves everyone in the
offering with their roles. Update the confirmation text to match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread server/login.py Outdated
account.canvas_courses = courses
account.is_global_admin = override_id is not None and str(override_id) in courses
# People added by staff or imported from a spreadsheet have a User (found
# by email) before they ever log in.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

found by email can be problematic. individuals are able to change their email, so while emails are always unique, a single user may have two associated emails.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed, and it was worse than a missed match: the email lookup could return a user who already had a different Canvas ID and overwrite it, which would let someone who set their email to another person's take over that record. Fixed in 9957c04:

  • Login matches by Canvas ID, which doesn't change. Email is only used to claim someone who has never logged in (added by staff or imported from a spreadsheet, so no Canvas ID yet). A new login whose email belongs to a different Canvas user is refused with a 403 instead of taking it over.
  • Email changes: a logged-in person's new email is saved. If the new email belongs to an unclaimed imported record, that's the same person, so it's merged in (sections, attendance, sections they teach, roles). If it belongs to another Canvas user, the existing email is kept.
  • Add Students (the Canvas lookup) now records the student's Canvas ID, so they're matched by ID from the start.

Tests cover each case: no takeover, email change, merge (including attendance and section staff), and an email owned by another account.

Two limits remain, because spreadsheets and the monorepo data only have emails:

  • A person imported under one email who logs in with a different one gets a separate record until their Canvas email matches the import.
  • The migration merges by email, so someone who appeared under two different emails in different courses stays two people. There's nothing else in that data to match them on. If that comes up, an admin merge command (using the same absorb helper) would be a small follow-up.

pancakereport and others added 2 commits October 26, 2026 15:19
Emails can change, so:
- Login matches by Canvas id. Email only claims someone who has never
  logged in (no Canvas id), so an email can't take over another account;
  a new login whose email belongs to a different Canvas user is refused.
- When a logged-in person's email changes, it's updated. If the new email
  belongs to an unlinked imported record, that's the same person, so it's
  merged in (sections, attendance, taught sections, roles). If it belongs
  to another Canvas user, the existing email is kept.
- Adding a student through the Canvas lookup records their Canvas id.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Enrollment import replaced a same-type section in every offering the
  student was in; it now only replaces one in the current offering.
- Email lookups ignore case, matching how the migration merged people.
- Merging someone imported under a new email keeps one section of each type
  per offering and one attendance record per session.
- Global admins count as members of every offering again (sudo API,
  fetch_user, get_userid, imports naming them as section staff).
- Role-set matching escapes LIKE wildcards, so "cs61a_tutoring" can't
  match another key.
- start_session only accepts the current offering's sections.
- Migration 0002 stops with a clear error on user rows without an email or
  on unexpected tables referencing user, skips roles for rows without an
  offering, and drops the leftover tables before user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pancakereport
pancakereport merged commit 04b2445 into main Oct 26, 2026
10 checks passed
@pancakereport
pancakereport deleted the single-user-model branch October 26, 2026 22:56
@pancakereport pancakereport mentioned this pull request Oct 26, 2026
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