From 8928cd88052c70f4d9def00efc66862cd5f47e7f Mon Sep 17 00:00:00 2001 From: "groombook-engineer[bot]" <3141748+groombook-engineer[bot]@users.noreply.github.com> Date: Wed, 1 Apr 2026 13:48:09 +0000 Subject: [PATCH] fix(db): move TRUNCATE before staff upsert and add id to set clause (GRO-369) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- packages/db/src/seed.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/db/src/seed.ts b/packages/db/src/seed.ts index 8b1a6c1..6813b5b 100644 --- a/packages/db/src/seed.ts +++ b/packages/db/src/seed.ts @@ -408,6 +408,10 @@ async function seed() { { id: uuid(), name: "Devon Williams", email: "devon@groombook.dev", role: "groomer" as const, isSuperUser: false }, ]; + // Truncate downstream tables before staff upsert — clears stale appointments + // and other FK references to old staff IDs so the id column can safely be updated + await db.execute(sql`TRUNCATE appointments, invoices, invoice_line_items, invoice_tip_splits, grooming_visit_logs CASCADE`); + const allStaff = [...managerStaff, ...receptionistStaff, ...groomers, ...bathers]; for (const s of allStaff) { await db.insert(schema.staff) @@ -421,15 +425,11 @@ async function seed() { }) .onConflictDoUpdate({ target: schema.staff.email, - set: { name: s.name, role: s.role, isSuperUser: s.isSuperUser, active: true }, + set: { id: s.id, name: s.name, role: s.role, isSuperUser: s.isSuperUser, active: true }, }); } console.log(`✓ Created ${allStaff.length} staff (1 manager, 1 receptionist, 3 groomers, 3 bathers)`); - // Truncate downstream tables before services upsert — clears stale appointments - // from prior seed runs so the FK constraint on service_id is never violated - await db.execute(sql`TRUNCATE appointments, invoices, invoice_line_items, invoice_tip_splits, grooming_visit_logs CASCADE`); - // ── Services ── // Upsert services using name as unique key. With deterministic IDs in // servicesDef and TRUNCATE clearing downstream tables first, this is