fix(db): seed staff_id FK fix (GRO-369) #199

Merged
groombook-engineer[bot] merged 5 commits from fix/gro-369-staff-id-fk into main 2026-04-01 14:19:49 +00:00

5 Commits

Author SHA1 Message Date
groombook-cto[bot] b9505404eb Merge branch 'main' into fix/gro-369-staff-id-fk 2026-04-01 13:58:38 +00:00
groombook-engineer[bot] 8928cd8805 fix(db): move TRUNCATE before staff upsert and add id to set clause (GRO-369)
TRUNCATE was positioned after the staff upsert block, so appointments
referencing stale staff IDs were never cleared before re-insert. Moving it
before the upsert ensures downstream FK references are purged first.

Also adds `id: s.id` to the staff upsert set clause so re-seeded rows get
their IDs overwritten to match the deterministic PRNG values — preventing
the appointments-insert referencing IDs that don't exist in the staff table.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-01 13:48:09 +00:00
groombook-engineer[bot] 84c8236227 fix(db): add migration 0020 for UNIQUE(name) + align admin seed ON CONFLICT
Problem:
- Schema change in eacf8ab added .unique() to services.name
- No migration existed to apply this constraint to the database
- Admin seed and seedKnownUsers still used ON CONFLICT (id) with
  a0000001-... IDs, inconsistent with main seed's b0000001-... IDs
  and ON CONFLICT (name)

Fix:
- Migration 0020: clean up existing duplicate services (keep lowest
  id per name), then add UNIQUE constraint on services.name
- Admin seed (apps/api): switch to ON CONFLICT (name) and b0000001
  IDs to match main seed's servicesDef
- seedKnownUsers (packages/db): same alignment — b0000001 IDs and
  ON CONFLICT (name)

GRO-364: ON CONFLICT (name) eliminates the duplicate-row problem
that the old dedup+ON CONFLICT(id) approach could not solve when
existing rows had non-deterministic IDs.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-01 13:41:18 +00:00
groombook-engineer[bot] eacf8abc3b fix(db): remove dedup DELETE and use ON CONFLICT (name) for idempotent services seed
The dedup DELETE was causing two problems:
1. FK violation (GRO-365) — deleting services referenced by appointments
2. Duplicate services (GRO-301) — MIN(id) per name could delete the wrong row,
   causing ON CONFLICT (id) to create duplicates on re-run

Fix:
- Remove the dedup DELETE entirely
- Keep TRUNCATE of downstream tables (appointments, invoices, etc.) to clear
  stale data from prior runs
- Change ON CONFLICT target from `id` to `name` with a unique constraint on
  name column — deterministic IDs in servicesDef ensure idempotency

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-01 13:23:11 +00:00
groombook-engineer[bot] 7fb5ddbbd1 fix(db): truncate downstream tables before services dedup to avoid FK violation
TRUNCATE appointments, invoices, invoice_line_items, invoice_tip_splits,
and grooming_visit_logs CASCADE before the services dedup DELETE to prevent
FK violations from appointments created by previous seed runs.

Fixes: GRO-365

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-01 13:10:58 +00:00