forked from EduCraft/curriculum-project-hub
fix(hub): reset integration DB with TRUNCATE CASCADE
deleteMany could not reliably clear nested agent-config folders and left ghost Organization rows that broke the next upsert. Truncate every public table except _prisma_migrations before seeding the default org.
This commit is contained in:
@@ -50,20 +50,25 @@ export const prisma = new PrismaClient({
|
||||
|
||||
/** Truncate all tables before each test for isolation. */
|
||||
export async function resetDb(): Promise<void> {
|
||||
// User and Organization are the aggregate roots for all domain rows; their
|
||||
// declared FK cascades clear projects, search documents, permissions,
|
||||
// sessions and connections without repeatedly truncating pg_trgm indexes.
|
||||
// Event receipts and global audit rows are independent roots.
|
||||
await prisma.$transaction([
|
||||
prisma.feishuEventReceipt.deleteMany(),
|
||||
prisma.auditEntry.deleteMany(),
|
||||
// Permission resource ids are intentionally polymorphic strings, so these
|
||||
// two tables have no FK to Project and must be cleared explicitly.
|
||||
prisma.permissionGrant.deleteMany(),
|
||||
prisma.permissionSettings.deleteMany(),
|
||||
prisma.user.deleteMany(),
|
||||
prisma.organization.deleteMany(),
|
||||
]);
|
||||
// Hard reset via TRUNCATE CASCADE. Parent RESTRICT edges and leftover
|
||||
// folder trees made deleteMany-based cleanup race Prisma upserts
|
||||
// ("Unique constraint failed on id" while the where-branch saw no row).
|
||||
await prisma.$executeRawUnsafe(`
|
||||
DO $$
|
||||
DECLARE
|
||||
stmt text;
|
||||
BEGIN
|
||||
SELECT 'TRUNCATE TABLE ' || string_agg(format('%I.%I', schemaname, tablename), ', ')
|
||||
|| ' RESTART IDENTITY CASCADE'
|
||||
INTO stmt
|
||||
FROM pg_tables
|
||||
WHERE schemaname = 'public'
|
||||
AND tablename <> '_prisma_migrations';
|
||||
IF stmt IS NOT NULL THEN
|
||||
EXECUTE stmt;
|
||||
END IF;
|
||||
END $$;
|
||||
`);
|
||||
await seedTestOrganization();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user