Steps 1 and 3 of docs/calendar-auth-migration.md. The calendar is the last module still authenticating against its own users/sessions tables; this is the groundwork that lets step 4 swap it for the shared admin identity. An event now records its creator twice: created_by_id, the legacy INT into the calendar database's own users table, and created_by_user_id, the admin module's VARCHAR(36) id. The two live in different databases, so there is no foreign key and no join - a cross-schema reference would tie the schemas' lifecycles together, and the name is instead resolved through one lookup per result set against the admin database. The creator is only ever rendered as a name; nothing authorises on it. That is what makes the planned account backfill unnecessary - dropped by decision - and what makes the read degrade rather than fail: an admin id that no longer resolves falls back, and an unreachable admin database costs a name rather than the response. The public calendar is read anonymously by nachklang.art and has never depended on the admin database being up. Since there is no backfill, step 5 dropping the legacy users table would have erased the authorship of every pre-cutover event. Migration 002 brings that part of step 5 forward and snapshots the names onto the events themselves, so the data is safe well before the table holding it goes away. The same SELECT and row mapper existed in four copies; collapsed to one of each first, so the dual read is written once rather than four times. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
10 KiB
Migrating the Calendar domain onto the admin identity module
Status: steps 1 and 3 done (2026-09-06), step 2 dropped by decision, part of step 5 brought forward, step 4 next.
Written 2026-09-05 alongside the admin module (step 2 of docs/plan-admin-auth.md in the
nachklang-admin repo), which deliberately left the calendar alone. Steps 1-4 of that plan
are now live, so the calendar is the last module still on the legacy query-parameter
sessions.
Why the calendar was left out
The admin module replaced authentication for feedback and tickets by swapping one middleware. The calendar cannot be done that way, because its user identity is woven into its data:
users/sessionslive in the calendar database and are the same tables the feedback and tickets admin areas used to authenticate against.events.created_by_idis an INT foreign key intousers.user_id. The admin module's user ids are VARCHAR(36) strings. Migrating identity means migrating that column and every query that joins it.- The Angular frontend passes
sessionId/sessionKeyas query parameters (DEFERRED_SECURITY.mditem 1). Cookie sessions remove the parameters entirely, so every calendar route signature and the frontend's HTTP layer change together. credentials.service.tsimplements a second, parallel authorisation model: theMEMBER_CREDENTIAL/CHOIR_CREDENTIAL/MANAGEMENT_CREDENTIALshared secrets that let non-users read specific calendars. That has no equivalent in the admin module and is not a per-user permission at all.
What already exists today: calendar is a value in the user_app_permissions.app enum, so
permissions can be granted before anything else moves.
What is in place to build on
- Cookie sessions across
*.nachklang.art, andrequireAppAccess('calendar')insrc/models/admin/admin.middleware.ts- usable the moment a calendar route wants it. res.locals.adminis{id, email, displayName, apps};idis the string user id.- Invitations, disable/enable and session revocation already cover calendar users, because they are properties of the account rather than of an app.
Suggested sequence
Each step is meant to leave production working on its own.
-
Add a bridging column.
Done 2026-09-06, asALTER TABLE events ADD COLUMN created_by_user_id VARCHAR(36) NULL, indexed. Nothing reads it yet.sql/calendar/001_add_admin_user_bridge.sql- the first migration this repo owns for the calendar schema, mirrored intodocker/init/01-calendar-schema-dev.sql. It covers bothevents.created_by_user_idandevent_versions.version_created_by_user_id, and carries no foreign key (see "What the code actually looks like" below). The dev seed leaves two events on the legacy path and gives one an admin id, so step 3's dual-read has both cases to exercise. Verified by applying the pre-migration schema and then the migration to a throwaway MariaDB 11 container, and diffingSHOW CREATE TABLEagainst a fresh dev schema: identical. Applied to the running dev database on the same day; a dev container created before then needs it applied, or recreating. -
Map the accounts.Dropped 2026-09-06. There is no backfill: since the creator is only ever a display name (see below), old events keep resolving through the legacy join until step 5 and then simply lose the name. Re-inviting the people who actually still need calendar access remains an operational task, but it is no longer a migration step and nothing is blocked on it. -
Dual-read.
ChangeDone 2026-09-06.events.service.tsto prefercreated_by_user_idand fall back tocreated_by_id. Writes fill both.events.service.tsnow reads both columns and prefers the admin one, resolving the name through a singlefindDisplayNameslookup against the admin database per result set (added tousers.admin.service.tsfor this). Four copies of the same SELECT and four copies of the row mapper were collapsed into one of each first - the dual read would otherwise have had to be written four times.A name now has three possible sources, tried weakest first: the legacy join, then the
created_by_namesnapshot from migration 002, then the live admin lookup - which wins because it is the only one that follows an account being renamed. An admin id that no longer resolves falls back rather than blanking, and a failure to reach the admin database is caught and logged rather than propagated, so an anonymous read of the public calendar never depends on the admin database being up. Covered bytest/calendar/events.service.test.ts.Writes are not dual-written, contrary to the original plan: before the cutover the request only ever carries a legacy session, so there is no admin id available to write. Writes start filling
created_by_user_id(and stop fillingcreated_by_id) in step 4. -
Switch the routes. Replace the query-parameter session checks in
events.router.tsandusers.router.tswithrequireAppAccess('calendar'), and change the Angular frontend towithCredentials: trueagainst the same origin list. Deploy the API first; the calendar frontend is broken between the two deploys, so pick a quiet time. This closesDEFERRED_SECURITY.mditem 1.Two things this step has to carry that the original sequence put in step 5:
sql/calendar/003_*.sqlmust makeevents.created_by_idnullable (MODIFY created_by_id INT NULL). It isNOT NULLtoday, so the first event created after the cutover would otherwise fail to insert - there is no legacy int id to write any more.event_versions.version_created_by_idis already nullable. The foreign key can stay until step 5; it permits NULL. The same migration should re-run 002's idempotent name backfill, to catch anything created between the two deploys.- The public calendar must stay anonymous.
hasAccess('public')returns true before any credential check, and nachklang.art reads/calendar/events/public/jsonand/public/json/nextwith no session at all. Pinned bytest/calendar/credentials.service.test.tsso this cannot regress quietly.
-
Drop the legacy path. Remove
users.service.ts's session handling, thesessionstable,created_by_id, and the legacy half of the step 3 read (theusersjoin and itslegacy_*aliases - the snapshot fallback stays, it is what makes dropping the table safe). The names were archived ahead of time bysql/calendar/002_snapshot_legacy_creator_names.sql, so nothing is lost here. Legacy/calendar/users/*stays only if something still calls it - otherwise delete it too.X-Session-Id/X-Session-Keycan then come out of the CORSallowedHeaderslist insrc/app.factory.ts.
What the code actually looks like (surveyed 2026-09-06)
Four things found while doing step 1 that change how the later steps should be built:
created_by_idis display-only. Nothing authorises on it.events.router.tsgates PUT, POST, DELETE and/moveonuser?.isActivealone - there is no "only the creator may edit" rule anywhere - and the column is read back solely to rendercreated_by_nameandlast_modified_by_name. That de-risks steps 2, 3 and 5 considerably: an event whose creator never gets re-invited loses a name in the UI, it does not become uneditable or invisible. It also means the step 2 backfill is best-effort, not a precondition.- The two schemas are separate databases.
nachklang_calendarandnachklang_adminhave their own connection pools (Calendar.db.tsvs the admin module's Kysely instance). So the bridging columns get no foreign key, and - the part the original sequence missed - theLEFT OUTER JOIN usersthat produces the creator's name cannot simply be repointed. It would have to become a cross-schema join, which hardcodes the admin database name into calendar SQL and ties the two schemas together exactly as an FK would. Recommendation for step 3: drop the join for the new path and resolve names in the service layer instead - collect the distinct ids from the result set and do one lookup against the admin users service. One extra query per listing, no coupling, and it keeps working if the admin database ever moves. events.created_by_idisNOT NULL. Step 5 cannot simply stop writing it; that step has to drop the column (and its FK tousers) in the same migration that stops the writes, or make it nullable first.- Every calendar read already hits the session table.
/:calendar/jsoncallsUserService.checkSessionbefore falling back tocredentials.service.ts, so the shared credentials are the fallback, not the primary path. Step 4 replaces the first half of that withrequireAppAccess('calendar')and has to decide what happens to the second half- which is the first open question below.
Open questions to settle before starting
Settled 2026-09-06:
- The shared calendar credentials keep working, but only for iCal. The web app goes
cookie-only at step 4;
MEMBER_CREDENTIALand friends survive onGET /calendar/events/{calendar}/ical, which is the one case where the client genuinely cannot send a cookie. Everything else incredentials.service.tsgoes with step 5.publicstays anonymous everywhere - see the note under step 4. - The iCal export keeps its own scheme. Same reasoning; it is the reason the shared credentials survive at all rather than an exception to their removal.
- No account backfill. See step 2 above.
- Pre-cutover authorship is archived, not discarded.
events.created_by_nameandevent_versions.version_created_by_name, backfilled once by migration 002 and never written again. This was originally listed as a step 5 question; it was brought forward so the data is safe well before the table that holds it is dropped.
Nothing is open. The last one - , the same INT
reference on the version rows - was handled in passing: step 1 gave it a sibling bridging
column, step 2 a sibling snapshot, and step 3 reads it exactly like event_versions.version_created_by_idevents.