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:
2026-07-30 13:28:42 +08:00
parent 49e1e2f19e
commit 4eafbf20a9
+19 -14
View File
@@ -50,20 +50,25 @@ export const prisma = new PrismaClient({
/** Truncate all tables before each test for isolation. */ /** Truncate all tables before each test for isolation. */
export async function resetDb(): Promise<void> { export async function resetDb(): Promise<void> {
// User and Organization are the aggregate roots for all domain rows; their // Hard reset via TRUNCATE CASCADE. Parent RESTRICT edges and leftover
// declared FK cascades clear projects, search documents, permissions, // folder trees made deleteMany-based cleanup race Prisma upserts
// sessions and connections without repeatedly truncating pg_trgm indexes. // ("Unique constraint failed on id" while the where-branch saw no row).
// Event receipts and global audit rows are independent roots. await prisma.$executeRawUnsafe(`
await prisma.$transaction([ DO $$
prisma.feishuEventReceipt.deleteMany(), DECLARE
prisma.auditEntry.deleteMany(), stmt text;
// Permission resource ids are intentionally polymorphic strings, so these BEGIN
// two tables have no FK to Project and must be cleared explicitly. SELECT 'TRUNCATE TABLE ' || string_agg(format('%I.%I', schemaname, tablename), ', ')
prisma.permissionGrant.deleteMany(), || ' RESTART IDENTITY CASCADE'
prisma.permissionSettings.deleteMany(), INTO stmt
prisma.user.deleteMany(), FROM pg_tables
prisma.organization.deleteMany(), WHERE schemaname = 'public'
]); AND tablename <> '_prisma_migrations';
IF stmt IS NOT NULL THEN
EXECUTE stmt;
END IF;
END $$;
`);
await seedTestOrganization(); await seedTestOrganization();
} }