From 5ee5a50a05b5fc42fccd14308563c3f62aa9cbae Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 18:46:39 +0000 Subject: [PATCH 01/17] =?UTF-8?q?WIP:=20notes=20are=20about=20a=20record,?= =?UTF-8?q?=20not=20only=20an=20attendee=20=E2=80=94=20NOT=20VERIFIED,=20f?= =?UTF-8?q?ull=20suite=20not=20yet=20run?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- src/features/admin/attendee-notes.ts | 15 +- src/features/admin/attendee-page.ts | 7 +- src/features/admin/attendees-edit.ts | 20 +- src/features/admin/attendees-list.ts | 2 +- src/features/admin/listing-page-data.ts | 12 +- .../api/payment-processing/store-refund.ts | 5 +- src/shared/db/attendees/delete.ts | 12 +- .../db/migrations/2026-07-28_note_entities.ts | 37 +++ src/shared/db/migrations/registry.ts | 4 + .../db/migrations/schema/tables-questions.ts | 23 +- src/shared/db/notes/queries.ts | 173 +++++++++++++ src/shared/db/notes/sealing.ts | 58 +++++ src/shared/db/notes/target.ts | 75 ++++++ src/shared/db/notes/types.ts | 38 +++ src/shared/db/system-notes.ts | 235 ------------------ src/ui/templates/admin/attendee-notes.tsx | 14 +- src/ui/templates/admin/attendees-list.tsx | 2 +- src/ui/templates/admin/listings/types.ts | 2 +- test/integration/admin/attendee-notes.test.ts | 15 +- .../2026-07-28_note_entities.test.ts | 115 +++++++++ .../server/attendee-form-status.test.ts | 8 +- .../integration/server/attendees-list.test.ts | 8 +- .../attendees/placeholder-refresh.test.ts | 14 +- .../show-actions-and-activity.test.ts | 8 +- .../server/payments-success-refunds.test.ts | 4 +- .../server/payments/replay.test.ts | 6 +- .../webhooks/multi-ticket-refunds.test.ts | 4 +- ...signature-stored-refund-and-ignore.test.ts | 9 +- .../webhook-price-signature/helpers.ts | 4 +- test/shared/db/attendees/delete.test.ts | 12 +- .../migrations/schema/version/guard.test.ts | 3 +- .../queries.test.ts} | 103 ++++---- test/shared/db/notes/target.test.ts | 86 +++++++ test/shared/db/orphan-attendees.test.ts | 7 +- test/specs/steps/payment-capacity.ts | 7 +- test/test-utils/reservation/helpers.ts | 4 +- test/test-utils/webhooks.ts | 4 +- .../templates/admin/attendee-notes.test.tsx | 17 +- test/ui/templates/admin/attendee-page.test.ts | 5 +- .../ui/templates/admin/attendees-list.test.ts | 5 +- 40 files changed, 794 insertions(+), 388 deletions(-) create mode 100644 src/shared/db/migrations/2026-07-28_note_entities.ts create mode 100644 src/shared/db/notes/queries.ts create mode 100644 src/shared/db/notes/sealing.ts create mode 100644 src/shared/db/notes/target.ts create mode 100644 src/shared/db/notes/types.ts delete mode 100644 src/shared/db/system-notes.ts create mode 100644 test/integration/db/migrations/2026-07-28_note_entities.test.ts rename test/shared/db/{system-notes.test.ts => notes/queries.test.ts} (67%) create mode 100644 test/shared/db/notes/target.test.ts diff --git a/src/features/admin/attendee-notes.ts b/src/features/admin/attendee-notes.ts index 3e9b1fbcde..28cf0ddb21 100644 --- a/src/features/admin/attendee-notes.ts +++ b/src/features/admin/attendee-notes.ts @@ -26,10 +26,11 @@ import { getSearchParam } from "#routes/url.ts"; import { getAttendeeOrNull } from "#shared/db/attendees/queries.ts"; import { createOwnerNote, - deleteAttendeeNote, - getAttendeeNote, - type SystemNote, -} from "#shared/db/system-notes.ts"; + deleteNote, + getNote, +} from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import type { ResponseHandler } from "#shared/response-steps.ts"; import { requireRequestPrivateKey } from "#shared/session-private-key.ts"; import type { AdminSession, Attendee } from "#shared/types.ts"; @@ -73,7 +74,7 @@ const loadNote = async ({ attendeeId, noteId, }: NoteRouteParams): Promise => - getAttendeeNote(attendeeId, noteId, await requireRequestPrivateKey()); + getNote(attendeeNotes(attendeeId), noteId, await requireRequestPrivateKey()); const noteEntityHandler = createEntityHandler( loadNote, ); @@ -134,7 +135,7 @@ const handleAddNotePost: TypedRouteHandler<"POST /admin/attendee/:attendeeId/not 400, ); } - await createOwnerNote(attendeeId, note); + await createOwnerNote(attendeeNotes(attendeeId), note); return redirect( returnTarget(attendeeId, returnUrl), t("notes.added"), @@ -162,7 +163,7 @@ const handleDeleteNoteGet: TypedRouteHandler<"GET /admin/attendee/:attendeeId/no /** POST /admin/attendee/:attendeeId/note/:noteId/delete — delete the note. */ const handleDeleteNotePost: TypedRouteHandler<"POST /admin/attendee/:attendeeId/note/:noteId/delete"> = noteHandlers.post(async (note, _session, form, _request, { attendeeId }) => { - await deleteAttendeeNote(attendeeId, note.id); + await deleteNote(attendeeNotes(attendeeId), note.id); return redirect( returnTarget(attendeeId, form.getString("return_url")), t("notes.deleted"), diff --git a/src/features/admin/attendee-page.ts b/src/features/admin/attendee-page.ts index 13c5b4cfd5..385fbb6fc0 100644 --- a/src/features/admin/attendee-page.ts +++ b/src/features/admin/attendee-page.ts @@ -45,8 +45,9 @@ import { loadPreviousBookings } from "#routes/admin/previous-bookings.ts"; import { requireSessionOr } from "#routes/auth.ts"; import { getEffectiveDomain } from "#shared/config.ts"; import { attendeeStatuses } from "#shared/db/attendee-statuses.ts"; +import { getNotesFor } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { settings } from "#shared/db/settings.ts"; -import { getNotesForAttendee } from "#shared/db/system-notes.ts"; import { isReadOnly } from "#shared/env.ts"; import { requireRequestPrivateKey } from "#shared/session-private-key.ts"; import { isOwnerRole } from "#shared/types.ts"; @@ -201,8 +202,8 @@ export const attendeePage: EntityPage = defineEntityPage({ attendeeBanner({ attendee, isOwner: ctx.session.adminLevel === "owner", - notes: await getNotesForAttendee( - attendee.id, + notes: await getNotesFor( + attendeeNotes(attendee.id), await requireRequestPrivateKey(), ), statuses: await attendeeStatuses.getAll(), diff --git a/src/features/admin/attendees-edit.ts b/src/features/admin/attendees-edit.ts index 6f36eb8936..01b4dc7e09 100644 --- a/src/features/admin/attendees-edit.ts +++ b/src/features/admin/attendees-edit.ts @@ -22,16 +22,17 @@ import { logActivity } from "#shared/db/activityLog.ts"; import { decryptAttendeeOrNull } from "#shared/db/attendees/pii.ts"; import { getAttendeeRaw } from "#shared/db/attendees/queries.ts"; import { queryOne } from "#shared/db/client.ts"; +import { + createSystemNote, + deleteNote, + getNotesFor, +} from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { getRefundPaymentReferencesForAttendee, markPaymentReferencesProviderRefunded, type RefundPaymentReference, } from "#shared/db/payment-references.ts"; -import { - createSystemNote, - deleteAttendeeNote, - getNotesForAttendee, -} from "#shared/db/system-notes.ts"; import type { FormParams } from "#shared/form-data.ts"; import { legMatches } from "#shared/ledger/legs.ts"; import type { PaymentProvider } from "#shared/payments.ts"; @@ -151,7 +152,10 @@ const recordConfirmedRefund = async ( // stale note could still be there if the previous cleanup failed). await cleanupStaleManualRefundNote(attendeeId, privateKey); if (isPlaceholder) { - await createSystemNote(attendeeId, t("note.placeholder_refund_confirmed")); + await createSystemNote( + attendeeNotes(attendeeId), + t("note.placeholder_refund_confirmed"), + ); } return null; }; @@ -164,10 +168,10 @@ const cleanupStaleManualRefundNote = async ( attendeeId: number, privateKey: CryptoKey, ): Promise => { - const notes = await getNotesForAttendee(attendeeId, privateKey); + const notes = await getNotesFor(attendeeNotes(attendeeId), privateKey); for (const note of notes) { if (note.type === "system" && note.note.includes("could NOT be refunded")) { - await deleteAttendeeNote(attendeeId, note.id); + await deleteNote(attendeeNotes(attendeeId), note.id); } } }; diff --git a/src/features/admin/attendees-list.ts b/src/features/admin/attendees-list.ts index 4ee01433eb..b76b1ca782 100644 --- a/src/features/admin/attendees-list.ts +++ b/src/features/admin/attendees-list.ts @@ -27,8 +27,8 @@ import { } from "#shared/db/attendees/queries.ts"; import { getActiveHolidays } from "#shared/db/holidays.ts"; import { getAllListings } from "#shared/db/listings/records.ts"; +import { loadNotesForAttendees } from "#shared/db/notes/queries.ts"; import { settings } from "#shared/db/settings.ts"; -import { loadNotesForAttendees } from "#shared/db/system-notes.ts"; import { type ListingFilter, listingCategory, diff --git a/src/features/admin/listing-page-data.ts b/src/features/admin/listing-page-data.ts index aa6aec345d..a8717d3e50 100644 --- a/src/features/admin/listing-page-data.ts +++ b/src/features/admin/listing-page-data.ts @@ -33,16 +33,16 @@ import { } from "#shared/db/listing-parents.ts"; import { getListingAggregateRecalculation } from "#shared/db/listings/aggregates.ts"; import { getAttendeesByListingIds } from "#shared/db/listings/attendees.ts"; +import { + loadNotesForAttendees, + loadNotesForListing, +} from "#shared/db/notes/queries.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import { getAttendeeIdsWithPaymentReference } from "#shared/db/payment-references.ts"; import { deleteAllStaleReservations } from "#shared/db/processed-payments.ts"; import { getListingChoiceAnswerMap } from "#shared/db/questions/attendee-answers/reads.ts"; import { getQuestionsForListing } from "#shared/db/questions/queries.ts"; import { settings } from "#shared/db/settings.ts"; -import { - loadNotesForAttendees, - loadNotesForListing, - type SystemNote, -} from "#shared/db/system-notes.ts"; import { listingLedgerHref } from "#shared/ledger-links.ts"; import { requireRequestPrivateKey } from "#shared/session-private-key.ts"; import { @@ -189,7 +189,7 @@ const noteAuthorNames = async ( notes.length === 0 ? new Map() : getAttendeeNamesByIds( - unique(notes.map((note) => note.attendee_id)), + unique(notes.map((note) => note.entity_id)), await requireRequestPrivateKey(), ); diff --git a/src/features/api/payment-processing/store-refund.ts b/src/features/api/payment-processing/store-refund.ts index 7667a69f01..02fcdc95ca 100644 --- a/src/features/api/payment-processing/store-refund.ts +++ b/src/features/api/payment-processing/store-refund.ts @@ -32,8 +32,9 @@ import { bookingDateFields } from "#shared/booking-date-fields.ts"; import { logActivity } from "#shared/db/activityLog.ts"; import { attendeesApi } from "#shared/db/attendees/api.ts"; import { settleAttendeeBalance } from "#shared/db/attendees/balance.ts"; +import { createSystemNote } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { balanceFinalizeStatements } from "#shared/db/payment-finalize.ts"; -import { createSystemNote } from "#shared/db/system-notes.ts"; import { ErrorCode, logError } from "#shared/logger.ts"; import { sendNtfyError } from "#shared/ntfy.ts"; import type { BookingItem, ValidatedPaymentSession } from "#shared/payments.ts"; @@ -195,7 +196,7 @@ export const storeRefundedBooking = async ( }); } await createSystemNote( - attendeeId, + attendeeNotes(attendeeId), refundedNoteText(attendeeId, spec, refunded, session.paymentReference), ); // Status 200: a fully-handled terminal outcome (booking kept, money returned or diff --git a/src/shared/db/attendees/delete.ts b/src/shared/db/attendees/delete.ts index 125b8151e4..a7658e44e3 100644 --- a/src/shared/db/attendees/delete.ts +++ b/src/shared/db/attendees/delete.ts @@ -9,6 +9,7 @@ import { type SqlStatement, } from "#shared/db/client.ts"; import { ticketCountSumExpr } from "#shared/db/migrations/schema/listing-aggregates.ts"; +import { noteDeleteStatement } from "#shared/db/notes/queries.ts"; type DeleteAttendeeOptions = { releaseBookings?: boolean }; type ListingContribution = { @@ -63,7 +64,6 @@ const DEPENDENT_ROW_TARGETS = [ { field: "attendee_id", table: "processed_payments" }, { field: "attendee_id", table: "attendee_answers" }, { field: "attendee_id", table: "listing_attendees" }, - { field: "attendee_id", table: "system_notes" }, { field: "servicing_attendee_id", table: "service_costs" }, ] as const; @@ -83,10 +83,14 @@ export const checkoutStageDeleteStatement = ( /** Build the common dependent-row deletes for one or many attendee ids. */ export const attendeeDependentDeleteStatements = ( attendeeIds: SqlStatement, -): SqlStatement[] => - DEPENDENT_ROW_TARGETS.map((target) => +): SqlStatement[] => [ + ...DEPENDENT_ROW_TARGETS.map((target) => dependentDeleteStatement(target, attendeeIds), - ); + ), + // Notes are named by the kind of record they are about, so the notes module + // builds this one rather than the plain "column = id" shape above. + noteDeleteStatement("attendee", attendeeIds), +]; /** Delete an attendee and all dependent data tied to the attendee record. */ const purgeAttendee = ( diff --git a/src/shared/db/migrations/2026-07-28_note_entities.ts b/src/shared/db/migrations/2026-07-28_note_entities.ts new file mode 100644 index 0000000000..70dee45bdb --- /dev/null +++ b/src/shared/db/migrations/2026-07-28_note_entities.ts @@ -0,0 +1,37 @@ +import { bareSchemaMigration } from "./define.ts"; + +/** + * Notes used to belong to an attendee and nothing else. They now name the kind + * of record they are about and which one, so any record can carry notes. + * + * The two new columns are added with a default so the database accepts them on + * a table that already has rows; the rebuild at the end drops `attendee_id` and + * takes the columns' final form from SCHEMA, which has no default. Each step is + * skipped when it has already been done, so a re-run after a failed verify is a + * cheap no-op. + */ +export default bareSchemaMigration( + "2026-07-28_note_entities", + "Replace system_notes.attendee_id with entity_type + entity_id, so notes can be about any record and not only an attendee. Existing notes become notes about an attendee", + async ({ getDb, recreateTable, syncIndexes }) => { + const info = await getDb().execute("PRAGMA table_info(system_notes)"); + const columns = new Set(info.rows.map((row) => String(row.name))); + if (!columns.has("attendee_id")) return; + + if (!columns.has("entity_type")) { + await getDb().execute( + "ALTER TABLE system_notes ADD COLUMN entity_type TEXT NOT NULL DEFAULT 'attendee'", + ); + } + if (!columns.has("entity_id")) { + await getDb().execute( + "ALTER TABLE system_notes ADD COLUMN entity_id INTEGER NOT NULL DEFAULT 0", + ); + } + await getDb().execute( + "UPDATE system_notes SET entity_type = 'attendee', entity_id = attendee_id", + ); + await recreateTable("system_notes"); // rebuild from SCHEMA, dropping attendee_id + await syncIndexes(); // idx_system_notes_entity in place of the attendee one + }, +); diff --git a/src/shared/db/migrations/registry.ts b/src/shared/db/migrations/registry.ts index 559e1b4b88..70a78de9af 100644 --- a/src/shared/db/migrations/registry.ts +++ b/src/shared/db/migrations/registry.ts @@ -382,6 +382,10 @@ export const MIGRATION_REGISTRY: MigrationRegistryEntry[] = [ "2026-07-22_maintenance_completion", () => import("./2026-07-22_maintenance_completion.ts"), ), + entry( + "2026-07-28_note_entities", + () => import("./2026-07-28_note_entities.ts"), + ), ]; /* jscpd:ignore-end */ diff --git a/src/shared/db/migrations/schema/tables-questions.ts b/src/shared/db/migrations/schema/tables-questions.ts index 92929bdb3c..b5a8e91c1b 100644 --- a/src/shared/db/migrations/schema/tables-questions.ts +++ b/src/shared/db/migrations/schema/tables-questions.ts @@ -1,8 +1,14 @@ /** Form tables: questions, answers, attributes, built sites, notes. */ +import { NOTE_ENTITIES } from "#shared/db/notes/target.ts"; import { createdColumn, sortOrderColumn } from "./scalar-columns.ts"; import type { Table } from "./types.ts"; +/** The record kinds a note may be about, as a CHECK the database enforces. */ +const noteEntityCheck = `entity_type IN (${NOTE_ENTITIES.map( + (entity) => `'${entity}'`, +).join(", ")})`; + export const questionTables: [name: string, table: Table][] = [ [ "questions", @@ -211,18 +217,22 @@ export const questionTables: [name: string, table: Table][] = [ ], [ - // Per-attendee notes the operator sees on the attendee record. `note` is - // always stored encrypted — a `system` note (auto-generated, e.g. the + // Notes the operator sees on a record, named by which kind of record they + // are about (`entity_type`) and which one (`entity_id`). `note` is always + // stored encrypted — a `system` note (auto-generated, e.g. the // refunded-but-stored booking warning) with the symmetric DB_ENCRYPTION_KEY // so a key-less system path can both write and read it back, an `owner` note // (operator-authored) with the owner public key so only the owner can read - // it. System notes are kept PII-free by convention. No FKs — the attendee + // it. System notes are kept PII-free by convention. No FKs — each record's // delete path prunes these rows explicitly. "system_notes", { columns: [ ["id", "INTEGER PRIMARY KEY AUTOINCREMENT"], - ["attendee_id", "INTEGER NOT NULL"], + // The kinds come from NOTE_ENTITIES, so the stored values and the ones + // the code accepts cannot drift apart. + ["entity_type", `TEXT NOT NULL CHECK (${noteEntityCheck})`], + ["entity_id", "INTEGER NOT NULL"], [ "type", "TEXT NOT NULL DEFAULT 'system' CHECK (type IN ('system', 'owner'))", @@ -231,7 +241,10 @@ export const questionTables: [name: string, table: Table][] = [ createdColumn, ], indexes: [ - { columns: ["attendee_id"], name: "idx_system_notes_attendee_id" }, + { + columns: ["entity_type", "entity_id"], + name: "idx_system_notes_entity", + }, ], }, ], diff --git a/src/shared/db/notes/queries.ts b/src/shared/db/notes/queries.ts new file mode 100644 index 0000000000..088da3a3bd --- /dev/null +++ b/src/shared/db/notes/queries.ts @@ -0,0 +1,173 @@ +/** + * Reading and writing notes (the `system_notes` table). + * + * Every read and write names the record the notes are about, so one shape + * serves every kind of record: see ./target.ts. + */ + +import { ATTENDEE_KIND } from "#shared/db/attendees/kind.ts"; +import { + execute, + insert, + queryAll, + type SqlStatement, +} from "#shared/db/client.ts"; +import { nowIso } from "#shared/now.ts"; +import { openNotes, sealNote } from "./sealing.ts"; +import { + type NoteEntity, + type NoteTarget, + targetsSelectedBy, + targetsWhere, + targetWhere, +} from "./target.ts"; +import type { SystemNote, SystemNoteRow, SystemNoteType } from "./types.ts"; + +const NOTE_COLUMNS = "id, entity_type, entity_id, type, note, created"; + +/** Build a "record a note" function for one kind of note: it seals the text the + * way that kind is sealed, then stores it against the record it is about. */ +const noteWriterOf = + (type: SystemNoteType) => + async (target: NoteTarget, note: string): Promise => { + const { sql, args } = insert("system_notes", { + created: nowIso(), + entity_id: target.id, + entity_type: target.entity, + note: await sealNote(type, note), + type, + }); + await execute(sql, args); + }; + +/** + * Record a note the app wrote itself. The text MUST be free of personal + * details: it is sealed with the symmetric DB key (readable from a database + * dump plus that key), and exists so a path with no owner session can write it. + */ +export const createSystemNote = noteWriterOf("system"); + +/** Record an operator's note, sealed with the owner public key. */ +export const createOwnerNote = noteWriterOf("owner"); + +/** The still-sealed rows matching a WHERE body, oldest first per record. Shared + * by every read so the column list and the ordering live in one place. */ +const noteRowsWhere = ({ args, sql }: SqlStatement): Promise => + queryAll( + `SELECT ${NOTE_COLUMNS} FROM system_notes WHERE ${sql} ORDER BY entity_id, id`, + args, + ); + +/** + * The still-sealed rows for several records of one kind, oldest first. Cheap + * (no key material): a caller can use it to decide whether any notes exist at + * all before working out the owner private key to open them. + */ +export const getNoteRows = ( + entity: NoteEntity, + ids: number[], +): Promise => + ids.length === 0 + ? Promise.resolve([]) + : noteRowsWhere(targetsWhere(entity, ids)); + +/** + * The still-sealed rows for every real (`kind = 'attendee'`) attendee booked + * onto one listing, oldest first — chosen in SQL rather than by passing a list + * of attendee ids, so the Overview never loads (nor binds) a row per attendee + * just to find the handful with notes. + */ +export const getNoteRowsForListing = ( + listingId: number, +): Promise => + noteRowsWhere( + targetsSelectedBy("attendee", { + args: [listingId], + sql: `SELECT listingAttendee.attendee_id + FROM listing_attendees AS listingAttendee + JOIN attendees AS attendee ON attendee.id = listingAttendee.attendee_id + WHERE listingAttendee.listing_id = ? AND attendee.kind = '${ATTENDEE_KIND}'`, + }), + ); + +/** All of one record's notes, opened up, oldest first. */ +export const getNotesFor = async ( + target: NoteTarget, + privateKey: CryptoKey, +): Promise => + openNotes(await noteRowsWhere(targetWhere(target)), privateKey); + +/** A "load the notes for " reader: takes the selector value and a way + * to get the owner private key, and answers with opened-up notes. */ +type NotesLoader = ( + selector: A, + getPrivateKey: () => Promise, +) => Promise; + +/** + * Build a loader from a row-fetcher: fetch the still-sealed rows, then open + * them, working out the owner private key only when at least one note exists — + * so a note-free view never pays for a key unwrap. + */ +const notesLoaderVia = + (fetchRows: (selector: A) => Promise): NotesLoader => + async (selector, getPrivateKey) => { + const rows = await fetchRows(selector); + return rows.length === 0 ? [] : openNotes(rows, await getPrivateKey()); + }; + +/** + * The notes of several attendees, oldest first, for an attendee-list view. + * Answers with [] when none of them have notes. + */ +export const loadNotesForAttendees: NotesLoader = notesLoaderVia( + (attendeeIds) => getNoteRows("attendee", attendeeIds), +); + +/** + * The notes of every real attendee on one listing, oldest first — chosen in SQL + * (see {@link getNoteRowsForListing}). Answers with [] when none have notes, so + * a note-free listing's Overview skips the key unwrap. + */ +export const loadNotesForListing: NotesLoader = notesLoaderVia( + getNoteRowsForListing, +); + +/** One record's note, opened up, or null when it isn't that record's. */ +export const getNote = async ( + target: NoteTarget, + noteId: number, + privateKey: CryptoKey, +): Promise => { + const { args, sql } = targetWhere(target); + const rows = await queryAll( + `SELECT ${NOTE_COLUMNS} FROM system_notes WHERE id = ? AND ${sql}`, + [noteId, ...args], + ); + const row = rows[0]; + if (!row) return null; + const [note] = await openNotes([row], privateKey); + return note ?? null; +}; + +/** Delete one note, tied to its record so a stray id can't reach another's. */ +export const deleteNote = ( + target: NoteTarget, + noteId: number, +): Promise => { + const { args, sql } = targetWhere(target); + return execute(`DELETE FROM system_notes WHERE id = ? AND ${sql}`, [ + noteId, + ...args, + ]); +}; + +/** Delete the notes of records chosen by a subquery — for a delete path that + * clears a record's dependent rows in one batch. */ +export const noteDeleteStatement = ( + entity: NoteEntity, + idsQuery: SqlStatement, +): SqlStatement => { + const { args, sql } = targetsSelectedBy(entity, idsQuery); + return { args, sql: `DELETE FROM system_notes WHERE ${sql}` }; +}; diff --git a/src/shared/db/notes/sealing.ts b/src/shared/db/notes/sealing.ts new file mode 100644 index 0000000000..40fe166549 --- /dev/null +++ b/src/shared/db/notes/sealing.ts @@ -0,0 +1,58 @@ +/** + * Sealing a note's text, and opening it back up. + * + * The two note kinds seal differently (see ./types.ts), so each way of sealing + * is named once here and everything else works in terms of a sealed note. + */ + +import { decrypt, encrypt } from "#shared/crypto/encryption.ts"; +import { + decryptWithOwnerKey, + encryptWithOwnerKey, +} from "#shared/crypto/keys.ts"; +import type { + EnvKeyEncrypted, + OwnerKeyEncrypted, +} from "#shared/crypto/sealed.ts"; +import { settings } from "#shared/db/settings.ts"; +import type { SystemNote, SystemNoteRow, SystemNoteType } from "./types.ts"; + +export type SealedNote = OwnerKeyEncrypted | EnvKeyEncrypted; + +/** How each kind of note is sealed. A new kind is a new entry here, and the + * compiler asks for it everywhere the kinds are handled. */ +const SEAL_AS: Record Promise> = { + owner: (note) => encryptWithOwnerKey(note, settings.publicKey), + system: (note) => encrypt(note), +}; + +export const sealNote = ( + type: SystemNoteType, + note: string, +): Promise => SEAL_AS[type](note); + +/** + * Open one note's text. We do not guard this: a failure here can only mean the + * data key itself is broken — in which case the record's own personal details + * would not open either and the page never renders — so a placeholder would + * hide a whole-system failure behind a branch nothing can reach. Let it throw. + */ +const openNote = ( + row: SystemNoteRow, + privateKey: CryptoKey, +): Promise => + row.type === "owner" + ? decryptWithOwnerKey(row.note, privateKey) + : decrypt(row.note); + +/** Open a batch of note rows, keeping their order. */ +export const openNotes = ( + rows: SystemNoteRow[], + privateKey: CryptoKey, +): Promise => + Promise.all( + rows.map(async (row) => ({ + ...row, + note: await openNote(row, privateKey), + })), + ); diff --git a/src/shared/db/notes/target.ts b/src/shared/db/notes/target.ts new file mode 100644 index 0000000000..ac98a38b25 --- /dev/null +++ b/src/shared/db/notes/target.ts @@ -0,0 +1,75 @@ +/** + * What a note is about. + * + * A note used to belong to an attendee and nothing else. It now names the kind + * of thing it is about and which one, so any record the operator can open can + * carry notes. This module is pure: it says what a target is and how to ask for + * one in SQL, and never touches the database itself. + */ + +import * as v from "valibot"; +import type { SqlStatement } from "#shared/db/client.ts"; +import { inPlaceholders } from "#shared/db/client.ts"; + +/** The kinds of record a note can be about. Adding one here is what lets a + * page carry notes; the stored values are checked against this list. */ +export const NoteEntitySchema = v.picklist(["attendee"]); + +export type NoteEntity = v.InferOutput; + +export const NOTE_ENTITIES = NoteEntitySchema.options; + +export const isNoteEntity = (value: string): value is NoteEntity => + v.is(NoteEntitySchema, value); + +/** One record a note is about: its kind, and which one of that kind. */ +export interface NoteTarget { + entity: NoteEntity; + id: number; +} + +/** Build "a note about this kind of thing" once, then name records with it. */ +export const notesAbout = + (entity: NoteEntity) => + (id: number): NoteTarget => ({ entity, id }); + +/** The record kind notes started with, and still the only one that has them. */ +export const attendeeNotes = notesAbout("attendee"); + +/** Do these two targets name the same record? */ +export const isSameTarget = (left: NoteTarget, right: NoteTarget): boolean => + left.entity === right.entity && left.id === right.id; + +/** Group notes by which record they are about — for a batch about one kind of + * record, where the id alone tells them apart. Input order is kept per group. */ +export const groupNotesByTargetId = ( + notes: Note[], +): Map => Map.groupBy(notes, (note) => note.entity_id); + +/** Ask for one record's notes. */ +export const targetWhere = ({ entity, id }: NoteTarget): SqlStatement => ({ + args: [entity, id], + sql: "entity_type = ? AND entity_id = ?", +}); + +/** Ask for several records of one kind at once. An empty list asks for nothing, + * which is what `0 = 1` says: no row can answer it. */ +export const targetsWhere = ( + entity: NoteEntity, + ids: number[], +): SqlStatement => + ids.length === 0 + ? { args: [], sql: "0 = 1" } + : { + args: [entity, ...ids], + sql: `entity_type = ? AND entity_id IN (${inPlaceholders(ids)})`, + }; + +/** Ask for the notes of one kind of record, chosen by a subquery of ids. */ +export const targetsSelectedBy = ( + entity: NoteEntity, + idsQuery: SqlStatement, +): SqlStatement => ({ + args: [entity, ...idsQuery.args], + sql: `entity_type = ? AND entity_id IN (${idsQuery.sql})`, +}); diff --git a/src/shared/db/notes/types.ts b/src/shared/db/notes/types.ts new file mode 100644 index 0000000000..c53ceb4cb0 --- /dev/null +++ b/src/shared/db/notes/types.ts @@ -0,0 +1,38 @@ +/** + * What a note is, stored and unstored. + * + * Two note kinds share one table and differ only in how the text is sealed: + * - `system` — written by the app itself (e.g. the refunded-but-stored booking + * warning). Sealed with the symmetric DB_ENCRYPTION_KEY so a path with no + * logged-in owner can both write it and read it back. Kept free of personal + * details by convention. + * - `owner` — written by an operator. Sealed with the owner public key, so it + * can be written without a session but read only with the owner private key. + */ + +import type { + EnvKeyEncrypted, + OwnerKeyEncrypted, +} from "#shared/crypto/sealed.ts"; +import type { NoteEntity } from "./target.ts"; + +export type SystemNoteType = "system" | "owner"; + +/** A stored note with its `note` field opened up to plain text. */ +export interface SystemNote { + created: string; + entity_id: number; + entity_type: NoteEntity; + id: number; + note: string; + type: SystemNoteType; +} + +/** The row as stored, its `note` still sealed. Which sealing was used follows + * the note's kind, so a row is a choice between the two rather than a shape + * where either ciphertext might turn up under either kind. */ +export type SystemNoteRow = Omit & + ( + | { type: "owner"; note: OwnerKeyEncrypted } + | { type: "system"; note: EnvKeyEncrypted } + ); diff --git a/src/shared/db/system-notes.ts b/src/shared/db/system-notes.ts deleted file mode 100644 index 459a0705f2..0000000000 --- a/src/shared/db/system-notes.ts +++ /dev/null @@ -1,235 +0,0 @@ -/** - * Per-attendee operator-visible notes (the `system_notes` table). - * - * Two note types share one table: - * - `system` — auto-generated by the app (e.g. the refunded-but-stored booking - * warning). Encrypted with the symmetric DB_ENCRYPTION_KEY so a key-less - * system path (a webhook with no logged-in owner) can both write and read it - * back. Kept PII-free by convention. - * - `owner` — operator-authored. Encrypted with the owner public key, so it can - * be written without a session but read only with the owner private key. - */ - -import type { InValue } from "@libsql/client"; -/* jscpd:ignore-start */ -import { decrypt, encrypt } from "#shared/crypto/encryption.ts"; -import { - decryptWithOwnerKey, - encryptWithOwnerKey, -} from "#shared/crypto/keys.ts"; -import type { - EnvKeyEncrypted, - OwnerKeyEncrypted, -} from "#shared/crypto/sealed.ts"; -import { ATTENDEE_KIND } from "#shared/db/attendees/kind.ts"; -import { - execute, - inPlaceholders, - insert, - queryAll, -} from "#shared/db/client.ts"; -import { settings } from "#shared/db/settings.ts"; -import { nowIso } from "#shared/now.ts"; -/* jscpd:ignore-end */ - -export type SystemNoteType = "system" | "owner"; - -/** A stored note with its `note` field decrypted to plaintext. */ -export type SystemNote = { - id: number; - attendee_id: number; - type: SystemNoteType; - note: string; - created: string; -}; - -/** The raw row as stored — `note` still encrypted. The note's sealing scheme - * follows its type: owner notes are owner-key ciphertext, system notes env-key - * ciphertext, so the row is a discriminated union on `type`. */ -type SystemNoteRow = Omit & - ( - | { type: "owner"; note: OwnerKeyEncrypted } - | { type: "system"; note: EnvKeyEncrypted } - ); - -const NOTE_COLUMNS = "id, attendee_id, type, note, created"; - -/** Insert one already-encrypted note of the given type. */ -const insertNote = async ( - attendeeId: number, - type: SystemNoteType, - encryptedNote: OwnerKeyEncrypted | EnvKeyEncrypted, -): Promise => { - const { sql, args } = insert("system_notes", { - attendee_id: attendeeId, - created: nowIso(), - note: encryptedNote, - type, - }); - await execute(sql, args); -}; - -/** Build a "record a note" function: it seals the text its own way, then stores - * the result under the given note type. The two note kinds differ only in how - * they seal, so they share this one body. */ -const noteCreatorVia = - ( - type: SystemNoteType, - seal: (note: string) => Promise, - ) => - async (attendeeId: number, note: string): Promise => - insertNote(attendeeId, type, await seal(note)); - -/** - * Record an auto-generated system note. The text MUST be PII-free: it is - * encrypted with the symmetric DB key (recoverable from a DB dump + that key), - * and exists so a system path with no owner session can write it. - */ -export const createSystemNote = noteCreatorVia("system", (note) => - encrypt(note), -); - -/** Record an operator-authored note, encrypted with the owner public key. */ -export const createOwnerNote = noteCreatorVia("owner", (note) => - encryptWithOwnerKey(note, settings.publicKey), -); - -/** Decrypt one note by its type. We do not guard this: a failure here can only - * mean the data key itself is broken — in which case the attendee's own PII - * wouldn't decrypt and the page never renders at all — so masking it with a - * placeholder would only hide a system-wide failure behind an untestable - * branch. Let it throw. */ -const decryptNote = ( - row: SystemNoteRow, - privateKey: CryptoKey, -): Promise => - row.type === "owner" - ? decryptWithOwnerKey(row.note, privateKey) - : decrypt(row.note); - -/** Decrypt a batch of note rows, preserving order. */ -export const decryptNotes = ( - rows: SystemNoteRow[], - privateKey: CryptoKey, -): Promise => - Promise.all( - rows.map(async (row) => ({ - ...row, - note: await decryptNote(row, privateKey), - })), - ); - -/** Fetch the still-encrypted note rows matching a WHERE body (no leading - * `WHERE`), oldest first per attendee. Shared by the by-ids and by-listing - * reads so the column list and ordering live in one place. */ -const noteRowsWhere = ( - where: string, - args: InValue[], -): Promise => - queryAll( - `SELECT ${NOTE_COLUMNS} FROM system_notes WHERE ${where} ORDER BY attendee_id, id`, - args, - ); - -/** - * The still-encrypted note rows for a set of attendees, oldest first. Cheap (no - * key material): a caller can use it to decide whether any notes exist before - * deriving the owner private key to decrypt them. - */ -export const getNoteRows = (attendeeIds: number[]): Promise => - attendeeIds.length === 0 - ? Promise.resolve([]) - : noteRowsWhere( - `attendee_id IN (${inPlaceholders(attendeeIds)})`, - attendeeIds, - ); - -/** - * The still-encrypted note rows for every real (`kind = 'attendee'`) attendee - * booked onto one listing, oldest first — scoped in SQL rather than by passing - * a per-attendee id list, so the Overview never materialises (nor binds) a row - * per attendee just to find the handful with notes. - */ -export const getNoteRowsForListing = ( - listingId: number, -): Promise => - noteRowsWhere( - `attendee_id IN (SELECT listingAttendee.attendee_id - FROM listing_attendees AS listingAttendee - JOIN attendees AS attendee ON attendee.id = listingAttendee.attendee_id - WHERE listingAttendee.listing_id = ? AND attendee.kind = '${ATTENDEE_KIND}')`, - [listingId], - ); - -/** All decrypted notes for one attendee, oldest first. */ -export const getNotesForAttendee = async ( - attendeeId: number, - privateKey: CryptoKey, -): Promise => - decryptNotes(await getNoteRows([attendeeId]), privateKey); - -/** - * A "load decrypted notes for " reader: fetch the still-encrypted rows - * for a selector value, then decrypt them, deriving the owner private key lazily - * — only when at least one note exists — so a note-free view never forces a key - * unwrap. Curried over the row-fetcher so the attendee-list and listing-scoped - * loaders share one body. - */ -/** A "load decrypted notes for " reader: takes the selector value and - * a lazy owner-private-key getter, and returns the decrypted notes. */ -type NotesLoader = ( - selector: A, - getPrivateKey: () => Promise, -) => Promise; - -const notesLoaderVia = - (fetchRows: (selector: A) => Promise): NotesLoader => - async (selector, getPrivateKey) => { - const rows = await fetchRows(selector); - return rows.length === 0 ? [] : decryptNotes(rows, await getPrivateKey()); - }; - -/** - * Decrypted notes for a set of attendees, oldest first, for an attendee-list - * view. Returns [] when none have notes. - */ -export const loadNotesForAttendees: NotesLoader = - notesLoaderVia(getNoteRows); - -/** - * Decrypted notes for every real attendee on one listing, oldest first — - * scoped in SQL (see {@link getNoteRowsForListing}). Returns [] when none have - * notes, so the Overview's key unwrap is skipped for a note-free listing. - */ -export const loadNotesForListing: NotesLoader = notesLoaderVia( - getNoteRowsForListing, -); - -/** Group decrypted notes by attendee id (input order preserved within a group). */ -export const groupNotesByAttendee = ( - notes: SystemNote[], -): Map => Map.groupBy(notes, (note) => note.attendee_id); - -/** Load one decrypted note scoped to its attendee, or null when it doesn't exist. */ -export const getAttendeeNote = async ( - attendeeId: number, - noteId: number, - privateKey: CryptoKey, -): Promise => { - const rows = await queryAll( - `SELECT ${NOTE_COLUMNS} FROM system_notes WHERE id = ? AND attendee_id = ?`, - [noteId, attendeeId], - ); - if (rows.length === 0) return null; - return { ...rows[0]!, note: await decryptNote(rows[0]!, privateKey) }; -}; - -/** Delete one note, scoped to its attendee so a stray id can't reach another's. */ -export const deleteAttendeeNote = ( - attendeeId: number, - noteId: number, -): Promise => - execute("DELETE FROM system_notes WHERE id = ? AND attendee_id = ?", [ - noteId, - attendeeId, - ]); diff --git a/src/ui/templates/admin/attendee-notes.tsx b/src/ui/templates/admin/attendee-notes.tsx index 22e965b60f..7688b811c9 100644 --- a/src/ui/templates/admin/attendee-notes.tsx +++ b/src/ui/templates/admin/attendee-notes.tsx @@ -13,10 +13,8 @@ /* jscpd:ignore-start */ import { t } from "#i18n"; import { formatDatetimeShort } from "#shared/dates.ts"; -import { - groupNotesByAttendee, - type SystemNote, -} from "#shared/db/system-notes.ts"; +import { groupNotesByTargetId } from "#shared/db/notes/target.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import { CsrfForm } from "#shared/forms/csrf-form.tsx"; import { Flash } from "#shared/forms/flash.tsx"; import { hiddenInputs } from "#shared/forms/hidden-inputs.tsx"; @@ -47,7 +45,7 @@ const attendeeUrl = (attendeeId: number): string => /** The are-you-sure delete page for one note, returning to `returnUrl`. */ const deleteNoteUrl = (note: SystemNote, returnUrl: string): string => - `/admin/attendee/${note.attendee_id}/note/${note.id}/delete?return_url=${encodeURIComponent( + `/admin/attendee/${note.entity_id}/note/${note.id}/delete?return_url=${encodeURIComponent( returnUrl, )}`; @@ -116,7 +114,7 @@ const NoteBox = ({ note, isOwner }: NoteViewProps): JSX.Element => { × @@ -180,7 +178,7 @@ export const AttendeeNotesSummary = ({ isOwner, }: NotesViewProps & { names: Map }): JSX.Element | null => { if (notes.length === 0) return null; - const grouped = groupNotesByAttendee(notes); + const grouped = groupNotesByTargetId(notes); return (
{t("notes.summary", { count: grouped.size })} @@ -254,7 +252,7 @@ export const adminDeleteNotePage = ({ error, }: { note: SystemNote } & NotePageProps): string => ConfirmPage({ - action: `/admin/attendee/${note.attendee_id}/note/${note.id}/delete`, + action: `/admin/attendee/${note.entity_id}/note/${note.id}/delete`, active: { section: "/admin/attendees" }, buttonText: t("notes.delete_submit"), children: ( diff --git a/src/ui/templates/admin/attendees-list.tsx b/src/ui/templates/admin/attendees-list.tsx index 7033a1819d..6a384b2c6d 100644 --- a/src/ui/templates/admin/attendees-list.tsx +++ b/src/ui/templates/admin/attendees-list.tsx @@ -8,7 +8,7 @@ import { sort } from "#fp"; import { t } from "#i18n"; import type { AttendeeSort } from "#shared/db/attendees/queries.ts"; -import type { SystemNote } from "#shared/db/system-notes.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import { Raw } from "#shared/jsx/jsx-runtime.ts"; import { type ListingFilter, diff --git a/src/ui/templates/admin/listings/types.ts b/src/ui/templates/admin/listings/types.ts index 45ed6e6b57..f37f3e0e52 100644 --- a/src/ui/templates/admin/listings/types.ts +++ b/src/ui/templates/admin/listings/types.ts @@ -1,6 +1,6 @@ import type { ListingMoneyTotals } from "#shared/accounting/listing-money-totals.ts"; import type { ListingAggregateRecalculation } from "#shared/db/listings/aggregates.ts"; -import type { SystemNote } from "#shared/db/system-notes.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import type { AdminSession, Attendee, diff --git a/test/integration/admin/attendee-notes.test.ts b/test/integration/admin/attendee-notes.test.ts index 348425cc3b..dee370c05d 100644 --- a/test/integration/admin/attendee-notes.test.ts +++ b/test/integration/admin/attendee-notes.test.ts @@ -1,7 +1,8 @@ import { expect } from "@std/expect"; import { it as test } from "@std/testing/bdd"; import { t } from "#i18n"; -import { getNoteRows, getNotesForAttendee } from "#shared/db/system-notes.ts"; +import { getNoteRows, getNotesFor } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { expectRedirectWithFlash } from "#test-utils/assertions.ts"; import { getTestPrivateKey } from "#test-utils/crypto.ts"; import { describeWithEnv } from "#test-utils/db.ts"; @@ -37,8 +38,8 @@ describeWithEnv("admin > attendee notes routes", { db: true }, () => { // Redirects to the return target with a SUCCESS flash naming the action. expectRedirectWithFlash(returnUrl, t("notes.added"), true)(response); - const notes = await getNotesForAttendee( - attendee.id, + const notes = await getNotesFor( + attendeeNotes(attendee.id), await getTestPrivateKey(), ); expect(notes).toHaveLength(1); @@ -56,7 +57,7 @@ describeWithEnv("admin > attendee notes routes", { db: true }, () => { ); expect(response.status).toBe(400); expect(await response.text()).toContain("Enter a note"); - expect(await getNoteRows([attendee.id])).toEqual([]); + expect(await getNoteRows("attendee", [attendee.id])).toEqual([]); }); test("POST on a missing attendee is a 404", async () => { @@ -72,7 +73,7 @@ describeWithEnv("admin > attendee notes routes", { db: true }, () => { await adminFormPost(`/admin/attendee/${attendee.id}/note`, { note: "deletable note", }); - const [row] = await getNoteRows([attendee.id]); + const [row] = await getNoteRows("attendee", [attendee.id]); const response = await adminGet( `/admin/attendee/${attendee.id}/note/${row!.id}/delete`, @@ -98,7 +99,7 @@ describeWithEnv("admin > attendee notes routes", { db: true }, () => { await adminFormPost(`/admin/attendee/${attendee.id}/note`, { note: "to be deleted", }); - const [row] = await getNoteRows([attendee.id]); + const [row] = await getNoteRows("attendee", [attendee.id]); const returnUrl = `/admin/listing/${listing.id}`; const { response } = await adminFormPost( @@ -107,7 +108,7 @@ describeWithEnv("admin > attendee notes routes", { db: true }, () => { ); // Redirects to the return target with a SUCCESS flash confirming deletion. expectRedirectWithFlash(returnUrl, t("notes.deleted"), true)(response); - expect(await getNoteRows([attendee.id])).toEqual([]); + expect(await getNoteRows("attendee", [attendee.id])).toEqual([]); }); test("POST delete on a missing note is a 404", async () => { diff --git a/test/integration/db/migrations/2026-07-28_note_entities.test.ts b/test/integration/db/migrations/2026-07-28_note_entities.test.ts new file mode 100644 index 0000000000..478ca16af4 --- /dev/null +++ b/test/integration/db/migrations/2026-07-28_note_entities.test.ts @@ -0,0 +1,115 @@ +import { expect } from "@std/expect"; +import { describe, it as test } from "@std/testing/bdd"; +import { execute, getDb, queryAll } from "#shared/db/client.ts"; +import noteEntities from "#shared/db/migrations/2026-07-28_note_entities.ts"; +import { + recreateTable, + syncIndexes, +} from "#shared/db/migrations/schema-sync.ts"; +import { columnNames } from "#test/test-utils/db/migration-test-helpers.ts"; +import { describeWithEnv } from "#test-utils/db.ts"; +import { buildMigrationContext, indexExists } from "#test-utils/migrations.ts"; + +/** Put system_notes back the way it was before this migration: a note belonged + * to an attendee and nothing else. */ +const downgradeToAttendeeNotes = async (): Promise => { + await execute("DROP TABLE IF EXISTS system_notes"); + await execute(`CREATE TABLE system_notes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + attendee_id INTEGER NOT NULL, + type TEXT NOT NULL DEFAULT 'system' CHECK (type IN ('system', 'owner')), + note TEXT NOT NULL, + created TEXT NOT NULL DEFAULT (datetime('now')) + )`); + await execute("DROP INDEX IF EXISTS idx_system_notes_entity"); + await execute( + "CREATE INDEX idx_system_notes_attendee_id ON system_notes(attendee_id)", + ); +}; + +const runMigration = (): Promise => + noteEntities(buildMigrationContext({ recreateTable, syncIndexes })).up(); + +type StoredNote = { + entity_id: number; + entity_type: string; + id: number; + note: string; + type: string; +}; + +const storedNotes = (): Promise => + queryAll( + "SELECT id, entity_type, entity_id, type, note FROM system_notes ORDER BY id", + ); + +describeWithEnv( + "db > migrations > 2026-07-28_note_entities", + { db: true }, + () => { + test("turns an attendee's notes into notes about an attendee", async () => { + await downgradeToAttendeeNotes(); + await execute( + "INSERT INTO system_notes (attendee_id, type, note) VALUES (?, ?, ?), (?, ?, ?)", + [7, "system", "kept system", 9, "owner", "kept owner"], + ); + + await runMigration(); + + expect(await storedNotes()).toEqual([ + { + entity_id: 7, + entity_type: "attendee", + id: 1, + note: "kept system", + type: "system", + }, + { + entity_id: 9, + entity_type: "attendee", + id: 2, + note: "kept owner", + type: "owner", + }, + ]); + expect(await columnNames("system_notes")).not.toContain("attendee_id"); + expect(await indexExists("idx_system_notes_entity")).toBe(true); + expect(await indexExists("idx_system_notes_attendee_id")).toBe(false); + }); + + test("leaves an already-migrated table alone", async () => { + await downgradeToAttendeeNotes(); + await execute( + "INSERT INTO system_notes (attendee_id, type, note) VALUES (?, ?, ?)", + [3, "system", "only once"], + ); + await runMigration(); + + // A verify retry, or a crash after the work but before the marker, runs + // up() a second time against the finished table. + await runMigration(); + + expect(await storedNotes()).toEqual([ + { + entity_id: 3, + entity_type: "attendee", + id: 1, + note: "only once", + type: "system", + }, + ]); + }); + + test("refuses a note about a kind of record it does not know", async () => { + await downgradeToAttendeeNotes(); + await runMigration(); + + await expect( + getDb().execute({ + args: [1, "system", "who?"], + sql: "INSERT INTO system_notes (entity_type, entity_id, type, note) VALUES ('spaceship', ?, ?, ?)", + }), + ).rejects.toThrow(); + }); + }, +); diff --git a/test/integration/server/attendee-form-status.test.ts b/test/integration/server/attendee-form-status.test.ts index 19a6d19804..e058b7af93 100644 --- a/test/integration/server/attendee-form-status.test.ts +++ b/test/integration/server/attendee-form-status.test.ts @@ -393,8 +393,12 @@ describeWithEnv( "Noted", "noted@example.com", ); - const { createOwnerNote } = await import("#shared/db/system-notes.ts"); - await createOwnerNote(attendee.id, "Allergic to peanuts"); + const { createOwnerNote } = await import("#shared/db/notes/queries.ts"); + const { attendeeNotes } = await import("#shared/db/notes/target.ts"); + await createOwnerNote( + attendeeNotes(attendee.id), + "Allergic to peanuts", + ); const html = await ( await adminGet(`/admin/attendees/${attendee.id}/activity`) ).text(); diff --git a/test/integration/server/attendees-list.test.ts b/test/integration/server/attendees-list.test.ts index 58449d5135..421dd93dc5 100644 --- a/test/integration/server/attendees-list.test.ts +++ b/test/integration/server/attendees-list.test.ts @@ -1,7 +1,8 @@ import { expect } from "@std/expect"; import { describe, it as test } from "@std/testing/bdd"; import { ATTENDEES_PAGE_SIZE } from "#shared/db/attendees/queries.ts"; -import { createSystemNote } from "#shared/db/system-notes.ts"; +import { createSystemNote } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { assertAdminHtml, expectHtml, @@ -382,7 +383,10 @@ describeWithEnv("server (admin attendees list)", { db: true }, () => { "Alice", "alice@example.com", ); - await createSystemNote(attendee.id, "Refunded — follow up tomorrow."); + await createSystemNote( + attendeeNotes(attendee.id), + "Refunded — follow up tomorrow.", + ); const response = await adminGet("/admin/attendees"); const html = await response.text(); diff --git a/test/integration/server/attendees/placeholder-refresh.test.ts b/test/integration/server/attendees/placeholder-refresh.test.ts index bd0b1d232f..2e923fb133 100644 --- a/test/integration/server/attendees/placeholder-refresh.test.ts +++ b/test/integration/server/attendees/placeholder-refresh.test.ts @@ -5,11 +5,9 @@ import { KIND } from "#shared/accounting/kinds.ts"; import { transfersByAccount } from "#shared/accounting/queries.ts"; import { attendeesApi } from "#shared/db/attendees/api.ts"; import { execute } from "#shared/db/client.ts"; +import { createSystemNote, getNotesFor } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { reserveSession } from "#shared/db/processed-payments.ts"; -import { - createSystemNote, - getNotesForAttendee, -} from "#shared/db/system-notes.ts"; import type { Attendee } from "#shared/types.ts"; import { expectFlash } from "#test-utils/assertions.ts"; import { getTestPrivateKey } from "#test-utils/crypto.ts"; @@ -109,7 +107,7 @@ describeWithEnv( ); const privateKey = await getTestPrivateKey(); await createSystemNote( - attendee.id, + attendeeNotes(attendee.id), "This booking was kept at quantity 0 but its payment could NOT be refunded automatically because the event filled up while they were paying. Payment reference: pi_stale_note (code: capacity_full). Please refund it manually and check the [ledger](/admin/ledger/attendee/" + attendee.id + ").", @@ -117,7 +115,7 @@ describeWithEnv( await submitRefreshPayment(attendee, () => Promise.resolve(true)); - const notes = await getNotesForAttendee(attendee.id, privateKey); + const notes = await getNotesFor(attendeeNotes(attendee.id), privateKey); expect( notes.some((note) => note.note.includes("could NOT be refunded")), ).toBe(false); @@ -152,7 +150,7 @@ describeWithEnv( // note cleanup failed — re-create the stale note afterward. await refreshAndVerifyRefundCash(attendee); await createSystemNote( - attendee.id, + attendeeNotes(attendee.id), "This booking was kept at quantity 0 but its payment could NOT be refunded.", ); @@ -164,7 +162,7 @@ describeWithEnv( expect.stringContaining("up to date"), ); - const notes = await getNotesForAttendee(attendee.id, privateKey); + const notes = await getNotesFor(attendeeNotes(attendee.id), privateKey); expect( notes.some((note) => note.note.includes("could NOT be refunded")), ).toBe(false); diff --git a/test/integration/server/listings/show-actions-and-activity.test.ts b/test/integration/server/listings/show-actions-and-activity.test.ts index 4359bfc399..55ef8eeebe 100644 --- a/test/integration/server/listings/show-actions-and-activity.test.ts +++ b/test/integration/server/listings/show-actions-and-activity.test.ts @@ -3,7 +3,8 @@ import { expect } from "@std/expect"; import { describe, it as test } from "@std/testing/bdd"; import { attendeeAccount } from "#shared/accounting/accounts.ts"; import { addDays } from "#shared/dates.ts"; -import { createSystemNote } from "#shared/db/system-notes.ts"; +import { createSystemNote } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { todayInTz } from "#shared/timezone.ts"; import { createDailyListing } from "#test/integration/server/listings/_shared-setup.ts"; import { logActivity } from "#test-utils/activity-log.ts"; @@ -102,7 +103,10 @@ describeWithEnv( gross: 5000, listingId: listing.id, }); - await createSystemNote(confirmed.id, "Called ahead about access"); + await createSystemNote( + attendeeNotes(confirmed.id), + "Called ahead about access", + ); // An incomplete booking: a recognised sale that was never paid. const incomplete = await createPaidAttendeeWithoutLedger( listing.id, diff --git a/test/integration/server/payments-success-refunds.test.ts b/test/integration/server/payments-success-refunds.test.ts index cf440e16eb..35bf3f744d 100644 --- a/test/integration/server/payments-success-refunds.test.ts +++ b/test/integration/server/payments-success-refunds.test.ts @@ -284,12 +284,12 @@ describeWithEnv("server (payment flow: ticket success)", { db: true }, () => { const { getAttendeesRaw } = await import( "#shared/db/attendees/queries.ts" ); - const { getNoteRows } = await import("#shared/db/system-notes.ts"); + const { getNoteRows } = await import("#shared/db/notes/queries.ts"); const ghost = (await getAttendeesRaw(listing.id)).find( (a) => a.quantity === 0, ); expect(ghost).toBeDefined(); - expect(await getNoteRows([ghost!.id])).toHaveLength(1); + expect(await getNoteRows("attendee", [ghost!.id])).toHaveLength(1); } finally { mockRetrieve.restore(); mockRefund.restore(); diff --git a/test/integration/server/payments/replay.test.ts b/test/integration/server/payments/replay.test.ts index b7b1e673bb..1eea4bd796 100644 --- a/test/integration/server/payments/replay.test.ts +++ b/test/integration/server/payments/replay.test.ts @@ -3,8 +3,8 @@ import { expect } from "@std/expect"; import { describe, it as test } from "@std/testing/bdd"; import { handleRequest } from "#routes"; import { getAttendeesRaw } from "#shared/db/attendees/queries.ts"; +import { getNoteRows } from "#shared/db/notes/queries.ts"; import { isSessionProcessed } from "#shared/db/processed-payments.ts"; -import { getNoteRows } from "#shared/db/system-notes.ts"; import { expectHtmlResponse } from "#test-utils/assertions.ts"; import { describeWithEnv } from "#test-utils/db.ts"; import { @@ -146,7 +146,9 @@ describeWithEnv("server (payment flow)", { db: true, triggers: true }, () => { const attendees = await getAttendeesRaw(listing.id); expect(attendees.length).toBe(1); expect(attendees[0]?.quantity).toBe(0); - expect((await getNoteRows([attendees[0]!.id])).length).toBe(1); + expect( + (await getNoteRows("attendee", [attendees[0]!.id])).length, + ).toBe(1); expect(mockRefund.calls.length).toBe(1); // The session is recorded as a terminal failure. diff --git a/test/integration/server/webhooks/multi-ticket-refunds.test.ts b/test/integration/server/webhooks/multi-ticket-refunds.test.ts index 7edce3e7e4..3ea3e7c3e0 100644 --- a/test/integration/server/webhooks/multi-ticket-refunds.test.ts +++ b/test/integration/server/webhooks/multi-ticket-refunds.test.ts @@ -131,8 +131,8 @@ describeWithEnv( listing1.id, listing2.id, ); - const { getNoteRows } = await import("#shared/db/system-notes.ts"); - expect((await getNoteRows([attendee.id])).length).toBe(1); + const { getNoteRows } = await import("#shared/db/notes/queries.ts"); + expect((await getNoteRows("attendee", [attendee.id])).length).toBe(1); await expectSessionFailed("cs_multi_mismatch"); // Verify refund was attempted exactly once diff --git a/test/integration/server/webhooks/price-signature-stored-refund-and-ignore.test.ts b/test/integration/server/webhooks/price-signature-stored-refund-and-ignore.test.ts index 3851400d5b..f4ec86a589 100644 --- a/test/integration/server/webhooks/price-signature-stored-refund-and-ignore.test.ts +++ b/test/integration/server/webhooks/price-signature-stored-refund-and-ignore.test.ts @@ -4,8 +4,9 @@ import { stub } from "@std/testing/mock"; import { attendeeAccount } from "#shared/accounting/accounts.ts"; import { transfersByAccount } from "#shared/accounting/queries.ts"; import { getAttendeesRaw } from "#shared/db/attendees/queries.ts"; +import { getNoteRows, getNotesFor } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { isSessionProcessed } from "#shared/db/processed-payments.ts"; -import { getNoteRows, getNotesForAttendee } from "#shared/db/system-notes.ts"; import { balanceOf } from "#shared/ledger/project.ts"; import { expectAcknowledgedIgnore, @@ -74,8 +75,8 @@ describeWithEnv( expect(legs.some((leg) => leg.kind === "sale")).toBe(false); // The system note names the reason (PII-free) and links to the ledger. - const notes = await getNotesForAttendee( - attendee!.id, + const notes = await getNotesFor( + attendeeNotes(attendee!.id), await getTestPrivateKey(), ); expect(notes).toHaveLength(1); @@ -344,7 +345,7 @@ describeWithEnv( expect(refund.calls.length).toBe(1); const [attendee] = await getAttendeesRaw(listing.id); expect(attendee?.quantity).toBe(0); - expect(await getNoteRows([attendee!.id])).toHaveLength(1); + expect(await getNoteRows("attendee", [attendee!.id])).toHaveLength(1); const record = await isSessionProcessed("cs_refund_retry"); expect(record?.failure_data).not.toBe(""); }, diff --git a/test/integration/webhook-price-signature/helpers.ts b/test/integration/webhook-price-signature/helpers.ts index 0f509e35b6..38eba72376 100644 --- a/test/integration/webhook-price-signature/helpers.ts +++ b/test/integration/webhook-price-signature/helpers.ts @@ -3,7 +3,7 @@ import { stub } from "@std/testing/mock"; import { handleRequest } from "#routes"; import { getAttendeesRaw } from "#shared/db/attendees/queries.ts"; import { setGroupPackageMembers } from "#shared/db/groups.ts"; -import { getNoteRows } from "#shared/db/system-notes.ts"; +import { getNoteRows } from "#shared/db/notes/queries.ts"; import { stripeApi } from "#shared/stripe.ts"; import { assertJson } from "#test-utils/assertions.ts"; import { createTestGroup } from "#test-utils/db-helpers/groups.ts"; @@ -157,7 +157,7 @@ export const expectStoredRefundRecord = async ( ): Promise => { const [attendee] = await getAttendeesRaw(listingId); expect(attendee?.quantity).toBe(0); - expect(await getNoteRows([attendee!.id])).toHaveLength(1); + expect(await getNoteRows("attendee", [attendee!.id])).toHaveLength(1); }; export const expectStoredRefund = async (listingId: number): Promise => { diff --git a/test/shared/db/attendees/delete.test.ts b/test/shared/db/attendees/delete.test.ts index 37a3e52be3..61b4d6b75d 100644 --- a/test/shared/db/attendees/delete.test.ts +++ b/test/shared/db/attendees/delete.test.ts @@ -9,11 +9,12 @@ import { } from "#shared/db/listings/records.ts"; import { modifierUsedQuantities } from "#shared/db/modifier-usage.ts"; import { getAllModifiers, modifiersTable } from "#shared/db/modifiers.ts"; +import { createSystemNote, getNoteRows } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { isSessionProcessed, reserveSession, } from "#shared/db/processed-payments.ts"; -import { createSystemNote, getNoteRows } from "#shared/db/system-notes.ts"; import { insertCheckoutStage } from "#test-utils/checkout-stages.ts"; import { getTestPrivateKey } from "#test-utils/crypto.ts"; import { describeWithEnv } from "#test-utils/db.ts"; @@ -224,12 +225,15 @@ describeWithEnv("db > attendees > deleteAttendee", { db: true }, () => { "Noted Attendee", "noted@example.com", ); - await createSystemNote(attendee.id, "a note that should be purged"); - expect(await getNoteRows([attendee.id])).toHaveLength(1); + await createSystemNote( + attendeeNotes(attendee.id), + "a note that should be purged", + ); + expect(await getNoteRows("attendee", [attendee.id])).toHaveLength(1); await deleteAttendee(attendee.id); - expect(await getNoteRows([attendee.id])).toEqual([]); + expect(await getNoteRows("attendee", [attendee.id])).toEqual([]); }); test("succeeds when the attendee has no modifier usage", async () => { diff --git a/test/shared/db/migrations/schema/version/guard.test.ts b/test/shared/db/migrations/schema/version/guard.test.ts index 03684a207f..d3faf94c97 100644 --- a/test/shared/db/migrations/schema/version/guard.test.ts +++ b/test/shared/db/migrations/schema/version/guard.test.ts @@ -100,8 +100,9 @@ describe("db > migrations > schema change guard", () => { "2026-07-19_maintenance_checkpoint", "2026-07-21_activity_backfill_complete", "2026-07-22_maintenance_completion", + "2026-07-28_note_entities", ], - schemaHash: "qxxe41", + schemaHash: "w6e6g0", }); }); diff --git a/test/shared/db/system-notes.test.ts b/test/shared/db/notes/queries.test.ts similarity index 67% rename from test/shared/db/system-notes.test.ts rename to test/shared/db/notes/queries.test.ts index 9b30d2d907..e85ee25394 100644 --- a/test/shared/db/system-notes.test.ts +++ b/test/shared/db/notes/queries.test.ts @@ -5,16 +5,19 @@ import { queryOne } from "#shared/db/client.ts"; import { createOwnerNote, createSystemNote, - decryptNotes, - deleteAttendeeNote, - getAttendeeNote, + deleteNote, + getNote, getNoteRows, getNoteRowsForListing, - getNotesForAttendee, - groupNotesByAttendee, + getNotesFor, loadNotesForAttendees, loadNotesForListing, -} from "#shared/db/system-notes.ts"; +} from "#shared/db/notes/queries.ts"; +import { openNotes } from "#shared/db/notes/sealing.ts"; +import { + attendeeNotes, + groupNotesByTargetId, +} from "#shared/db/notes/target.ts"; import { getTestPrivateKey } from "#test-utils/crypto.ts"; import { describeWithEnv } from "#test-utils/db.ts"; import { createTestAttendee } from "#test-utils/db-helpers/attendees.ts"; @@ -37,11 +40,11 @@ const makeAttendee = async (name = "Note Target"): Promise => { const rawNote = (attendeeId: number): Promise<{ note: string } | null> => queryOne<{ note: string }>( - "SELECT note FROM system_notes WHERE attendee_id = ? ORDER BY id", + "SELECT note FROM system_notes WHERE entity_type = 'attendee' AND entity_id = ? ORDER BY id", [attendeeId], ); -describeWithEnv("db > system-notes", { db: true }, () => { +describeWithEnv("db > notes", { db: true }, () => { test("loads notes scoped to one listing's attendees only", async () => { const listing = await createTestListing({ maxAttendees: 50 }); const other = await createTestListing({ maxAttendees: 50 }); @@ -57,11 +60,11 @@ describeWithEnv("db > system-notes", { db: true }, () => { "Theirs", "theirs@example.com", ); - await createSystemNote(mine.id, "Note on this listing"); - await createSystemNote(theirs.id, "Note on another listing"); + await createSystemNote(attendeeNotes(mine.id), "Note on this listing"); + await createSystemNote(attendeeNotes(theirs.id), "Note on another listing"); const rows = await getNoteRowsForListing(listing.id); - expect(rows.map((r) => r.attendee_id)).toEqual([mine.id]); + expect(rows.map((r) => r.entity_id)).toEqual([mine.id]); const notes = await loadNotesForListing(listing.id, getTestPrivateKey); expect(notes).toHaveLength(1); @@ -84,15 +87,18 @@ describeWithEnv("db > system-notes", { db: true }, () => { test("stores and reads back a decrypted system note", async () => { const attendeeId = await makeAttendee(); - await createSystemNote(attendeeId, "Refunded: price changed"); + await createSystemNote( + attendeeNotes(attendeeId), + "Refunded: price changed", + ); - const notes = await getNotesForAttendee( - attendeeId, + const notes = await getNotesFor( + attendeeNotes(attendeeId), await getTestPrivateKey(), ); expect(notes).toHaveLength(1); expect(notes[0]).toMatchObject({ - attendee_id: attendeeId, + entity_id: attendeeId, note: "Refunded: price changed", type: "system", }); @@ -100,10 +106,13 @@ describeWithEnv("db > system-notes", { db: true }, () => { test("stores and reads back a decrypted owner note", async () => { const attendeeId = await makeAttendee(); - await createOwnerNote(attendeeId, "Called to confirm dietary needs"); + await createOwnerNote( + attendeeNotes(attendeeId), + "Called to confirm dietary needs", + ); - const notes = await getNotesForAttendee( - attendeeId, + const notes = await getNotesFor( + attendeeNotes(attendeeId), await getTestPrivateKey(), ); expect(notes).toHaveLength(1); @@ -115,14 +124,14 @@ describeWithEnv("db > system-notes", { db: true }, () => { test("never stores note text in plaintext", async () => { const attendeeId = await makeAttendee(); - await createSystemNote(attendeeId, "system secret"); + await createSystemNote(attendeeNotes(attendeeId), "system secret"); // The symmetric encryption format is the enc:1: envelope, not the plaintext. const stored = await rawNote(attendeeId); expect(stored?.note.startsWith("enc:")).toBe(true); expect(stored?.note).not.toContain("system secret"); const ownerAttendee = await makeAttendee("Owner Target"); - await createOwnerNote(ownerAttendee, "owner secret"); + await createOwnerNote(attendeeNotes(ownerAttendee), "owner secret"); const ownerStored = await rawNote(ownerAttendee); // The owner hybrid-RSA envelope is hyb:1:, again never the plaintext. expect(ownerStored?.note.startsWith("hyb:")).toBe(true); @@ -131,12 +140,12 @@ describeWithEnv("db > system-notes", { db: true }, () => { test("returns an attendee's notes oldest first", async () => { const attendeeId = await makeAttendee(); - await createSystemNote(attendeeId, "first"); - await createOwnerNote(attendeeId, "second"); - await createSystemNote(attendeeId, "third"); + await createSystemNote(attendeeNotes(attendeeId), "first"); + await createOwnerNote(attendeeNotes(attendeeId), "second"); + await createSystemNote(attendeeNotes(attendeeId), "third"); - const notes = await getNotesForAttendee( - attendeeId, + const notes = await getNotesFor( + attendeeNotes(attendeeId), await getTestPrivateKey(), ); expect(notes.map((n) => n.note)).toEqual(["first", "second", "third"]); @@ -145,27 +154,27 @@ describeWithEnv("db > system-notes", { db: true }, () => { test("groups notes for several attendees by attendee id", async () => { const a = await makeAttendee("Alice Notes"); const b = await makeAttendee("Bob Notes"); - await createSystemNote(a, "a1"); - await createSystemNote(b, "b1"); - await createSystemNote(a, "a2"); + await createSystemNote(attendeeNotes(a), "a1"); + await createSystemNote(attendeeNotes(b), "b1"); + await createSystemNote(attendeeNotes(a), "a2"); - const notes = await decryptNotes( - await getNoteRows([a, b]), + const notes = await openNotes( + await getNoteRows("attendee", [a, b]), await getTestPrivateKey(), ); - const grouped = groupNotesByAttendee(notes); + const grouped = groupNotesByTargetId(notes); expect(grouped.get(a)?.map((n) => n.note)).toEqual(["a1", "a2"]); expect(grouped.get(b)?.map((n) => n.note)).toEqual(["b1"]); }); test("getNoteRows returns nothing for an empty attendee list", async () => { - expect(await getNoteRows([])).toEqual([]); + expect(await getNoteRows("attendee", [])).toEqual([]); }); test("loadNotesForAttendees derives the key only when notes exist", async () => { const withNotes = await makeAttendee("Has Notes"); const withoutNotes = await makeAttendee("No Notes"); - await createSystemNote(withNotes, "hi"); + await createSystemNote(attendeeNotes(withNotes), "hi"); const lazyKey = spy(() => getTestPrivateKey()); @@ -184,34 +193,34 @@ describeWithEnv("db > system-notes", { db: true }, () => { test("getAttendeeNote loads one note scoped to its attendee", async () => { const owner = await makeAttendee("Scoped Owner"); const other = await makeAttendee("Other Owner"); - await createSystemNote(owner, "scoped note"); - const [row] = await getNoteRows([owner]); + await createSystemNote(attendeeNotes(owner), "scoped note"); + const [row] = await getNoteRows("attendee", [owner]); const pk = await getTestPrivateKey(); - const found = await getAttendeeNote(owner, row!.id, pk); + const found = await getNote(attendeeNotes(owner), row!.id, pk); expect(found?.note).toBe("scoped note"); // The same note id under a different attendee must not resolve. - expect(await getAttendeeNote(other, row!.id, pk)).toBeNull(); + expect(await getNote(attendeeNotes(other), row!.id, pk)).toBeNull(); // A missing id resolves to null too. - expect(await getAttendeeNote(owner, 9_999_999, pk)).toBeNull(); + expect(await getNote(attendeeNotes(owner), 9_999_999, pk)).toBeNull(); }); test("deleteAttendeeNote removes only the scoped note", async () => { const owner = await makeAttendee("Delete Owner"); const other = await makeAttendee("Keep Owner"); - await createSystemNote(owner, "delete me"); - await createSystemNote(other, "keep me"); - const [ownerRow] = await getNoteRows([owner]); - const [otherRow] = await getNoteRows([other]); + await createSystemNote(attendeeNotes(owner), "delete me"); + await createSystemNote(attendeeNotes(other), "keep me"); + const [ownerRow] = await getNoteRows("attendee", [owner]); + const [otherRow] = await getNoteRows("attendee", [other]); // A wrong attendee id must not delete another attendee's note. - await deleteAttendeeNote(other, ownerRow!.id); - expect(await getNoteRows([owner])).toHaveLength(1); + await deleteNote(attendeeNotes(other), ownerRow!.id); + expect(await getNoteRows("attendee", [owner])).toHaveLength(1); - await deleteAttendeeNote(owner, ownerRow!.id); - expect(await getNoteRows([owner])).toEqual([]); + await deleteNote(attendeeNotes(owner), ownerRow!.id); + expect(await getNoteRows("attendee", [owner])).toEqual([]); // The other attendee's note is untouched. - expect((await getNoteRows([other]))[0]?.id).toBe(otherRow!.id); + expect((await getNoteRows("attendee", [other]))[0]?.id).toBe(otherRow!.id); }); }); diff --git a/test/shared/db/notes/target.test.ts b/test/shared/db/notes/target.test.ts new file mode 100644 index 0000000000..062ebf0616 --- /dev/null +++ b/test/shared/db/notes/target.test.ts @@ -0,0 +1,86 @@ +import { expect } from "@std/expect"; +import { describe, it as test } from "@std/testing/bdd"; +import { + attendeeNotes, + groupNotesByTargetId, + isNoteEntity, + isSameTarget, + NOTE_ENTITIES, + notesAbout, + targetsSelectedBy, + targetsWhere, + targetWhere, +} from "#shared/db/notes/target.ts"; + +describe("what a note is about", () => { + test("names a record by its kind and its id", () => { + expect(attendeeNotes(7)).toEqual({ entity: "attendee", id: 7 }); + }); + + test("every listed kind can name a record", () => { + expect(NOTE_ENTITIES).toContain("attendee"); + for (const entity of NOTE_ENTITIES) { + expect(notesAbout(entity)(3)).toEqual({ entity, id: 3 }); + } + }); + + test("accepts only the kinds on the list", () => { + expect(isNoteEntity("attendee")).toBe(true); + expect(isNoteEntity("listing")).toBe(false); + expect(isNoteEntity("")).toBe(false); + }); + + test("two targets match only when both kind and id match", () => { + expect(isSameTarget(attendeeNotes(1), attendeeNotes(1))).toBe(true); + expect(isSameTarget(attendeeNotes(1), attendeeNotes(2))).toBe(false); + }); + + test("asks for one record by both of its parts", () => { + expect(targetWhere(attendeeNotes(4))).toEqual({ + args: ["attendee", 4], + sql: "entity_type = ? AND entity_id = ?", + }); + }); + + test("asks for several records of one kind in one go", () => { + expect(targetsWhere("attendee", [4, 5])).toEqual({ + args: ["attendee", 4, 5], + sql: "entity_type = ? AND entity_id IN (?, ?)", + }); + }); + + test("asks for nothing when given no ids", () => { + // `0 = 1` is false for every row, so an empty list selects no notes rather + // than every note of that kind. + expect(targetsWhere("attendee", [])).toEqual({ args: [], sql: "0 = 1" }); + }); + + test("keeps the subquery's own arguments after the kind", () => { + expect( + targetsSelectedBy("attendee", { + args: [12], + sql: "SELECT attendee_id FROM listing_attendees WHERE listing_id = ?", + }), + ).toEqual({ + args: ["attendee", 12], + sql: "entity_type = ? AND entity_id IN (SELECT attendee_id FROM listing_attendees WHERE listing_id = ?)", + }); + }); + + test("groups notes by the record they are about, keeping their order", () => { + const notes = [ + { entity_id: 1, note: "first" }, + { entity_id: 2, note: "other" }, + { entity_id: 1, note: "second" }, + ]; + + const grouped = groupNotesByTargetId(notes); + + expect(grouped.get(1)?.map((note) => note.note)).toEqual([ + "first", + "second", + ]); + expect(grouped.get(2)?.map((note) => note.note)).toEqual(["other"]); + expect(grouped.get(3)).toBeUndefined(); + }); +}); diff --git a/test/shared/db/orphan-attendees.test.ts b/test/shared/db/orphan-attendees.test.ts index 48241b2010..b1b70c0df3 100644 --- a/test/shared/db/orphan-attendees.test.ts +++ b/test/shared/db/orphan-attendees.test.ts @@ -10,11 +10,12 @@ import { assertExists } from "@std/assert"; import { expect } from "@std/expect"; import { describe, it as test } from "@std/testing/bdd"; import { getDb, insert, queryOne, requireOne } from "#shared/db/client.ts"; +import { createSystemNote, getNoteRows } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { countOrphanedAttendees, purgeOrphanedAttendees, } from "#shared/db/orphan-attendees.ts"; -import { createSystemNote, getNoteRows } from "#shared/db/system-notes.ts"; import { nowIso, nowMs } from "#shared/now.ts"; import { insertCheckoutStage } from "#test-utils/checkout-stages.ts"; import { describeWithEnv } from "#test-utils/db.ts"; @@ -148,13 +149,13 @@ describeWithEnv("db > orphan-attendees", { db: true }, () => { processed_at: nowIso(), }), ); - await createSystemNote(id, "orphan note"); + await createSystemNote(attendeeNotes(id), "orphan note"); await purgeOrphanedAttendees(nowIso()); expect(await childCount("attendee_answers", id)).toBe(0); expect(await childCount("processed_payments", id)).toBe(0); - expect(await getNoteRows([id])).toEqual([]); + expect(await getNoteRows("attendee", [id])).toEqual([]); }); test("removes the orphan's checkout stage", async () => { diff --git a/test/specs/steps/payment-capacity.ts b/test/specs/steps/payment-capacity.ts index bb0ddbedd4..3d8bbfdabb 100644 --- a/test/specs/steps/payment-capacity.ts +++ b/test/specs/steps/payment-capacity.ts @@ -4,8 +4,9 @@ import { Given, Then, When } from "@cucumber/cucumber"; import { expect } from "@std/expect"; import { handleRequest } from "#routes"; import { getAttendeesRaw } from "#shared/db/attendees/queries.ts"; +import { getNotesFor } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { isSessionProcessed } from "#shared/db/processed-payments.ts"; -import { getNotesForAttendee } from "#shared/db/system-notes.ts"; import { requiredWorldValue, type TicketsWorld, @@ -147,8 +148,8 @@ Then( async function (this: TicketsWorld): Promise { const attendeeId = requiredWorldValue(this.placeholderId, "placeholder id"); const sessionId = requiredWorldValue(this.sessionId, "session id"); - const notes = await getNotesForAttendee( - attendeeId, + const notes = await getNotesFor( + attendeeNotes(attendeeId), await getTestPrivateKey(), ); expect(notes).toHaveLength(1); diff --git a/test/test-utils/reservation/helpers.ts b/test/test-utils/reservation/helpers.ts index af1e9f420f..95ddb47697 100644 --- a/test/test-utils/reservation/helpers.ts +++ b/test/test-utils/reservation/helpers.ts @@ -239,7 +239,7 @@ export const expectRefundedPlaceholder = async ( expect(await modifierUsageCount(addOnId)).toBe(0); expect(refund.calls[0]!.args).toEqual([paymentIntentId]); expect(refund.calls.length).toBe(1); - const { getNoteRows } = await import("#shared/db/system-notes.ts"); - expect((await getNoteRows([attendees[0]!.id])).length).toBe(1); + const { getNoteRows } = await import("#shared/db/notes/queries.ts"); + expect((await getNoteRows("attendee", [attendees[0]!.id])).length).toBe(1); return attendees; }; diff --git a/test/test-utils/webhooks.ts b/test/test-utils/webhooks.ts index c50c8a80f0..c6f15f6716 100644 --- a/test/test-utils/webhooks.ts +++ b/test/test-utils/webhooks.ts @@ -213,8 +213,8 @@ export const expectRefundedWithNote = async ( mockRefund: { calls: unknown[] }, ): Promise => { expect(mockRefund.calls.length).toBe(1); - const { getNoteRows } = await import("#shared/db/system-notes.ts"); - expect((await getNoteRows([attendeeId])).length).toBe(1); + const { getNoteRows } = await import("#shared/db/notes/queries.ts"); + expect((await getNoteRows("attendee", [attendeeId])).length).toBe(1); }; /** diff --git a/test/ui/templates/admin/attendee-notes.test.tsx b/test/ui/templates/admin/attendee-notes.test.tsx index 277b7d2fbe..e6b20f2108 100644 --- a/test/ui/templates/admin/attendee-notes.test.tsx +++ b/test/ui/templates/admin/attendee-notes.test.tsx @@ -1,6 +1,6 @@ import { expect } from "@std/expect"; import { beforeAll, describe, it as test } from "@std/testing/bdd"; -import type { SystemNote } from "#shared/db/system-notes.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import { AddNoteLink, AttendeeNotesSection, @@ -15,8 +15,9 @@ import { import { withEnv } from "#test-utils/env.ts"; const note = (overrides: Partial = {}): SystemNote => ({ - attendee_id: 5, created: "2026-06-23T10:00:00.000Z", + entity_id: 5, + entity_type: "attendee", id: 1, note: "Refunded — see the [ledger](/admin/ledger?attendee=5).", type: "system", @@ -117,9 +118,9 @@ describe("AttendeeNotesSummary", () => { isOwner={false} names={names} notes={[ - note({ attendee_id: 5, id: 1, note: "first" }), - note({ attendee_id: 5, id: 2, note: "second" }), - note({ attendee_id: 6, id: 3, note: "other" }), + note({ entity_id: 5, id: 1, note: "first" }), + note({ entity_id: 5, id: 2, note: "second" }), + note({ entity_id: 6, id: 3, note: "other" }), ]} />, ); @@ -136,7 +137,7 @@ describe("AttendeeNotesSummary", () => { , ); expect(html).toContain("#9"); @@ -149,7 +150,7 @@ describe("AttendeeNotesSummary", () => { , ); expect(staffHtml).toContain("see the ledger"); @@ -159,7 +160,7 @@ describe("AttendeeNotesSummary", () => { , ); expect(ownerHtml).toContain('href="/admin/ledger?attendee=5"'); diff --git a/test/ui/templates/admin/attendee-page.test.ts b/test/ui/templates/admin/attendee-page.test.ts index d56925dd83..4e15c31ca4 100644 --- a/test/ui/templates/admin/attendee-page.test.ts +++ b/test/ui/templates/admin/attendee-page.test.ts @@ -1,7 +1,7 @@ import { expect } from "@std/expect"; import { beforeAll, describe, it as test } from "@std/testing/bdd"; import type { AttendeeStatus } from "#shared/db/attendee-statuses.ts"; -import type { SystemNote } from "#shared/db/system-notes.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import { attendeeBanner, ContactHistory, @@ -24,8 +24,9 @@ const attendeeStatus = ( }); const OWNER_NOTE: SystemNote = { - attendee_id: 1, created: "2026-07-11T10:00:00.000Z", + entity_id: 1, + entity_type: "attendee", id: 1, note: "Bring identification", type: "owner", diff --git a/test/ui/templates/admin/attendees-list.test.ts b/test/ui/templates/admin/attendees-list.test.ts index d7f24ed202..b1cc96cb0d 100644 --- a/test/ui/templates/admin/attendees-list.test.ts +++ b/test/ui/templates/admin/attendees-list.test.ts @@ -1,6 +1,6 @@ import { expect } from "@std/expect"; import { beforeAll, describe, it as test } from "@std/testing/bdd"; -import type { SystemNote } from "#shared/db/system-notes.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import type { AttendeeTableRow } from "#shared/types.ts"; import { type AttendeesListPageProps, @@ -87,8 +87,9 @@ describe("adminAttendeesListPage", () => { test("surfaces a red notes summary for attendees that have notes", () => { const noteRow: SystemNote = { - attendee_id: 1, created: "2026-06-23T10:00:00.000Z", + entity_id: 1, + entity_type: "attendee", id: 1, note: "needs a follow-up call", type: "system", From 8d97fc29517f73f04a443f2a2cb859a28c18c9b0 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 19:00:19 +0000 Subject: [PATCH 02/17] Retire the attendee-only index from the note table's first migration Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- src/shared/db/migrations/2026-06-23_system_notes.ts | 3 +-- .../integration/db/migrations/2026-07-28_note_entities.test.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/shared/db/migrations/2026-06-23_system_notes.ts b/src/shared/db/migrations/2026-06-23_system_notes.ts index de51785ac5..e768adda94 100644 --- a/src/shared/db/migrations/2026-06-23_system_notes.ts +++ b/src/shared/db/migrations/2026-06-23_system_notes.ts @@ -2,9 +2,8 @@ import { schemaMigration } from "./define.ts"; export default schemaMigration( "2026-06-23_system_notes", - "Add system_notes table of per-attendee operator-visible notes (encrypted: system notes with DB_ENCRYPTION_KEY, owner notes with the owner public key)", + "Add system_notes table of per-attendee operator-visible notes (encrypted: system notes with DB_ENCRYPTION_KEY, owner notes with the owner public key). (This historically also owned an attendee_id column and its idx_system_notes_attendee_id index, since replaced — a note now names the kind of record it is about and which one; see 2026-07-28_note_entities.)", { - indexes: ["idx_system_notes_attendee_id"], newTables: ["system_notes"], }, ); diff --git a/test/integration/db/migrations/2026-07-28_note_entities.test.ts b/test/integration/db/migrations/2026-07-28_note_entities.test.ts index 478ca16af4..eaa5491750 100644 --- a/test/integration/db/migrations/2026-07-28_note_entities.test.ts +++ b/test/integration/db/migrations/2026-07-28_note_entities.test.ts @@ -1,5 +1,5 @@ import { expect } from "@std/expect"; -import { describe, it as test } from "@std/testing/bdd"; +import { it as test } from "@std/testing/bdd"; import { execute, getDb, queryAll } from "#shared/db/client.ts"; import noteEntities from "#shared/db/migrations/2026-07-28_note_entities.ts"; import { From 2eec413916bceaf68417edad36873adef108b237 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 19:14:45 +0000 Subject: [PATCH 03/17] Answer the schema, registry and code-quality checks the change moves Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- src/shared/db/notes/target.ts | 9 +------- .../migration-restore-verify.test.ts | 7 ++++-- .../migration-round-trip-budget.test.ts | 2 +- .../servicing/purge-edge-cases.test.ts | 21 ++++++++--------- test/shared/db/migrations/registry.test.ts | 11 +++++++-- .../schema/tables-questions.test.ts | 2 +- test/shared/db/notes/target.test.ts | 23 ++++--------------- 7 files changed, 30 insertions(+), 45 deletions(-) diff --git a/src/shared/db/notes/target.ts b/src/shared/db/notes/target.ts index ac98a38b25..9c6d18be66 100644 --- a/src/shared/db/notes/target.ts +++ b/src/shared/db/notes/target.ts @@ -19,9 +19,6 @@ export type NoteEntity = v.InferOutput; export const NOTE_ENTITIES = NoteEntitySchema.options; -export const isNoteEntity = (value: string): value is NoteEntity => - v.is(NoteEntitySchema, value); - /** One record a note is about: its kind, and which one of that kind. */ export interface NoteTarget { entity: NoteEntity; @@ -29,17 +26,13 @@ export interface NoteTarget { } /** Build "a note about this kind of thing" once, then name records with it. */ -export const notesAbout = +const notesAbout = (entity: NoteEntity) => (id: number): NoteTarget => ({ entity, id }); /** The record kind notes started with, and still the only one that has them. */ export const attendeeNotes = notesAbout("attendee"); -/** Do these two targets name the same record? */ -export const isSameTarget = (left: NoteTarget, right: NoteTarget): boolean => - left.entity === right.entity && left.id === right.id; - /** Group notes by which record they are about — for a batch about one kind of * record, where the id alone tells them apart. Input order is kept per group. */ export const groupNotesByTargetId = ( diff --git a/test/integration/migration-restore-verify.test.ts b/test/integration/migration-restore-verify.test.ts index 833ec20445..ed99a71e01 100644 --- a/test/integration/migration-restore-verify.test.ts +++ b/test/integration/migration-restore-verify.test.ts @@ -69,8 +69,11 @@ describeWithEnv( // absent-table checks cannot be rebuilt by a restore case. enabled_features // now owns the triggers that keep saved feature data and visibility in step. // The built-site marker drop is also removal-only. The activity backfill - // completion migration is data-only and covered by its direct tests. - expect(additiveMigrations.length).toBe(MIGRATIONS.length - 22); + // completion migration is data-only and covered by its direct tests. The + // note-entities migration rebuilds system_notes around its new + // entity_type/entity_id pair, owning no additive objects to drop and + // restore; it has its own migration test. + expect(additiveMigrations.length).toBe(MIGRATIONS.length - 23); }); test("restores triggers attached to a dropped table", async () => { diff --git a/test/integration/migration-round-trip-budget.test.ts b/test/integration/migration-round-trip-budget.test.ts index 98c87819fe..73aea37066 100644 --- a/test/integration/migration-round-trip-budget.test.ts +++ b/test/integration/migration-round-trip-budget.test.ts @@ -119,7 +119,7 @@ describeWithEnv("migration request round-trip budget", { db: true }, () => { } } expect(finished).toBe(true); - expect(continuations).toBe(4); + expect(continuations).toBe(5); const result = await getDb().execute({ args: pendingIds, diff --git a/test/integration/servicing/purge-edge-cases.test.ts b/test/integration/servicing/purge-edge-cases.test.ts index 7b22ec5178..59c774ab69 100644 --- a/test/integration/servicing/purge-edge-cases.test.ts +++ b/test/integration/servicing/purge-edge-cases.test.ts @@ -15,6 +15,8 @@ import { expect } from "@std/expect"; import { it as test } from "@std/testing/bdd"; import { getDb } from "#shared/db/client.ts"; +import { createSystemNote, getNoteRows } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { countOrphanedAttendees, purgeOrphanedAttendees, @@ -33,14 +35,9 @@ import { // jscpd:ignore-end -/** Insert a system_notes row for an attendee (a dependent row the purge clears). */ -const attachSystemNote = async (attendeeId: number): Promise => { - await getDb().execute({ - args: [attendeeId], - sql: `INSERT INTO system_notes (attendee_id, type, note, created) - VALUES (?, 'system', 'x', '2026-01-01T00:00:00Z')`, - }); -}; +/** How many notes this attendee has — a dependent row the purge clears. */ +const noteCount = async (attendeeId: number): Promise => + (await getNoteRows("attendee", [attendeeId])).length; describeWithEnv("servicing edge cases — purge", { db: true }, () => { test("a servicing orphan created exactly at the cutoff survives; one ms earlier is swept", async () => { @@ -82,11 +79,11 @@ describeWithEnv("servicing edge cases — purge", { db: true }, () => { test("a servicing orphan's system_notes are swept with the attendee", async () => { const { id } = await createServicingHold(); await orphanServicingEvent(id); - await attachSystemNote(id); - expect(await childRowCount("system_notes", id)).toBe(1); + await createSystemNote(attendeeNotes(id), "swept with the attendee"); + expect(await noteCount(id)).toBe(1); await purgeOrphanedAttendees(nowIso()); - // system_notes is a dependent row, so the shared purge clears it. - expect(await childRowCount("system_notes", id)).toBe(0); + // A note is a dependent row, so the shared purge clears it. + expect(await noteCount(id)).toBe(0); }); test("the purge sweeps both a servicing orphan and an attendee orphan in one batch", async () => { diff --git a/test/shared/db/migrations/registry.test.ts b/test/shared/db/migrations/registry.test.ts index 8e46d109e5..b4130c0baa 100644 --- a/test/shared/db/migrations/registry.test.ts +++ b/test/shared/db/migrations/registry.test.ts @@ -31,12 +31,19 @@ describe("db > migration registry", () => { }); test("orders every scheduled-maintenance schema change", () => { - expect(MIGRATION_IDS.slice(-5)).toEqual([ + const maintenanceOrder = [ "2026-07-18_maintenance_tasks", "2026-07-18_drop_built_sites_last_pruned", "2026-07-19_maintenance_checkpoint", "2026-07-21_activity_backfill_complete", "2026-07-22_maintenance_completion", - ]); + ]; + // Taken from where the run starts rather than from the end of the list, so + // a later migration does not look like the maintenance run losing its order. + const start = MIGRATION_IDS.indexOf(maintenanceOrder[0]!); + + expect(MIGRATION_IDS.slice(start, start + maintenanceOrder.length)).toEqual( + maintenanceOrder, + ); }); }); diff --git a/test/shared/db/migrations/schema/tables-questions.test.ts b/test/shared/db/migrations/schema/tables-questions.test.ts index fab30dcd42..21ea3a3f83 100644 --- a/test/shared/db/migrations/schema/tables-questions.test.ts +++ b/test/shared/db/migrations/schema/tables-questions.test.ts @@ -5,7 +5,7 @@ import { jsonHash } from "#test-utils/hash.ts"; test("keeps the complete question and built-site schema exact", async () => { expect(await jsonHash(questionTables)).toBe( - "b2cb65571a95e6cad5cf42d2de7f88818719da14151f8eabbff2e761f8f52c85", + "2a24e32ee290fb0761fae15956b64c8246cc2b87845a5260c21943067131facb", ); }); diff --git a/test/shared/db/notes/target.test.ts b/test/shared/db/notes/target.test.ts index 062ebf0616..27b41f097f 100644 --- a/test/shared/db/notes/target.test.ts +++ b/test/shared/db/notes/target.test.ts @@ -3,10 +3,7 @@ import { describe, it as test } from "@std/testing/bdd"; import { attendeeNotes, groupNotesByTargetId, - isNoteEntity, - isSameTarget, NOTE_ENTITIES, - notesAbout, targetsSelectedBy, targetsWhere, targetWhere, @@ -17,22 +14,10 @@ describe("what a note is about", () => { expect(attendeeNotes(7)).toEqual({ entity: "attendee", id: 7 }); }); - test("every listed kind can name a record", () => { - expect(NOTE_ENTITIES).toContain("attendee"); - for (const entity of NOTE_ENTITIES) { - expect(notesAbout(entity)(3)).toEqual({ entity, id: 3 }); - } - }); - - test("accepts only the kinds on the list", () => { - expect(isNoteEntity("attendee")).toBe(true); - expect(isNoteEntity("listing")).toBe(false); - expect(isNoteEntity("")).toBe(false); - }); - - test("two targets match only when both kind and id match", () => { - expect(isSameTarget(attendeeNotes(1), attendeeNotes(1))).toBe(true); - expect(isSameTarget(attendeeNotes(1), attendeeNotes(2))).toBe(false); + test("lists the kinds of record a note can be about", () => { + // The database CHECK is built from this list, so it is also what a stored + // note is allowed to say. + expect([...NOTE_ENTITIES]).toEqual(["attendee"]); }); test("asks for one record by both of its parts", () => { From 873189f498307aaa71e2f5297c5127542c0ee872 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 19:26:02 +0000 Subject: [PATCH 04/17] Open one note through the same opener as a batch The single read had a fallback that no input could reach, only there to satisfy the compiler. Opening one note is now the shared step a batch is built from, so there is no unreachable branch to test. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- src/shared/db/notes/queries.ts | 6 ++---- src/shared/db/notes/sealing.ts | 17 ++++++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/shared/db/notes/queries.ts b/src/shared/db/notes/queries.ts index 088da3a3bd..07cc8e501c 100644 --- a/src/shared/db/notes/queries.ts +++ b/src/shared/db/notes/queries.ts @@ -13,7 +13,7 @@ import { type SqlStatement, } from "#shared/db/client.ts"; import { nowIso } from "#shared/now.ts"; -import { openNotes, sealNote } from "./sealing.ts"; +import { openNote, openNotes, sealNote } from "./sealing.ts"; import { type NoteEntity, type NoteTarget, @@ -145,9 +145,7 @@ export const getNote = async ( [noteId, ...args], ); const row = rows[0]; - if (!row) return null; - const [note] = await openNotes([row], privateKey); - return note ?? null; + return row ? openNote(row, privateKey) : null; }; /** Delete one note, tied to its record so a stray id can't reach another's. */ diff --git a/src/shared/db/notes/sealing.ts b/src/shared/db/notes/sealing.ts index 40fe166549..13c2259835 100644 --- a/src/shared/db/notes/sealing.ts +++ b/src/shared/db/notes/sealing.ts @@ -37,7 +37,15 @@ export const sealNote = ( * would not open either and the page never renders — so a placeholder would * hide a whole-system failure behind a branch nothing can reach. Let it throw. */ -const openNote = ( +export const openNote = async ( + row: SystemNoteRow, + privateKey: CryptoKey, +): Promise => ({ + ...row, + note: await openText(row, privateKey), +}); + +const openText = ( row: SystemNoteRow, privateKey: CryptoKey, ): Promise => @@ -50,9 +58,4 @@ export const openNotes = ( rows: SystemNoteRow[], privateKey: CryptoKey, ): Promise => - Promise.all( - rows.map(async (row) => ({ - ...row, - note: await openNote(row, privateKey), - })), - ); + Promise.all(rows.map((row) => openNote(row, privateKey))); From 506d29ab5c71218310d8164cdbf79bb594984bc6 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 19:37:58 +0000 Subject: [PATCH 05/17] Put the note routes' tests where the source can find them, and let a moved test not stop a mutation run The mutation gate demands a test at the mirrored path; the note routes' suite was under test/integration, so the gate had nothing direct to mutate against. It drives real pages through the session helpers, which is what test/features holds. The walk that finds test files also died on a path that had moved: the guard for a missing directory was around creating the reader, but the disk is only reached when the entries are read. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- scripts/mutation.ts | 28 ++++++++++++++----- .../admin/attendee-notes.test.ts | 0 2 files changed, 21 insertions(+), 7 deletions(-) rename test/{integration => features}/admin/attendee-notes.test.ts (100%) diff --git a/scripts/mutation.ts b/scripts/mutation.ts index 4520abf6ca..c31107fa92 100644 --- a/scripts/mutation.ts +++ b/scripts/mutation.ts @@ -48,15 +48,29 @@ Examples: /** Glob metacharacters; a path segment with none is a fixed directory name. */ const GLOB_CHARS = /[*?{}[\]]/; -/** Every file under `dir`, recursively; a missing directory yields nothing. */ -async function* walkFiles(dir: string): AsyncGenerator { - let entries: AsyncIterable; +/** + * What `dir` holds, or nothing when there is no such directory — a path that + * has since moved or gone is asked for and simply answers with nothing. Reading + * the entries is what reaches the disk, so a missing directory shows up here + * rather than where the reader was created. + */ +const dirEntriesOrNone = async (dir: string): Promise => { + const entries: Deno.DirEntry[] = []; try { - entries = Deno.readDir(dir); - } catch { - return; + for await (const entry of Deno.readDir(dir)) entries.push(entry); + } catch (error) { + const gone = + error instanceof Deno.errors.NotFound || + error instanceof Deno.errors.NotADirectory; + if (!gone) throw error; + return []; } - for await (const entry of entries) { + return entries; +}; + +/** Every file under `dir`, recursively; a missing directory yields nothing. */ +async function* walkFiles(dir: string): AsyncGenerator { + for (const entry of await dirEntriesOrNone(dir)) { const path = join(dir, entry.name); if (entry.isDirectory) yield* walkFiles(path); else if (entry.isFile) yield path; diff --git a/test/integration/admin/attendee-notes.test.ts b/test/features/admin/attendee-notes.test.ts similarity index 100% rename from test/integration/admin/attendee-notes.test.ts rename to test/features/admin/attendee-notes.test.ts From fc912b906b627c2eaae7f5f6951311d3e134d9db Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 19:43:59 +0000 Subject: [PATCH 06/17] Give the new files direct tests, and record the four modules that have none Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- TODO.md | 28 ++++++ .../2026-07-28_note_entities.test.ts | 75 ++++++++++++++- test/shared/db/notes/sealing.test.ts | 95 +++++++++++++++++++ 3 files changed, 196 insertions(+), 2 deletions(-) rename test/{integration => shared}/db/migrations/2026-07-28_note_entities.test.ts (61%) create mode 100644 test/shared/db/notes/sealing.test.ts diff --git a/TODO.md b/TODO.md index 87438d645c..6d4ba2916b 100644 --- a/TODO.md +++ b/TODO.md @@ -1761,3 +1761,31 @@ as the supervisor is up, closing the gap. That means changing what and thinking again about the startup grace, which exists because a process id can be given to somebody else after the original has gone. Start at `RUN_STARTUP_GRACE_MS` in `isolation-state.ts` and the comment above it. + +## Four feature modules have no test at their mirrored path + +*Origin: `deno task precommit:mutation` on the notes-migration branch, which +could not start.* + +The mutation gate refuses to run a source that has mutants but no test at its +mirrored path under `test/`. Four modules are in that state: + +- `src/features/admin/attendee-page.ts` (55 mutants) +- `src/features/admin/attendees-list.ts` (37) +- `src/features/admin/listing-page-data.ts` (45) +- `src/features/api/payment-processing/store-refund.ts` (29) + +Nothing needs moving: no test imports any of them. They are reached only +through the app, by integration and Cucumber journeys, so each needs a direct +test written at `test/features/…` to match its path. + +This blocks the gate for *any* branch that touches one of them, however small +the change — the notes migration only swapped an import in each. Until they +have direct tests, a branch touching them can prove its own work with a +targeted `deno task mutation ` run instead. + +`src/features/admin/attendee-notes.ts` was in the same state and is now fixed: +its route suite drives real pages through the session helpers, so it moved from +`test/integration/admin/` to `test/features/admin/`, which is where that kind +of suite belongs (see "Let the misplaced-test list see past request helpers" +above). The other four have no such suite to move. diff --git a/test/integration/db/migrations/2026-07-28_note_entities.test.ts b/test/shared/db/migrations/2026-07-28_note_entities.test.ts similarity index 61% rename from test/integration/db/migrations/2026-07-28_note_entities.test.ts rename to test/shared/db/migrations/2026-07-28_note_entities.test.ts index eaa5491750..46e4b5db21 100644 --- a/test/integration/db/migrations/2026-07-28_note_entities.test.ts +++ b/test/shared/db/migrations/2026-07-28_note_entities.test.ts @@ -2,10 +2,12 @@ import { expect } from "@std/expect"; import { it as test } from "@std/testing/bdd"; import { execute, getDb, queryAll } from "#shared/db/client.ts"; import noteEntities from "#shared/db/migrations/2026-07-28_note_entities.ts"; +import { MIGRATION_IDS } from "#shared/db/migrations/registry.ts"; import { recreateTable, syncIndexes, } from "#shared/db/migrations/schema-sync.ts"; +import type { Migration } from "#shared/db/migrations/types.ts"; import { columnNames } from "#test/test-utils/db/migration-test-helpers.ts"; import { describeWithEnv } from "#test-utils/db.ts"; import { buildMigrationContext, indexExists } from "#test-utils/migrations.ts"; @@ -27,8 +29,25 @@ const downgradeToAttendeeNotes = async (): Promise => { ); }; -const runMigration = (): Promise => - noteEntities(buildMigrationContext({ recreateTable, syncIndexes })).up(); +const migration = (): Migration => + noteEntities(buildMigrationContext({ recreateTable, syncIndexes })); + +const runMigration = (): Promise => migration().up(); + +/** Stop partway: the columns are added, but the rebuild never happened. */ +const addColumnsWithoutRebuilding = async ( + ...columns: string[] +): Promise => { + for (const column of columns) { + await execute( + `ALTER TABLE system_notes ADD COLUMN ${column} ${ + column === "entity_type" + ? "TEXT NOT NULL DEFAULT 'attendee'" + : "INTEGER NOT NULL DEFAULT 0" + }`, + ); + } +}; type StoredNote = { entity_id: number; @@ -100,6 +119,58 @@ describeWithEnv( ]); }); + test("carries on from a run that stopped after the first column", async () => { + await downgradeToAttendeeNotes(); + await execute( + "INSERT INTO system_notes (attendee_id, type, note) VALUES (?, ?, ?)", + [5, "system", "half-migrated"], + ); + await addColumnsWithoutRebuilding("entity_type"); + + await runMigration(); + + expect(await storedNotes()).toEqual([ + { + entity_id: 5, + entity_type: "attendee", + id: 1, + note: "half-migrated", + type: "system", + }, + ]); + }); + + test("carries on from a run that stopped after both columns", async () => { + await downgradeToAttendeeNotes(); + await execute( + "INSERT INTO system_notes (attendee_id, type, note) VALUES (?, ?, ?)", + [6, "owner", "columns already there"], + ); + await addColumnsWithoutRebuilding("entity_type", "entity_id"); + + await runMigration(); + + expect(await storedNotes()).toEqual([ + { + entity_id: 6, + entity_type: "attendee", + id: 1, + note: "columns already there", + type: "owner", + }, + ]); + }); + + test("is registered under the id it declares, and says what it does", () => { + const { description, id } = migration(); + + // The id is what the applied-marker is written under: a mismatch with the + // registry would run the migration again on every boot. + expect(MIGRATION_IDS).toContain(id); + expect(description).toContain("entity_type"); + expect(description).toContain("entity_id"); + }); + test("refuses a note about a kind of record it does not know", async () => { await downgradeToAttendeeNotes(); await runMigration(); diff --git a/test/shared/db/notes/sealing.test.ts b/test/shared/db/notes/sealing.test.ts new file mode 100644 index 0000000000..240ed096f2 --- /dev/null +++ b/test/shared/db/notes/sealing.test.ts @@ -0,0 +1,95 @@ +import { expect } from "@std/expect"; +import { describe, it as test } from "@std/testing/bdd"; +import { decrypt } from "#shared/crypto/encryption.ts"; +import { decryptWithOwnerKey } from "#shared/crypto/keys.ts"; +import type { + EnvKeyEncrypted, + OwnerKeyEncrypted, +} from "#shared/crypto/sealed.ts"; +import { openNote, openNotes, sealNote } from "#shared/db/notes/sealing.ts"; +import type { SystemNoteRow } from "#shared/db/notes/types.ts"; +import { getTestPrivateKey } from "#test-utils/crypto.ts"; +import { describeWithEnv } from "#test-utils/db.ts"; + +const row = ( + type: Type, + note: string, + id = 1, +): SystemNoteRow => + ({ + created: "2026-07-28T10:00:00.000Z", + entity_id: 4, + entity_type: "attendee", + id, + note, + type, + }) as SystemNoteRow; + +describeWithEnv("db > notes > sealing", { db: true }, () => { + describe("sealing a note", () => { + test("seals a system note so the data key alone opens it", async () => { + const sealed = await sealNote("system", "system secret"); + + expect(sealed.startsWith("enc:")).toBe(true); + expect(sealed).not.toContain("system secret"); + expect(await decrypt(sealed as EnvKeyEncrypted)).toBe("system secret"); + }); + + test("seals an owner note so only the owner key opens it", async () => { + const sealed = await sealNote("owner", "owner secret"); + + expect(sealed.startsWith("hyb:")).toBe(true); + expect(sealed).not.toContain("owner secret"); + expect( + await decryptWithOwnerKey( + sealed as OwnerKeyEncrypted, + await getTestPrivateKey(), + ), + ).toBe("owner secret"); + }); + }); + + describe("opening a note", () => { + test("opens each kind the way it was sealed", async () => { + const sealed = [ + row("system", await sealNote("system", "from the app"), 1), + row("owner", await sealNote("owner", "from the operator"), 2), + ]; + + const opened = await openNotes(sealed, await getTestPrivateKey()); + + expect(opened.map((note) => note.note)).toEqual([ + "from the app", + "from the operator", + ]); + }); + + test("keeps everything about the note except its sealed text", async () => { + const sealed = row("system", await sealNote("system", "kept"), 12); + + const opened = await openNote(sealed, await getTestPrivateKey()); + + expect(opened).toEqual({ ...sealed, note: "kept" }); + }); + + test("opens a batch in the order it was given", async () => { + const sealed = await Promise.all( + ["first", "second", "third"].map(async (text, index) => + row("system", await sealNote("system", text), index + 1), + ), + ); + + const opened = await openNotes(sealed, await getTestPrivateKey()); + + expect(opened.map((note) => note.note)).toEqual([ + "first", + "second", + "third", + ]); + }); + + test("opens nothing when given nothing", async () => { + expect(await openNotes([], await getTestPrivateKey())).toEqual([]); + }); + }); +}); From 8063d91be7eeb3d47a8c9941ccc5350c593311e8 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 20:03:48 +0000 Subject: [PATCH 07/17] Answer the review: read the primary, refuse an empty ask, fix the plans The probe that decides which steps are still to do now reads the primary: a replica can lag behind columns an earlier attempt added, and adding one twice is an error. Asking for the notes of no records threw away its own SQL and was reached only by its test. It now says so loudly, and the one live empty path stays where it saves a round trip. The importer plan told a future reader to repoint notes by a column this change removes. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- TODO.md | 12 ++++++---- docs/importer-plan.md | 10 ++++---- .../db/migrations/2026-07-28_note_entities.ts | 12 ++++++++-- src/shared/db/notes/queries.ts | 2 ++ src/shared/db/notes/target.ts | 23 +++++++++++-------- test/shared/db/notes/target.test.ts | 10 ++++---- 6 files changed, 46 insertions(+), 23 deletions(-) diff --git a/TODO.md b/TODO.md index 6d4ba2916b..f82117eff5 100644 --- a/TODO.md +++ b/TODO.md @@ -257,10 +257,14 @@ re-rendering. Migration is deliberately gradual and hardest-first. - **Slice 4 — the long tail.** Questions and `history/:hmac` remain. Groups, holidays, users, built sites, and attendee statuses are complete. -- **Slice 5 — generalize `system_notes`** from attendee-only to - `(entity_type, entity_id)` so any entity page can carry a notes section. The - notes DB module currently has no `entity_type` column; this is a small - migration + query change that unblocks notes tabs on the other entities. +- **Slice 5 — generalize `system_notes`: done.** A note now names the kind of + record it is about and which one (`entity_type`, `entity_id`), and the notes + module works in those terms (`src/shared/db/notes/`). What remains is per-page + work: add `"listing"` (or whichever record is next) to `NOTE_ENTITIES` in + `notes/target.ts`, give that record's delete path the notes delete statement, + and add the notes section to its entity page. The list drives the database + `CHECK`, so adding a kind ships with a small rebuild migration — as + `2026-06-20_free_text_questions` did for `questions.display_type`. --- diff --git a/docs/importer-plan.md b/docs/importer-plan.md index 44b08b2382..d0c7352d40 100644 --- a/docs/importer-plan.md +++ b/docs/importer-plan.md @@ -1429,10 +1429,12 @@ Implementation notes: raw-deletes the source attendee and never touches `system_notes`, so the imported audit trail (stored as a `system_notes` owner note) would be left orphaned on the deleted source id. The merge must **repoint the source's - `system_notes` rows to the target** (`UPDATE system_notes SET attendee_id = - targetId WHERE attendee_id = sourceId`, in the same transaction — the - `booking_imports` remap's sibling), with a test that a merged imported source's - audit note survives on the target. + `system_notes` rows to the target** (`UPDATE system_notes SET entity_id = + targetId WHERE entity_type = 'attendee' AND entity_id = sourceId`, in the same + transaction — the `booking_imports` remap's sibling), with a test that a merged + imported source's audit note survives on the target. A note names the kind of + record it is about and which one, so both parts are matched; see + `src/shared/db/notes/target.ts`. - The `attendee_answers` XOR/validation triggers will `ABORT` a malformed answer row (e.g. both `answer_id` and `string_id` set). The importer only ever writes the text-answer shape (`answer_id` NULL, `question_id` + `string_id` set), so a diff --git a/src/shared/db/migrations/2026-07-28_note_entities.ts b/src/shared/db/migrations/2026-07-28_note_entities.ts index 70dee45bdb..373ad5147c 100644 --- a/src/shared/db/migrations/2026-07-28_note_entities.ts +++ b/src/shared/db/migrations/2026-07-28_note_entities.ts @@ -1,3 +1,4 @@ +import { queryBatchPrimary } from "#shared/db/client.ts"; import { bareSchemaMigration } from "./define.ts"; /** @@ -9,13 +10,20 @@ import { bareSchemaMigration } from "./define.ts"; * takes the columns' final form from SCHEMA, which has no default. Each step is * skipped when it has already been done, so a re-run after a failed verify is a * cheap no-op. + * + * Which steps are still to do is read from the primary: a replica can lag + * behind the columns an earlier attempt added, and adding one twice is an + * error. The rebuild reads the live schema from the primary for the same + * reason, so it always copies the columns just added. */ export default bareSchemaMigration( "2026-07-28_note_entities", "Replace system_notes.attendee_id with entity_type + entity_id, so notes can be about any record and not only an attendee. Existing notes become notes about an attendee", async ({ getDb, recreateTable, syncIndexes }) => { - const info = await getDb().execute("PRAGMA table_info(system_notes)"); - const columns = new Set(info.rows.map((row) => String(row.name))); + const [info] = await queryBatchPrimary([ + { args: [], sql: "SELECT name FROM pragma_table_info('system_notes')" }, + ]); + const columns = new Set(info!.rows.map((row) => String(row.name))); if (!columns.has("attendee_id")) return; if (!columns.has("entity_type")) { diff --git a/src/shared/db/notes/queries.ts b/src/shared/db/notes/queries.ts index 07cc8e501c..840e179ec0 100644 --- a/src/shared/db/notes/queries.ts +++ b/src/shared/db/notes/queries.ts @@ -67,6 +67,8 @@ export const getNoteRows = ( entity: NoteEntity, ids: number[], ): Promise => + // Nothing to ask about is answered here rather than by the database: an empty + // list would otherwise cost a round trip to be told what we already know. ids.length === 0 ? Promise.resolve([]) : noteRowsWhere(targetsWhere(entity, ids)); diff --git a/src/shared/db/notes/target.ts b/src/shared/db/notes/target.ts index 9c6d18be66..7944707514 100644 --- a/src/shared/db/notes/target.ts +++ b/src/shared/db/notes/target.ts @@ -45,18 +45,23 @@ export const targetWhere = ({ entity, id }: NoteTarget): SqlStatement => ({ sql: "entity_type = ? AND entity_id = ?", }); -/** Ask for several records of one kind at once. An empty list asks for nothing, - * which is what `0 = 1` says: no row can answer it. */ +/** + * Ask for several records of one kind at once. There must be at least one: a + * caller with nothing to ask about has no question for the database, and asking + * anyway would build SQL no database accepts. + */ export const targetsWhere = ( entity: NoteEntity, ids: number[], -): SqlStatement => - ids.length === 0 - ? { args: [], sql: "0 = 1" } - : { - args: [entity, ...ids], - sql: `entity_type = ? AND entity_id IN (${inPlaceholders(ids)})`, - }; +): SqlStatement => { + if (ids.length === 0) { + throw new Error(`Asked for the notes of no ${entity} records`); + } + return { + args: [entity, ...ids], + sql: `entity_type = ? AND entity_id IN (${inPlaceholders(ids)})`, + }; +}; /** Ask for the notes of one kind of record, chosen by a subquery of ids. */ export const targetsSelectedBy = ( diff --git a/test/shared/db/notes/target.test.ts b/test/shared/db/notes/target.test.ts index 27b41f097f..b8b793e6a5 100644 --- a/test/shared/db/notes/target.test.ts +++ b/test/shared/db/notes/target.test.ts @@ -34,10 +34,12 @@ describe("what a note is about", () => { }); }); - test("asks for nothing when given no ids", () => { - // `0 = 1` is false for every row, so an empty list selects no notes rather - // than every note of that kind. - expect(targetsWhere("attendee", [])).toEqual({ args: [], sql: "0 = 1" }); + test("refuses to ask about no records at all", () => { + // Building `IN ()` would be SQL no database accepts, and a caller with + // nothing to ask about should not have reached here. + expect(() => targetsWhere("attendee", [])).toThrow( + "Asked for the notes of no attendee records", + ); }); test("keeps the subquery's own arguments after the kind", () => { From eec0f5b8f0076b79666635dd56b4ce329993e13c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 20:26:40 +0000 Subject: [PATCH 08/17] Answer the second review: cover the walk fix, read one the same way as many The glob expansion moved into its own module so the missing-directory fix has a direct test; it fails against the old lazy-readDir shape with the same NotFound the gate hit. One record's notes are now asked for as a list of one, so filtering and ordering cannot drift from the list reads. Comments describe the design as it is rather than what it replaced. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- scripts/mutation.ts | 73 +------------------------- scripts/mutation/expand.ts | 77 ++++++++++++++++++++++++++++ src/shared/db/notes/queries.ts | 5 +- src/shared/db/notes/target.ts | 10 ++-- test/scripts/mutation/expand.test.ts | 74 ++++++++++++++++++++++++++ 5 files changed, 160 insertions(+), 79 deletions(-) create mode 100644 scripts/mutation/expand.ts create mode 100644 test/scripts/mutation/expand.test.ts diff --git a/scripts/mutation.ts b/scripts/mutation.ts index c31107fa92..9845a6d45a 100644 --- a/scripts/mutation.ts +++ b/scripts/mutation.ts @@ -17,8 +17,8 @@ * Usage: deno task mutation [options] */ -import { globToRegExp, join, normalize, SEPARATOR } from "@std/path"; import { DEFAULT_TIMEOUT, parseArgs } from "./mutation/args.ts"; +import { expand } from "./mutation/expand.ts"; import { runIsolatedMutationCommand } from "./mutation/isolation.ts"; import { isSnapshotChild, @@ -45,77 +45,6 @@ Examples: deno task mutation src/shared/dates.ts test/shared/dates.test.ts deno task mutation 'src/shared/forms/definition.ts' 'test/shared/forms/definition/*.test.ts' --exhaustive`; -/** Glob metacharacters; a path segment with none is a fixed directory name. */ -const GLOB_CHARS = /[*?{}[\]]/; - -/** - * What `dir` holds, or nothing when there is no such directory — a path that - * has since moved or gone is asked for and simply answers with nothing. Reading - * the entries is what reaches the disk, so a missing directory shows up here - * rather than where the reader was created. - */ -const dirEntriesOrNone = async (dir: string): Promise => { - const entries: Deno.DirEntry[] = []; - try { - for await (const entry of Deno.readDir(dir)) entries.push(entry); - } catch (error) { - const gone = - error instanceof Deno.errors.NotFound || - error instanceof Deno.errors.NotADirectory; - if (!gone) throw error; - return []; - } - return entries; -}; - -/** Every file under `dir`, recursively; a missing directory yields nothing. */ -async function* walkFiles(dir: string): AsyncGenerator { - for (const entry of await dirEntriesOrNone(dir)) { - const path = join(dir, entry.name); - if (entry.isDirectory) yield* walkFiles(path); - else if (entry.isFile) yield path; - } -} - -/** The leading, glob-free directory of `glob` — where a walk can start without - * scanning the whole tree. An exact path (no metacharacters) returns itself. */ -const staticBase = (glob: string): string => { - const fixed: string[] = []; - for (const segment of normalize(glob).split(SEPARATOR)) { - if (GLOB_CHARS.test(segment)) break; - fixed.push(segment); - } - return fixed.length > 0 ? fixed.join(SEPARATOR) : "."; -}; - -/** - * Expand source/test globs to absolute file paths. Replaces `@std/fs`'s - * `expandGlob` (not in this project's lock, so unfetchable in a sandboxed run) - * with `@std/path`'s `globToRegExp` over a `Deno.readDir` walk — same contract: - * absolute paths to existing files, sorted and de-duplicated. - */ -const expand = async (globs: string[]): Promise => { - const cwd = Deno.cwd(); - const paths = new Set(); - for (const glob of globs) { - const absGlob = join(cwd, glob); - const pattern = globToRegExp(absGlob, { extended: true, globstar: true }); - const base = staticBase(absGlob); - try { - if ((await Deno.stat(base)).isFile) { - if (pattern.test(base)) paths.add(base); - continue; - } - } catch { - // base doesn't exist; the walk below simply yields nothing - } - for await (const path of walkFiles(base)) { - if (pattern.test(path)) paths.add(path); - } - } - return [...paths].sort(); -}; - const main = async (): Promise => { const args = parseArgs(Deno.args); if (args.error !== null) { diff --git a/scripts/mutation/expand.ts b/scripts/mutation/expand.ts new file mode 100644 index 0000000000..4370365c90 --- /dev/null +++ b/scripts/mutation/expand.ts @@ -0,0 +1,77 @@ +/** + * Turning source/test globs into the files they name. + * + * Replaces `@std/fs`'s `expandGlob` (not in this project's lock, so unfetchable + * in a sandboxed run) with `@std/path`'s `globToRegExp` over a `Deno.readDir` + * walk — same contract: absolute paths to existing files, sorted and without + * repeats. + */ + +import { globToRegExp, join, normalize, SEPARATOR } from "@std/path"; + +/** Glob metacharacters; a path segment with none is a fixed directory name. */ +const GLOB_CHARS = /[*?{}[\]]/; + +/** + * What `dir` holds, or nothing when there is no such directory — a path that + * has since moved or gone is asked for and simply answers with nothing. Reading + * the entries is what reaches the disk, so a missing directory shows up here + * rather than where the reader was created. + */ +const dirEntriesOrNone = async (dir: string): Promise => { + const entries: Deno.DirEntry[] = []; + try { + for await (const entry of Deno.readDir(dir)) entries.push(entry); + } catch (error) { + const gone = + error instanceof Deno.errors.NotFound || + error instanceof Deno.errors.NotADirectory; + if (!gone) throw error; + return []; + } + return entries; +}; + +/** Every file under `dir`, recursively; a missing directory yields nothing. */ +async function* walkFiles(dir: string): AsyncGenerator { + for (const entry of await dirEntriesOrNone(dir)) { + const path = join(dir, entry.name); + if (entry.isDirectory) yield* walkFiles(path); + else if (entry.isFile) yield path; + } +} + +/** The leading, glob-free directory of `glob` — where a walk can start without + * scanning the whole tree. An exact path (no metacharacters) returns itself. */ +const staticBase = (glob: string): string => { + const fixed: string[] = []; + for (const segment of normalize(glob).split(SEPARATOR)) { + if (GLOB_CHARS.test(segment)) break; + fixed.push(segment); + } + return fixed.length > 0 ? fixed.join(SEPARATOR) : "."; +}; + +/** The files these globs name, sorted and without repeats. A glob whose + * directory is not there names no files rather than failing. */ +export const expand = async (globs: string[]): Promise => { + const cwd = Deno.cwd(); + const paths = new Set(); + for (const glob of globs) { + const absGlob = join(cwd, glob); + const pattern = globToRegExp(absGlob, { extended: true, globstar: true }); + const base = staticBase(absGlob); + try { + if ((await Deno.stat(base)).isFile) { + if (pattern.test(base)) paths.add(base); + continue; + } + } catch { + // base doesn't exist; the walk below simply yields nothing + } + for await (const path of walkFiles(base)) { + if (pattern.test(path)) paths.add(path); + } + } + return [...paths].sort(); +}; diff --git a/src/shared/db/notes/queries.ts b/src/shared/db/notes/queries.ts index 840e179ec0..1e3fc93da8 100644 --- a/src/shared/db/notes/queries.ts +++ b/src/shared/db/notes/queries.ts @@ -92,12 +92,13 @@ export const getNoteRowsForListing = ( }), ); -/** All of one record's notes, opened up, oldest first. */ +/** All of one record's notes, opened up, oldest first. One record is a list of + * one, so it is asked for the same way several are. */ export const getNotesFor = async ( target: NoteTarget, privateKey: CryptoKey, ): Promise => - openNotes(await noteRowsWhere(targetWhere(target)), privateKey); + openNotes(await getNoteRows(target.entity, [target.id]), privateKey); /** A "load the notes for " reader: takes the selector value and a way * to get the owner private key, and answers with opened-up notes. */ diff --git a/src/shared/db/notes/target.ts b/src/shared/db/notes/target.ts index 7944707514..952a59a545 100644 --- a/src/shared/db/notes/target.ts +++ b/src/shared/db/notes/target.ts @@ -1,10 +1,10 @@ /** * What a note is about. * - * A note used to belong to an attendee and nothing else. It now names the kind - * of thing it is about and which one, so any record the operator can open can - * carry notes. This module is pure: it says what a target is and how to ask for - * one in SQL, and never touches the database itself. + * A note names the kind of record it is about and which one of that kind, so + * any record the operator can open can carry notes. This module is pure: it + * says what a target is and how to ask for one in SQL, and never touches the + * database itself. */ import * as v from "valibot"; @@ -30,7 +30,7 @@ const notesAbout = (entity: NoteEntity) => (id: number): NoteTarget => ({ entity, id }); -/** The record kind notes started with, and still the only one that has them. */ +/** Notes about one attendee. */ export const attendeeNotes = notesAbout("attendee"); /** Group notes by which record they are about — for a batch about one kind of diff --git a/test/scripts/mutation/expand.test.ts b/test/scripts/mutation/expand.test.ts new file mode 100644 index 0000000000..16fe87b688 --- /dev/null +++ b/test/scripts/mutation/expand.test.ts @@ -0,0 +1,74 @@ +import { join, relative } from "node:path"; +import { expect } from "@std/expect"; +import { describe, it as test } from "@std/testing/bdd"; +import { expand } from "#scripts/mutation/expand.ts"; +import { withTempDir } from "#test-utils/files.ts"; + +/** Globs are matched against the working directory, so a test that names files + * in a temporary folder has to ask from there. */ +const expandFrom = async (dir: string, globs: string[]): Promise => { + const cwd = Deno.cwd(); + Deno.chdir(dir); + try { + return await expand(globs); + } finally { + Deno.chdir(cwd); + } +}; + +const writeFile = async ( + path: string, + body = "export {};\n", +): Promise => { + await Deno.mkdir(join(path, ".."), { recursive: true }); + await Deno.writeTextFile(path, body); +}; + +describe("the files a glob names", () => { + test("finds every match under a folder, sorted and without repeats", async () => { + await withTempDir(async (dir) => { + await writeFile(join(dir, "src", "b.ts")); + await writeFile(join(dir, "src", "a.ts")); + await writeFile(join(dir, "src", "deep", "c.ts")); + await writeFile(join(dir, "src", "notes.txt")); + + const found = await expandFrom(dir, ["src/**/*.ts", "src/a.ts"]); + + expect(found.map((path) => relative(dir, path))).toEqual([ + join("src", "a.ts"), + join("src", "b.ts"), + join("src", "deep", "c.ts"), + ]); + }); + }); + + test("names no files when the folder is not there", async () => { + await withTempDir(async (dir) => { + // A test file that moved is still named by a branch's committed diff, so + // this is asked for in a normal run. Reading a missing folder only fails + // once its entries are read, so a guard around opening it misses this. + const found = await expandFrom(dir, ["gone/**/*.test.ts"]); + + expect(found).toEqual([]); + }); + }); + + test("names no files when the path is a file, not a folder", async () => { + await withTempDir(async (dir) => { + await writeFile(join(dir, "a.ts")); + + expect(await expandFrom(dir, ["a.ts/*.ts"])).toEqual([]); + }); + }); + + test("names an exact file, and skips one that is not there", async () => { + await withTempDir(async (dir) => { + await writeFile(join(dir, "src", "only.ts")); + + expect(await expandFrom(dir, ["src/only.ts"])).toEqual([ + join(dir, "src", "only.ts"), + ]); + expect(await expandFrom(dir, ["src/missing.ts"])).toEqual([]); + }); + }); +}); From cb1c0dd9d0ef743359832b4a31377cea82968740 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 20:39:05 +0000 Subject: [PATCH 09/17] Ask whether it is a directory before reading it The catch that classified the error had arms no test could take. Asking statOrNull first says the same thing in one step, and the base a glob walks from is always absolute, so its empty-path arm went too. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- scripts/mutation/expand.ts | 31 ++++++++++++++-------------- test/scripts/mutation/expand.test.ts | 11 ++++++++++ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/scripts/mutation/expand.ts b/scripts/mutation/expand.ts index 4370365c90..e73364e7c9 100644 --- a/scripts/mutation/expand.ts +++ b/scripts/mutation/expand.ts @@ -8,27 +8,22 @@ */ import { globToRegExp, join, normalize, SEPARATOR } from "@std/path"; +import { statOrNull } from "#scripts/not-found.ts"; /** Glob metacharacters; a path segment with none is a fixed directory name. */ const GLOB_CHARS = /[*?{}[\]]/; /** - * What `dir` holds, or nothing when there is no such directory — a path that - * has since moved or gone is asked for and simply answers with nothing. Reading - * the entries is what reaches the disk, so a missing directory shows up here - * rather than where the reader was created. + * What `dir` holds, or nothing when it is not a directory at all — a path that + * has moved or gone is asked for in an ordinary run, and answers with nothing. + * Whether it is there is asked before reading, because reading is what reaches + * the disk: a check around opening the reader would never see the answer. */ const dirEntriesOrNone = async (dir: string): Promise => { + const info = await statOrNull(dir); + if (info === null || !info.isDirectory) return []; const entries: Deno.DirEntry[] = []; - try { - for await (const entry of Deno.readDir(dir)) entries.push(entry); - } catch (error) { - const gone = - error instanceof Deno.errors.NotFound || - error instanceof Deno.errors.NotADirectory; - if (!gone) throw error; - return []; - } + for await (const entry of Deno.readDir(dir)) entries.push(entry); return entries; }; @@ -41,15 +36,19 @@ async function* walkFiles(dir: string): AsyncGenerator { } } -/** The leading, glob-free directory of `glob` — where a walk can start without - * scanning the whole tree. An exact path (no metacharacters) returns itself. */ +/** + * The leading, glob-free directory of `glob` — where a walk can start without + * scanning the whole tree. An exact path (no metacharacters) returns itself. + * Every glob reaching here is an absolute path, so there is always at least the + * leading separator to start from. + */ const staticBase = (glob: string): string => { const fixed: string[] = []; for (const segment of normalize(glob).split(SEPARATOR)) { if (GLOB_CHARS.test(segment)) break; fixed.push(segment); } - return fixed.length > 0 ? fixed.join(SEPARATOR) : "."; + return fixed.join(SEPARATOR); }; /** The files these globs name, sorted and without repeats. A glob whose diff --git a/test/scripts/mutation/expand.test.ts b/test/scripts/mutation/expand.test.ts index 16fe87b688..9eda537016 100644 --- a/test/scripts/mutation/expand.test.ts +++ b/test/scripts/mutation/expand.test.ts @@ -61,6 +61,17 @@ describe("the files a glob names", () => { }); }); + test("matches from where it is asked when the glob starts with a star", async () => { + await withTempDir(async (dir) => { + await writeFile(join(dir, "top.ts")); + await writeFile(join(dir, "nested", "deep.ts")); + + const found = await expandFrom(dir, ["*.ts"]); + + expect(found.map((path) => relative(dir, path))).toEqual(["top.ts"]); + }); + }); + test("names an exact file, and skips one that is not there", async () => { await withTempDir(async (dir) => { await writeFile(join(dir, "src", "only.ts")); From bb7beb2b1a541a3d7bc3156ac1abfa07b2fe0736 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 20:41:08 +0000 Subject: [PATCH 10/17] Read a folder's contents one way across the scripts The copy that makes a mutation run's snapshot collected directory entries the same way the glob walk did. Both now ask the shared walk helper, which also gained the missing-folder answer, and the glob walk stops carrying its own copy of a recursive walk that already existed. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- scripts/mutation/expand.ts | 25 +------------------------ scripts/mutation/isolation-state.ts | 4 ++-- scripts/walk-files.ts | 23 +++++++++++++++++++++-- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/scripts/mutation/expand.ts b/scripts/mutation/expand.ts index e73364e7c9..b1dbcbb4b3 100644 --- a/scripts/mutation/expand.ts +++ b/scripts/mutation/expand.ts @@ -8,34 +8,11 @@ */ import { globToRegExp, join, normalize, SEPARATOR } from "@std/path"; -import { statOrNull } from "#scripts/not-found.ts"; +import { walkFiles } from "#scripts/walk-files.ts"; /** Glob metacharacters; a path segment with none is a fixed directory name. */ const GLOB_CHARS = /[*?{}[\]]/; -/** - * What `dir` holds, or nothing when it is not a directory at all — a path that - * has moved or gone is asked for in an ordinary run, and answers with nothing. - * Whether it is there is asked before reading, because reading is what reaches - * the disk: a check around opening the reader would never see the answer. - */ -const dirEntriesOrNone = async (dir: string): Promise => { - const info = await statOrNull(dir); - if (info === null || !info.isDirectory) return []; - const entries: Deno.DirEntry[] = []; - for await (const entry of Deno.readDir(dir)) entries.push(entry); - return entries; -}; - -/** Every file under `dir`, recursively; a missing directory yields nothing. */ -async function* walkFiles(dir: string): AsyncGenerator { - for (const entry of await dirEntriesOrNone(dir)) { - const path = join(dir, entry.name); - if (entry.isDirectory) yield* walkFiles(path); - else if (entry.isFile) yield path; - } -} - /** * The leading, glob-free directory of `glob` — where a walk can start without * scanning the whole tree. An exact path (no metacharacters) returns itself. diff --git a/scripts/mutation/isolation-state.ts b/scripts/mutation/isolation-state.ts index c28fc897d6..405e4cd613 100644 --- a/scripts/mutation/isolation-state.ts +++ b/scripts/mutation/isolation-state.ts @@ -8,6 +8,7 @@ import { isAbsolute, join, relative, resolve, SEPARATOR } from "@std/path"; import * as v from "valibot"; import { projectRoot } from "#scripts/project-root.ts"; +import { directoryEntries } from "#scripts/walk-files.ts"; export const MUTATION_RUNS_DIR = ".mutation-runs"; export const MUTATION_WORK_DIR = "work"; @@ -139,8 +140,7 @@ const copyDirectory = async ( const toDir = join(toRoot, relativePath); await Deno.mkdir(toDir, { recursive: true }); - const entries: Deno.DirEntry[] = []; - for await (const entry of Deno.readDir(fromDir)) entries.push(entry); + const entries = await directoryEntries(fromDir); for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name), diff --git a/scripts/walk-files.ts b/scripts/walk-files.ts index e8af935e03..a9e968fcd5 100644 --- a/scripts/walk-files.ts +++ b/scripts/walk-files.ts @@ -1,7 +1,26 @@ +import { join } from "@std/path"; +import { statOrNull } from "./not-found.ts"; + +/** + * What `directory` holds, or nothing when it is not a directory at all — a path + * that has moved or gone answers with nothing rather than failing. Whether it + * is there is asked before reading, because reading is what reaches the disk: a + * check around opening the reader would never see the answer. + */ +export const directoryEntries = async ( + directory: string, +): Promise => { + const info = await statOrNull(directory); + if (info === null || !info.isDirectory) return []; + const entries: Deno.DirEntry[] = []; + for await (const entry of Deno.readDir(directory)) entries.push(entry); + return entries; +}; + /** Recursively yield every file path beneath `directory` (depth-first). */ export async function* walkFiles(directory: string): AsyncGenerator { - for await (const entry of Deno.readDir(directory)) { - const path = `${directory}/${entry.name}`; + for (const entry of await directoryEntries(directory)) { + const path = join(directory, entry.name); if (entry.isDirectory) { yield* walkFiles(path); continue; From 4f459d62b935ff57b7f8d506375e863e39ac4cde Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 21:02:23 +0000 Subject: [PATCH 11/17] Answer the third review: strict walker, resumable index, batched note deletes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The shared walk is strict again — a caller naming a root that is not there is asking about something it believes in, and a snapshot copy that answered nothing would report success on an empty copy. Only the glob expansion tolerates a path that has moved, which is the case it is for. A run that died right after the table rebuild left no index behind, and the next run returned early without making one. It now asks for the indexes on that path too. Adding a column is taken as done when the column is already there: a primary read can still briefly answer with the schema as it was. Stale notes go in one batch, and the version label names this change. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- scripts/mutation/expand.ts | 48 ++++++++++------ scripts/walk-files.ts | 10 +--- src/features/admin/attendee-notes.ts | 4 +- src/features/admin/attendees-edit.ts | 15 +++-- .../db/migrations/2026-07-28_note_entities.ts | 55 +++++++++++++------ src/shared/db/migrations/schema/version.ts | 3 +- src/shared/db/notes/queries.ts | 23 +++++--- src/shared/db/notes/sealing.ts | 2 +- test/scripts/mutation/expand.test.ts | 15 ++--- .../2026-07-28_note_entities.test.ts | 13 +++++ .../migrations/schema/version/guard.test.ts | 2 +- test/shared/db/notes/queries.test.ts | 17 +++++- 12 files changed, 132 insertions(+), 75 deletions(-) diff --git a/scripts/mutation/expand.ts b/scripts/mutation/expand.ts index b1dbcbb4b3..d1683a9010 100644 --- a/scripts/mutation/expand.ts +++ b/scripts/mutation/expand.ts @@ -8,7 +8,8 @@ */ import { globToRegExp, join, normalize, SEPARATOR } from "@std/path"; -import { walkFiles } from "#scripts/walk-files.ts"; +import { statOrNull } from "#scripts/not-found.ts"; +import { collectFiles } from "#scripts/walk-files.ts"; /** Glob metacharacters; a path segment with none is a fixed directory name. */ const GLOB_CHARS = /[*?{}[\]]/; @@ -28,25 +29,38 @@ const staticBase = (glob: string): string => { return fixed.join(SEPARATOR); }; -/** The files these globs name, sorted and without repeats. A glob whose - * directory is not there names no files rather than failing. */ -export const expand = async (globs: string[]): Promise => { - const cwd = Deno.cwd(); +/** Every file a glob names below `basePath` — or the path itself, when the + * glob named an exact file rather than somewhere to look. */ +const matchesUnder = ( + base: Deno.FileInfo, + basePath: string, + matches: (path: string) => boolean, +): Promise => + base.isDirectory + ? collectFiles(basePath, matches) + : Promise.resolve(matches(basePath) ? [basePath] : []); + +/** + * The files these globs name, sorted and without repeats. A glob whose + * directory is not there names no files rather than failing: the changed set a + * run is given is a branch's committed diff, which still names test files that + * have since moved. + */ +export const expand = async ( + globs: string[], + from: string = Deno.cwd(), +): Promise => { const paths = new Set(); for (const glob of globs) { - const absGlob = join(cwd, glob); + const absGlob = join(from, glob); const pattern = globToRegExp(absGlob, { extended: true, globstar: true }); - const base = staticBase(absGlob); - try { - if ((await Deno.stat(base)).isFile) { - if (pattern.test(base)) paths.add(base); - continue; - } - } catch { - // base doesn't exist; the walk below simply yields nothing - } - for await (const path of walkFiles(base)) { - if (pattern.test(path)) paths.add(path); + const basePath = staticBase(absGlob); + const base = await statOrNull(basePath); + if (base === null) continue; + for (const path of await matchesUnder(base, basePath, (candidate) => + pattern.test(candidate), + )) { + paths.add(path); } } return [...paths].sort(); diff --git a/scripts/walk-files.ts b/scripts/walk-files.ts index a9e968fcd5..717b5d1f41 100644 --- a/scripts/walk-files.ts +++ b/scripts/walk-files.ts @@ -1,17 +1,13 @@ import { join } from "@std/path"; -import { statOrNull } from "./not-found.ts"; /** - * What `directory` holds, or nothing when it is not a directory at all — a path - * that has moved or gone answers with nothing rather than failing. Whether it - * is there is asked before reading, because reading is what reaches the disk: a - * check around opening the reader would never see the answer. + * What `directory` holds, as a list. A directory that is not there fails here, + * loudly: a caller asking to walk somewhere that has gone is asking about a + * root it believes in, and answering "nothing" would read as "nothing to do". */ export const directoryEntries = async ( directory: string, ): Promise => { - const info = await statOrNull(directory); - if (info === null || !info.isDirectory) return []; const entries: Deno.DirEntry[] = []; for await (const entry of Deno.readDir(directory)) entries.push(entry); return entries; diff --git a/src/features/admin/attendee-notes.ts b/src/features/admin/attendee-notes.ts index 28cf0ddb21..a0015c41c8 100644 --- a/src/features/admin/attendee-notes.ts +++ b/src/features/admin/attendee-notes.ts @@ -26,7 +26,7 @@ import { getSearchParam } from "#routes/url.ts"; import { getAttendeeOrNull } from "#shared/db/attendees/queries.ts"; import { createOwnerNote, - deleteNote, + deleteNotes, getNote, } from "#shared/db/notes/queries.ts"; import { attendeeNotes } from "#shared/db/notes/target.ts"; @@ -163,7 +163,7 @@ const handleDeleteNoteGet: TypedRouteHandler<"GET /admin/attendee/:attendeeId/no /** POST /admin/attendee/:attendeeId/note/:noteId/delete — delete the note. */ const handleDeleteNotePost: TypedRouteHandler<"POST /admin/attendee/:attendeeId/note/:noteId/delete"> = noteHandlers.post(async (note, _session, form, _request, { attendeeId }) => { - await deleteNote(attendeeNotes(attendeeId), note.id); + await deleteNotes(attendeeNotes(attendeeId), [note.id]); return redirect( returnTarget(attendeeId, form.getString("return_url")), t("notes.deleted"), diff --git a/src/features/admin/attendees-edit.ts b/src/features/admin/attendees-edit.ts index 01b4dc7e09..d5b06a948e 100644 --- a/src/features/admin/attendees-edit.ts +++ b/src/features/admin/attendees-edit.ts @@ -24,7 +24,7 @@ import { getAttendeeRaw } from "#shared/db/attendees/queries.ts"; import { queryOne } from "#shared/db/client.ts"; import { createSystemNote, - deleteNote, + deleteNotes, getNotesFor, } from "#shared/db/notes/queries.ts"; import { attendeeNotes } from "#shared/db/notes/target.ts"; @@ -169,11 +169,14 @@ const cleanupStaleManualRefundNote = async ( privateKey: CryptoKey, ): Promise => { const notes = await getNotesFor(attendeeNotes(attendeeId), privateKey); - for (const note of notes) { - if (note.type === "system" && note.note.includes("could NOT be refunded")) { - await deleteNote(attendeeNotes(attendeeId), note.id); - } - } + const stale = notes.filter( + (note) => + note.type === "system" && note.note.includes("could NOT be refunded"), + ); + await deleteNotes( + attendeeNotes(attendeeId), + stale.map((note) => note.id), + ); }; /** Load the attendee, listing, and payment references for a refresh. Returns diff --git a/src/shared/db/migrations/2026-07-28_note_entities.ts b/src/shared/db/migrations/2026-07-28_note_entities.ts index 373ad5147c..205add0602 100644 --- a/src/shared/db/migrations/2026-07-28_note_entities.ts +++ b/src/shared/db/migrations/2026-07-28_note_entities.ts @@ -1,20 +1,42 @@ import { queryBatchPrimary } from "#shared/db/client.ts"; +import { errorMessage } from "#shared/error-message.ts"; import { bareSchemaMigration } from "./define.ts"; +/** A column already there is the state this asks for, so the work is done. */ +const ALREADY_ADDED = "duplicate column name"; + +/** + * Add a column, treating one that is already there as added. A run that added + * it and then died leaves the database ahead of what a read reports — even a + * read from the primary, which can briefly answer with the schema as it was + * (see the backoff note in ./runner.ts) — so the database's own answer is the + * one to trust. + */ +const addColumn = async ( + getDb: () => { execute(sql: string): Promise }, + column: string, +): Promise => { + try { + await getDb().execute(`ALTER TABLE system_notes ADD COLUMN ${column}`); + } catch (error) { + if (!errorMessage(error).includes(ALREADY_ADDED)) throw error; + } +}; + /** * Notes used to belong to an attendee and nothing else. They now name the kind * of record they are about and which one, so any record can carry notes. * * The two new columns are added with a default so the database accepts them on * a table that already has rows; the rebuild at the end drops `attendee_id` and - * takes the columns' final form from SCHEMA, which has no default. Each step is - * skipped when it has already been done, so a re-run after a failed verify is a - * cheap no-op. + * takes the columns' final form from SCHEMA, which has no default. * - * Which steps are still to do is read from the primary: a replica can lag - * behind the columns an earlier attempt added, and adding one twice is an - * error. The rebuild reads the live schema from the primary for the same - * reason, so it always copies the columns just added. + * Every step can be done again: adding a column that is there is taken as done, + * and the rebuild and the index sync each describe an end state rather than a + * change. So a run that stopped anywhere — a failed verify, a request that died + * — is picked up by the next one. The only early exit is "there is no + * `attendee_id` left to move", which is the state *after* this work; a stale + * read can only report the state before it, so it cannot end the run early. */ export default bareSchemaMigration( "2026-07-28_note_entities", @@ -24,18 +46,15 @@ export default bareSchemaMigration( { args: [], sql: "SELECT name FROM pragma_table_info('system_notes')" }, ]); const columns = new Set(info!.rows.map((row) => String(row.name))); - if (!columns.has("attendee_id")) return; - - if (!columns.has("entity_type")) { - await getDb().execute( - "ALTER TABLE system_notes ADD COLUMN entity_type TEXT NOT NULL DEFAULT 'attendee'", - ); - } - if (!columns.has("entity_id")) { - await getDb().execute( - "ALTER TABLE system_notes ADD COLUMN entity_id INTEGER NOT NULL DEFAULT 0", - ); + if (!columns.has("attendee_id")) { + // The columns are already moved, but a run that died right after the + // rebuild never reached the index. Asking for it again is cheap. + await syncIndexes(); + return; } + + await addColumn(getDb, "entity_type TEXT NOT NULL DEFAULT 'attendee'"); + await addColumn(getDb, "entity_id INTEGER NOT NULL DEFAULT 0"); await getDb().execute( "UPDATE system_notes SET entity_type = 'attendee', entity_id = attendee_id", ); diff --git a/src/shared/db/migrations/schema/version.ts b/src/shared/db/migrations/schema/version.ts index 59627ffeea..0dbf8a5ead 100644 --- a/src/shared/db/migrations/schema/version.ts +++ b/src/shared/db/migrations/schema/version.ts @@ -1,6 +1,7 @@ /** Schema version label and the migrations bookkeeping table name. */ -export const LATEST_UPDATE = "Keep completed maintenance work dormant."; +export const LATEST_UPDATE = + "Let a note be about any record, not only an attendee."; export const SCHEMA_MIGRATIONS_TABLE = "schema_migrations"; export const LATEST_DB_UPDATE_KEY = "latest_db_update"; diff --git a/src/shared/db/notes/queries.ts b/src/shared/db/notes/queries.ts index 1e3fc93da8..5c99a13a0a 100644 --- a/src/shared/db/notes/queries.ts +++ b/src/shared/db/notes/queries.ts @@ -8,6 +8,7 @@ import { ATTENDEE_KIND } from "#shared/db/attendees/kind.ts"; import { execute, + executeBatch, insert, queryAll, type SqlStatement, @@ -151,16 +152,22 @@ export const getNote = async ( return row ? openNote(row, privateKey) : null; }; -/** Delete one note, tied to its record so a stray id can't reach another's. */ -export const deleteNote = ( +/** + * Delete notes, tied to their record so a stray id can't reach another's. They + * go in one batch: several stale notes cost one round trip, not one each. + */ +export const deleteNotes = ( target: NoteTarget, - noteId: number, -): Promise => { + noteIds: number[], +): Promise => { + if (noteIds.length === 0) return Promise.resolve(); const { args, sql } = targetWhere(target); - return execute(`DELETE FROM system_notes WHERE id = ? AND ${sql}`, [ - noteId, - ...args, - ]); + return executeBatch( + noteIds.map((noteId) => ({ + args: [noteId, ...args], + sql: `DELETE FROM system_notes WHERE id = ? AND ${sql}`, + })), + ); }; /** Delete the notes of records chosen by a subquery — for a delete path that diff --git a/src/shared/db/notes/sealing.ts b/src/shared/db/notes/sealing.ts index 13c2259835..568a0de577 100644 --- a/src/shared/db/notes/sealing.ts +++ b/src/shared/db/notes/sealing.ts @@ -17,7 +17,7 @@ import type { import { settings } from "#shared/db/settings.ts"; import type { SystemNote, SystemNoteRow, SystemNoteType } from "./types.ts"; -export type SealedNote = OwnerKeyEncrypted | EnvKeyEncrypted; +type SealedNote = OwnerKeyEncrypted | EnvKeyEncrypted; /** How each kind of note is sealed. A new kind is a new entry here, and the * compiler asks for it everywhere the kinds are handled. */ diff --git a/test/scripts/mutation/expand.test.ts b/test/scripts/mutation/expand.test.ts index 9eda537016..884428c370 100644 --- a/test/scripts/mutation/expand.test.ts +++ b/test/scripts/mutation/expand.test.ts @@ -4,17 +4,10 @@ import { describe, it as test } from "@std/testing/bdd"; import { expand } from "#scripts/mutation/expand.ts"; import { withTempDir } from "#test-utils/files.ts"; -/** Globs are matched against the working directory, so a test that names files - * in a temporary folder has to ask from there. */ -const expandFrom = async (dir: string, globs: string[]): Promise => { - const cwd = Deno.cwd(); - Deno.chdir(dir); - try { - return await expand(globs); - } finally { - Deno.chdir(cwd); - } -}; +/** Globs are matched from a starting folder, so a test names its own without + * touching the working directory this whole process shares. */ +const expandFrom = (dir: string, globs: string[]): Promise => + expand(globs, dir); const writeFile = async ( path: string, diff --git a/test/shared/db/migrations/2026-07-28_note_entities.test.ts b/test/shared/db/migrations/2026-07-28_note_entities.test.ts index 46e4b5db21..579a6e6164 100644 --- a/test/shared/db/migrations/2026-07-28_note_entities.test.ts +++ b/test/shared/db/migrations/2026-07-28_note_entities.test.ts @@ -161,6 +161,19 @@ describeWithEnv( ]); }); + test("puts the index back when a run died right after the rebuild", async () => { + await downgradeToAttendeeNotes(); + await runMigration(); + // The state a run that stopped between the rebuild and the index leaves: + // the columns are moved, but nothing indexes them. + await execute("DROP INDEX idx_system_notes_entity"); + expect(await indexExists("idx_system_notes_entity")).toBe(false); + + await runMigration(); + + expect(await indexExists("idx_system_notes_entity")).toBe(true); + }); + test("is registered under the id it declares, and says what it does", () => { const { description, id } = migration(); diff --git a/test/shared/db/migrations/schema/version/guard.test.ts b/test/shared/db/migrations/schema/version/guard.test.ts index d3faf94c97..af50a6f874 100644 --- a/test/shared/db/migrations/schema/version/guard.test.ts +++ b/test/shared/db/migrations/schema/version/guard.test.ts @@ -116,7 +116,7 @@ describe("db > migrations > schema change guard", () => { }).toEqual({ dbSchemaHash: "db_schema_hash", latestDbUpdate: "latest_db_update", - latestUpdate: "Keep completed maintenance work dormant.", + latestUpdate: "Let a note be about any record, not only an attendee.", migrationLock: "migration_lock", schemaMigrations: "schema_migrations", }); diff --git a/test/shared/db/notes/queries.test.ts b/test/shared/db/notes/queries.test.ts index e85ee25394..6b603e7912 100644 --- a/test/shared/db/notes/queries.test.ts +++ b/test/shared/db/notes/queries.test.ts @@ -5,7 +5,7 @@ import { queryOne } from "#shared/db/client.ts"; import { createOwnerNote, createSystemNote, - deleteNote, + deleteNotes, getNote, getNoteRows, getNoteRowsForListing, @@ -215,12 +215,23 @@ describeWithEnv("db > notes", { db: true }, () => { const [otherRow] = await getNoteRows("attendee", [other]); // A wrong attendee id must not delete another attendee's note. - await deleteNote(attendeeNotes(other), ownerRow!.id); + await deleteNotes(attendeeNotes(other), [ownerRow!.id]); expect(await getNoteRows("attendee", [owner])).toHaveLength(1); - await deleteNote(attendeeNotes(owner), ownerRow!.id); + await deleteNotes(attendeeNotes(owner), [ownerRow!.id]); expect(await getNoteRows("attendee", [owner])).toEqual([]); // The other attendee's note is untouched. expect((await getNoteRows("attendee", [other]))[0]?.id).toBe(otherRow!.id); }); + + test("deletes nothing, and asks the database nothing, for an empty list", async () => { + const owner = await makeAttendee("Nothing To Delete"); + await createSystemNote(attendeeNotes(owner), "kept"); + + // The stale-note cleanup runs on every refresh, almost always with nothing + // to remove, so this path must not cost a round trip. + await deleteNotes(attendeeNotes(owner), []); + + expect(await getNoteRows("attendee", [owner])).toHaveLength(1); + }); }); From 7ba74474b488a806e0cf017865d79389847b56d3 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 21:18:37 +0000 Subject: [PATCH 12/17] Say what happens when adding a column fails for another reason A column already there is taken as done; anything else means the database could not do what was asked. Only the tolerated case had a test, so the arm that lets a real failure through was never run. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- .../2026-07-28_note_entities.test.ts | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/shared/db/migrations/2026-07-28_note_entities.test.ts b/test/shared/db/migrations/2026-07-28_note_entities.test.ts index 579a6e6164..b4cfb894ba 100644 --- a/test/shared/db/migrations/2026-07-28_note_entities.test.ts +++ b/test/shared/db/migrations/2026-07-28_note_entities.test.ts @@ -7,7 +7,10 @@ import { recreateTable, syncIndexes, } from "#shared/db/migrations/schema-sync.ts"; -import type { Migration } from "#shared/db/migrations/types.ts"; +import type { + Migration, + MigrationContext, +} from "#shared/db/migrations/types.ts"; import { columnNames } from "#test/test-utils/db/migration-test-helpers.ts"; import { describeWithEnv } from "#test-utils/db.ts"; import { buildMigrationContext, indexExists } from "#test-utils/migrations.ts"; @@ -174,6 +177,25 @@ describeWithEnv( expect(await indexExists("idx_system_notes_entity")).toBe(true); }); + test("stops when adding a column fails for any other reason", async () => { + await downgradeToAttendeeNotes(); + const brokenDatabase = () => ({ + execute: () => Promise.reject(new Error("database or disk is full")), + }); + + // A column that is already there is taken as done; anything else means + // the database could not do what was asked, and must not be swallowed. + await expect( + noteEntities( + buildMigrationContext({ + getDb: brokenDatabase as unknown as MigrationContext["getDb"], + recreateTable, + syncIndexes, + }), + ).up(), + ).rejects.toThrow("database or disk is full"); + }); + test("is registered under the id it declares, and says what it does", () => { const { description, id } = migration(); From d2b4a29d99a4717c447e933eeae3c26a8ce8a613 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 21:22:07 +0000 Subject: [PATCH 13/17] Take the database's word on every step, not just adding a column MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I argued on review that a stale read could only send the migration backwards into work every step tolerated. That was wrong about one step: if the rebuild had finished, moving the notes across failed on an attendee_id that had already gone. Each step now asks the database and believes its answer — a column already there, an attendee_id already gone — through one shared helper. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- .../db/migrations/2026-07-28_note_entities.ts | 64 ++++++++++++------- .../2026-07-28_note_entities.test.ts | 26 ++++++++ 2 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/shared/db/migrations/2026-07-28_note_entities.ts b/src/shared/db/migrations/2026-07-28_note_entities.ts index 205add0602..3aab135d43 100644 --- a/src/shared/db/migrations/2026-07-28_note_entities.ts +++ b/src/shared/db/migrations/2026-07-28_note_entities.ts @@ -2,27 +2,49 @@ import { queryBatchPrimary } from "#shared/db/client.ts"; import { errorMessage } from "#shared/error-message.ts"; import { bareSchemaMigration } from "./define.ts"; -/** A column already there is the state this asks for, so the work is done. */ +/** What the database says when a column is already there. */ const ALREADY_ADDED = "duplicate column name"; +/** What it says when `attendee_id` has already gone, so its notes are moved. */ +const ALREADY_MOVED = "no such column: attendee_id"; + +type RunSql = (sql: string) => Promise; + /** - * Add a column, treating one that is already there as added. A run that added - * it and then died leaves the database ahead of what a read reports — even a - * read from the primary, which can briefly answer with the schema as it was - * (see the backoff note in ./runner.ts) — so the database's own answer is the - * one to trust. + * Do a step, treating the database's own "that is already so" as done. + * + * A run that got partway and died leaves the database ahead of what a read + * reports — even a read from the primary, which can briefly answer with the + * schema as it was (see the backoff note in ./runner.ts). So each step asks the + * database to do the thing and believes its answer, rather than deciding from a + * picture of the schema that may be out of date. */ -const addColumn = async ( - getDb: () => { execute(sql: string): Promise }, - column: string, +const stepDone = async ( + run: () => Promise, + alreadySo: string, ): Promise => { try { - await getDb().execute(`ALTER TABLE system_notes ADD COLUMN ${column}`); + await run(); } catch (error) { - if (!errorMessage(error).includes(ALREADY_ADDED)) throw error; + if (!errorMessage(error).includes(alreadySo)) throw error; } }; +const addColumn = (execute: RunSql, column: string): Promise => + stepDone( + () => execute(`ALTER TABLE system_notes ADD COLUMN ${column}`), + ALREADY_ADDED, + ); + +const moveNotesToTargets = (execute: RunSql): Promise => + stepDone( + () => + execute( + "UPDATE system_notes SET entity_type = 'attendee', entity_id = attendee_id", + ), + ALREADY_MOVED, + ); + /** * Notes used to belong to an attendee and nothing else. They now name the kind * of record they are about and which one, so any record can carry notes. @@ -31,12 +53,11 @@ const addColumn = async ( * a table that already has rows; the rebuild at the end drops `attendee_id` and * takes the columns' final form from SCHEMA, which has no default. * - * Every step can be done again: adding a column that is there is taken as done, - * and the rebuild and the index sync each describe an end state rather than a - * change. So a run that stopped anywhere — a failed verify, a request that died - * — is picked up by the next one. The only early exit is "there is no - * `attendee_id` left to move", which is the state *after* this work; a stale - * read can only report the state before it, so it cannot end the run early. + * Every step can be done again, and each one takes the database's word for + * whether it is already so — a column that is there, an `attendee_id` that has + * gone. The rebuild and the index sync describe an end state rather than a + * change. So a run that stopped anywhere is picked up by the next one, even + * when what it reads about the schema is a moment behind what is true. */ export default bareSchemaMigration( "2026-07-28_note_entities", @@ -53,11 +74,10 @@ export default bareSchemaMigration( return; } - await addColumn(getDb, "entity_type TEXT NOT NULL DEFAULT 'attendee'"); - await addColumn(getDb, "entity_id INTEGER NOT NULL DEFAULT 0"); - await getDb().execute( - "UPDATE system_notes SET entity_type = 'attendee', entity_id = attendee_id", - ); + const execute: RunSql = (sql) => getDb().execute(sql); + await addColumn(execute, "entity_type TEXT NOT NULL DEFAULT 'attendee'"); + await addColumn(execute, "entity_id INTEGER NOT NULL DEFAULT 0"); + await moveNotesToTargets(execute); await recreateTable("system_notes"); // rebuild from SCHEMA, dropping attendee_id await syncIndexes(); // idx_system_notes_entity in place of the attendee one }, diff --git a/test/shared/db/migrations/2026-07-28_note_entities.test.ts b/test/shared/db/migrations/2026-07-28_note_entities.test.ts index b4cfb894ba..db29c62ab6 100644 --- a/test/shared/db/migrations/2026-07-28_note_entities.test.ts +++ b/test/shared/db/migrations/2026-07-28_note_entities.test.ts @@ -177,6 +177,32 @@ describeWithEnv( expect(await indexExists("idx_system_notes_entity")).toBe(true); }); + test("finishes when the work is done but the schema read is behind", async () => { + await downgradeToAttendeeNotes(); + // What a finished table answers to each step, when the read that chose + // to run them was a moment out of date. + const finishedDatabase = () => ({ + execute: (sql: string) => + Promise.reject( + new Error( + sql.startsWith("ALTER") + ? "duplicate column name: entity_type" + : "no such column: attendee_id", + ), + ), + }); + + await noteEntities( + buildMigrationContext({ + getDb: finishedDatabase as unknown as MigrationContext["getDb"], + recreateTable, + syncIndexes, + }), + ).up(); + + expect(await indexExists("idx_system_notes_entity")).toBe(true); + }); + test("stops when adding a column fails for any other reason", async () => { await downgradeToAttendeeNotes(); const brokenDatabase = () => ({ From b72a98e07db28bfc1edfd16c447ef42ff5d94b58 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 21:35:00 +0000 Subject: [PATCH 14/17] Pin each step's tolerance to its own answer, and drop a call that did nothing Mutation testing showed both: blanking either "already so" message still passed, because one test failed every statement and the other message threw regardless; and the index sync after the rebuild could be removed with nothing noticing, because the rebuild takes the table's indexes from SCHEMA as it goes. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EdDZCWRXwjqsidfRJFarXC --- .../db/migrations/2026-07-28_note_entities.ts | 5 ++- .../2026-07-28_note_entities.test.ts | 39 +++++++++++-------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/shared/db/migrations/2026-07-28_note_entities.ts b/src/shared/db/migrations/2026-07-28_note_entities.ts index 3aab135d43..1c44644914 100644 --- a/src/shared/db/migrations/2026-07-28_note_entities.ts +++ b/src/shared/db/migrations/2026-07-28_note_entities.ts @@ -78,7 +78,8 @@ export default bareSchemaMigration( await addColumn(execute, "entity_type TEXT NOT NULL DEFAULT 'attendee'"); await addColumn(execute, "entity_id INTEGER NOT NULL DEFAULT 0"); await moveNotesToTargets(execute); - await recreateTable("system_notes"); // rebuild from SCHEMA, dropping attendee_id - await syncIndexes(); // idx_system_notes_entity in place of the attendee one + // The rebuild drops attendee_id, and takes the table's indexes from SCHEMA + // as it goes, so idx_system_notes_entity arrives with it. + await recreateTable("system_notes"); }, ); diff --git a/test/shared/db/migrations/2026-07-28_note_entities.test.ts b/test/shared/db/migrations/2026-07-28_note_entities.test.ts index db29c62ab6..a6d634f4eb 100644 --- a/test/shared/db/migrations/2026-07-28_note_entities.test.ts +++ b/test/shared/db/migrations/2026-07-28_note_entities.test.ts @@ -203,24 +203,29 @@ describeWithEnv( expect(await indexExists("idx_system_notes_entity")).toBe(true); }); - test("stops when adding a column fails for any other reason", async () => { - await downgradeToAttendeeNotes(); - const brokenDatabase = () => ({ - execute: () => Promise.reject(new Error("database or disk is full")), + for (const failing of ["ALTER", "UPDATE"]) { + test(`stops when ${failing} fails for any other reason`, async () => { + await downgradeToAttendeeNotes(); + // Only this one statement fails, and not with an "already so" answer, + // so each step's tolerance has to be about its own answer alone. + const database = () => ({ + execute: (sql: string) => + sql.startsWith(failing) + ? Promise.reject(new Error("database or disk is full")) + : Promise.resolve({}), + }); + + await expect( + noteEntities( + buildMigrationContext({ + getDb: database as unknown as MigrationContext["getDb"], + recreateTable, + syncIndexes, + }), + ).up(), + ).rejects.toThrow("database or disk is full"); }); - - // A column that is already there is taken as done; anything else means - // the database could not do what was asked, and must not be swallowed. - await expect( - noteEntities( - buildMigrationContext({ - getDb: brokenDatabase as unknown as MigrationContext["getDb"], - recreateTable, - syncIndexes, - }), - ).up(), - ).rejects.toThrow("database or disk is full"); - }); + } test("is registered under the id it declares, and says what it does", () => { const { description, id } = migration(); From 25e7844584c7623187a93b131b833d0564c62b27 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 21:48:27 +0000 Subject: [PATCH 15/17] Re-anchor equivalent mutants after merging main Lines moved in test-browser.ts and attendee-page.ts, so the recorded positions no longer named the same mutants. --- scripts/mutation/equivalent-mutants.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/mutation/equivalent-mutants.txt b/scripts/mutation/equivalent-mutants.txt index a5575633f6..a811a2266a 100644 --- a/scripts/mutation/equivalent-mutants.txt +++ b/scripts/mutation/equivalent-mutants.txt @@ -247,9 +247,9 @@ test/test-utils/csrf.ts:236:20 ?? → || # quantity id capture: string|undefin test/test-utils/test-browser.ts:80:73 ?? → || # find(): LinkMatch|undefined; a found object is truthy test/test-utils/test-browser.ts:100:26 ?? → || # missing input type defaults to text; empty/invalid type follows the same successful-input path as text test/test-utils/test-browser.ts:150:19 ?? → || # submitted option tags are non-empty strings when present, otherwise undefined -test/test-utils/test-browser.ts:256:49 ?? → || # button value fallback is "", so empty value and absent value agree -test/test-utils/test-browser.ts:357:58 ?? → || # RequestInit.method is omitted or a non-empty method string in every caller -test/test-utils/test-browser.ts:422:28 ?? → || # forms[0] is a FormInfo object when present, otherwise undefined, so both operators reach throwNoForm() on the same input +test/test-utils/test-browser.ts:249:45 ?? → || # button value fallback is "", so empty value and absent value agree +test/test-utils/test-browser.ts:382:58 ?? → || # RequestInit.method is omitted or a non-empty method string in every caller +test/test-utils/test-browser.ts:460:28 ?? → || # forms[0] is a FormInfo object when present, otherwise undefined, so both operators reach throwNoForm() on the same input src/ui/templates/admin/listing-defaults.tsx:105:17 ?? → || # UrlControl value: string|undefined; the only falsy-non-null string is "" which equals the "" fallback, so ?? and || coincide src/ui/templates/admin/listing-defaults.tsx:128:38 ?? → || # value?.includes(day): boolean|undefined; for boolean b, b ?? false === b || false, and undefined ?? false === undefined || false src/shared/booking/build-tree.ts:130:48 ?? → || # childrenByParentId?.get(): readonly TicketListing[]|undefined — an array is always truthy, so ?? and || coincide @@ -354,7 +354,7 @@ src/shared/db/listing-prices.ts:201:45 ?? → || # foldGroupDayRows result.g # attendee entity page: `?? → ||` where the operand's only falsy-non-null # value equals the "" fallback. -src/features/admin/attendee-page.ts:265:60 ?? → || # ctx.query.get("token"): string|null; the only falsy-non-null value "" equals the "" fallback +src/features/admin/attendee-page.ts:266:60 ?? → || # ctx.query.get("token"): string|null; the only falsy-non-null value "" equals the "" fallback src/shared/db/name-registry.ts:89:51 ?? → || # matchName index.get(): number[]|undefined — an array is always truthy src/shared/db/name-registry.ts:109:20 ?? → || # isNameTakenAnywhere index.get(): number[]|undefined — an array is always truthy src/features/admin/catalog-transfer/import.ts:182:33 ?? → || # membershipSpec quantity comes from the PositiveInt schema (at least 1) or is undefined, so it is never 0 From 2c5ffa79a98486687b8136e5e40567e77ea72496 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 21:56:00 +0000 Subject: [PATCH 16/17] Point the importer plan at where the notes store now lives --- docs/importer-plan.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/importer-plan.md b/docs/importer-plan.md index d0c7352d40..99dd758ac5 100644 --- a/docs/importer-plan.md +++ b/docs/importer-plan.md @@ -313,8 +313,9 @@ Attendee notes / legacy metadata storage: - The original plan added a per-attendee encrypted `notes` column as the single home for `Customer Notes`, `Operator Notes`, payment metadata, etc. The notes - rework has since **landed**: main added a per-attendee **`system_notes`** table - (`src/shared/db/system-notes.ts`) — owner-public-key-encrypted `owner` notes + rework has since **landed**: main added a **`system_notes`** table, whose rows + name the kind of record they are about and which one + (`src/shared/db/notes/queries.ts`) — owner-public-key-encrypted `owner` notes (`createOwnerNote`) plus DB-key `system` notes — alongside per-contact `/admin/history/:hmac` records. **Do not** add a competing per-attendee `notes` column; `system_notes` is the home. From 21e7fd2ed1b386863bac26b8901a4fe4c223bdd9 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 28 Jul 2026 22:07:33 +0000 Subject: [PATCH 17/17] Assert the empty note delete really asks the database nothing The test's name promised a zero round-trip contract but only checked the note survived, so an empty batch would have passed it. --- test/shared/db/notes/queries.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/shared/db/notes/queries.test.ts b/test/shared/db/notes/queries.test.ts index 6b603e7912..4f6a476186 100644 --- a/test/shared/db/notes/queries.test.ts +++ b/test/shared/db/notes/queries.test.ts @@ -22,6 +22,7 @@ import { getTestPrivateKey } from "#test-utils/crypto.ts"; import { describeWithEnv } from "#test-utils/db.ts"; import { createTestAttendee } from "#test-utils/db-helpers/attendees.ts"; import { createTestListing } from "#test-utils/db-helpers/listings.ts"; +import { runAndCountRoundTrips } from "#test-utils/query-log.ts"; /** Create a listing + attendee and return the attendee id. */ const makeAttendee = async (name = "Note Target"): Promise => { @@ -230,8 +231,11 @@ describeWithEnv("db > notes", { db: true }, () => { // The stale-note cleanup runs on every refresh, almost always with nothing // to remove, so this path must not cost a round trip. - await deleteNotes(attendeeNotes(owner), []); + const { roundTrips } = await runAndCountRoundTrips(() => + deleteNotes(attendeeNotes(owner), []), + ); + expect(roundTrips).toBe(0); expect(await getNoteRows("attendee", [owner])).toHaveLength(1); }); });