Repository navigation
One User per person across all offerings, like seating - #16
Conversation
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>
|
🚀 Staging Preview Deployed! URL: https://preview-sections-pr-16-137456085712.us-west1.run.app |
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>
| 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
absorbhelper) would be a small follow-up.
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>
Summary
Removes
Accountand makesUserone person across all offerings, like seating'sUser.accountrow (if they logged in) + oneuserrow per offeringusersrowis_staff/is_adminon that offering'suserrowstaff_offerings,admin_offerings,student_offeringssets on the usercurrent_userinside an offeringuserrowis_staff/is_adminanswer for the current offeringThe role sets hold offering keys (the
coursecolumn 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:usersand merges the per-offering rows by email; the newest row's name wins, or the Canvas name for anyone who has logged inaccount, the olduser, and the unusedcourse,slot,survey,user_section_junctionandsession.slot_idIt's one transaction on Postgres, and can't be downgraded.
The container runs
flask db upgradeon start only withRUN_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 toslot) would have blocked droppingslot.user_sectioncould briefly collide with its primary key, because new person ids overlap old row ids. Re-pointing now goes through negative ids.flask db checkagainst the result showsusersmatches 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
gcloud sql backups create --instance staging-cdss-web-apps-2 --project staging-cdss-web-apps.RUN_MIGRATIONS=trueonsections-staging(the current image ignores it).This PR's preview won't work until staging is migrated, because the preview expects the
userstable. Use the Postgres rehearsal above, or check the preview after merging.Test plan
account-cookies ignored, and SQLite migration tests (merge plus fresh database)flask db upgradeon an empty database🤖 Generated with Claude Code