Skip to content

Shift Types: adding a 2nd time to a shift silently fails to save #466

Open

Description

@mbhewitt

Reported on the PEERS deployment; the same bug exists on Census (shared shift-types code). Filing so the fix can be ported. Fixed on the peers-main branch in commits ab8fa94 and 68c1c90 — note it is not a clean cherry-pick onto main because it sits on top of peers-only shift features (cancel-shift notify, time_text columns), so it needs a manual port.

Symptom

Creating/editing a Shift Type, an admin can add one time (date + start/end + position/slots) but a second time "saves" with no error, then is gone after clicking Update and reopening.

Root causes (three, compounding)

  1. op_shift_times.shift_instance had a GLOBAL UNIQUE key, but the "Add time" dialog (ShiftTypesTimeDialogAdd.tsx) only checks a new Instance label against the current shift type's times. So a label another type already uses (e.g. one shift's "Monday Morning") passes the UI but violates the DB key.
  2. The insert errors were swallowed. handleTimeListAdd (api/shifts/types/index.ts) and the PATCH add/remove/position paths (api/shifts/types/[typeId]/index.ts) used un-awaited forEach(async …), so the duplicate-key error never surfaced — the route returned 2xx and the row silently disappeared. (fetcherTrigger also resolves the JSON body on non-2xx, so the client can't see a 4xx either.)
  3. New-time position wrongly marked removed. Even with a unique label, the PATCH handler snapshotted existing op_shift_time_position rows after inserting the new time, so the freshly-inserted position (server id not in the request) was seen as orphaned and set remove_time_position=1. The form GET filters out any time whose position is removed → the time hides on reload.

Fix applied (peers-main)

  • Per-type uniqueness: migrate op_shift_times from UNIQUE(shift_instance) → UNIQUE(shift_name_id, shift_instance). Labels must be unique within a shift type but can repeat across types (matches the UI's within-type check). Migration: database/migrations/2026-07-02-shift-instance-per-type.sql. Safe on existing data (all instances already globally unique); no code looks up a row by shift_instance alone.
  • Server pre-check findInstanceConflict() run before any writes → returns a clean 409 with a clear message naming the offending label (no partial writes).
  • Snapshot dbTimePositionList BEFORE inserting new times, so a fresh position is never treated as orphaned.
  • Await the add/remove writes (Promise.all) so errors propagate; insert each new time individually (old code re-inserted the whole timeListAdd once per item).
  • Client: both shift-type submit handlers now treat a statusCode >= 400 body as an error so the message surfaces.

Still open (lower priority, not addressed)

  • The remaining un-awaited forEach(async …) in the PATCH time-position update/remove paths.
  • generateId() returns before its async collision recheck resolves (recheck is effectively dead code).

Activity

  1. mbhewitt commented on Jul 2, 2026

    @mbhewitt
    OwnerAuthor

    Correction: the migration file is migrations/006_shift_instance_per_type.sql (top-level migrations/ dir, following the repo's 001–005 convention) — not the database/migrations/ path mentioned above.

  2. added
    ready to build
    on Oct 30, 2026
  3. mbhewitt commented on Nov 27, 2026

    @mbhewitt
    OwnerAuthor

    Folded in #477 (Shift instance uniqueness bug — borrow Peers' solution). Why: same shift-instance uniqueness bug (scheduler_schema.sql:313). Link #533 too, but do not fold it (front-end cause, same editor). The migration named in #466 (006_shift_instance_per_type.sql) does not exist

    Its description, for reference:

    From Mew 2026-07-04. Peers (the other bot/team) found a bug in shift instance uniqueness and has a solution — we should borrow it.

    Action: get the details of the uniqueness bug + Peers' fix from that work and port the same solution here. (Not investigating Peers' repo unprompted — pull the specifics when picking this up, or ask Mew/Peers to point at the fix.)

    Low detail for now by design; capturing so it isn't lost.

    Approved by Mew on #851 (issue sweep, 2026-11-27).

  4. mbhewitt commented on Nov 29, 2026

    @mbhewitt
    OwnerAuthor

    Shipped in PR #901 (prod 2027.G.00901.C, 2026-11-29). Migration 037 applied. Leaving this open for Mew to close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ready to build

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions