fix(db): move impersonation TRUNCATE before staff upsert to avoid FK violation
The TRUNCATE of impersonation_sessions/audit_logs was running after the staff upsert. When a prior seed left impersonation_sessions rows referencing staff records, ON CONFLICT DO UPDATE on staff violated the FK constraint on impersonation_sessions.staff_id. Moving the TRUNCATE before the staff block ensures all impersonation rows are cleared before any staff insert/update attempt. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -408,9 +408,11 @@ async function seed() {
|
|||||||
{ id: uuid(), name: "Devon Williams", email: "devon@groombook.dev", role: "groomer" as const, isSuperUser: false },
|
{ id: uuid(), name: "Devon Williams", email: "devon@groombook.dev", role: "groomer" as const, isSuperUser: false },
|
||||||
];
|
];
|
||||||
|
|
||||||
// Truncate downstream tables before staff upsert — clears stale appointments
|
// Truncate downstream tables before staff upsert — clears stale impersonation
|
||||||
// and other FK references to old staff IDs so the id column can safely be updated
|
// sessions from prior seed runs so the FK constraint on staff_id is never
|
||||||
await db.execute(sql`TRUNCATE appointments, invoices, invoice_line_items, invoice_tip_splits, grooming_visit_logs CASCADE`);
|
// violated when ON CONFLICT DO UPDATE touches staff rows that still have
|
||||||
|
// impersonation_sessions references.
|
||||||
|
await db.execute(sql`TRUNCATE impersonation_sessions, impersonation_audit_logs, appointments, invoices, invoice_line_items, invoice_tip_splits, grooming_visit_logs CASCADE`);
|
||||||
|
|
||||||
const allStaff = [...managerStaff, ...receptionistStaff, ...groomers, ...bathers];
|
const allStaff = [...managerStaff, ...receptionistStaff, ...groomers, ...bathers];
|
||||||
for (const s of allStaff) {
|
for (const s of allStaff) {
|
||||||
@@ -430,10 +432,6 @@ async function seed() {
|
|||||||
}
|
}
|
||||||
console.log(`✓ Created ${allStaff.length} staff (1 manager, 1 receptionist, 3 groomers, 3 bathers)`);
|
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 impersonation_sessions, impersonation_audit_logs, appointments, invoices, invoice_line_items, invoice_tip_splits, grooming_visit_logs CASCADE`);
|
|
||||||
|
|
||||||
// ── Services ──
|
// ── Services ──
|
||||||
// Upsert services using name as unique key. With deterministic IDs in
|
// Upsert services using name as unique key. With deterministic IDs in
|
||||||
// servicesDef and TRUNCATE clearing downstream tables first, this is
|
// servicesDef and TRUNCATE clearing downstream tables first, this is
|
||||||
|
|||||||
Reference in New Issue
Block a user