From 07d5161a4a587855480c8cda6f91f21931e04118 Mon Sep 17 00:00:00 2001 From: Lucas Berger Date: Sat, 13 Jun 2026 17:08:46 -0400 Subject: [PATCH] fix(quick-260613-ndv): reset familysync_test each run (truncate-all) CREATE DATABASE IF NOT EXISTS reuses the prior run's data, so the test DB would itself accumulate users run-over-run (the same flaky list_shares fan-out the dev DB suffered). Truncate every table (except the drizzle migration ledger) at globalSetup start, FK-safe, so each run is a clean deterministic slate. Verified: familysync_test users 186 -> 93 across a run (reset, not doubled); dev familysync untouched at 3. --- apps/api/test/global-setup.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/apps/api/test/global-setup.ts b/apps/api/test/global-setup.ts index ec67000..0249048 100644 --- a/apps/api/test/global-setup.ts +++ b/apps/api/test/global-setup.ts @@ -105,9 +105,32 @@ export default async function setup(): Promise { new URL('../src/db/migrations', import.meta.url), ); await migrate(db, { migrationsFolder }); + + // Clean slate each run: truncate every table (except drizzle's migration + // ledger) so familysync_test does NOT accumulate rows across runs. Without + // this, CREATE DATABASE IF NOT EXISTS reuses the prior run's data and the + // users table grows unbounded — re-creating the slow/flaky list_shares + // fan-out the dev DB used to suffer, just in the test DB. FK-safe via the + // session FOREIGN_KEY_CHECKS toggle (scoped to this one connection). + const conn = await appPool.getConnection(); + try { + const [rows] = await conn.query( + `SELECT table_name AS t FROM information_schema.tables + WHERE table_schema = ? AND table_type = 'BASE TABLE' + AND table_name <> '__drizzle_migrations'`, + [TEST_DB], + ); + await conn.query('SET FOREIGN_KEY_CHECKS = 0'); + for (const { t } of rows as Array<{ t: string }>) { + await conn.query(`TRUNCATE TABLE \`${t}\``); + } + await conn.query('SET FOREIGN_KEY_CHECKS = 1'); + } finally { + conn.release(); + } } finally { await appPool.end(); } - console.log('[global-setup] provisioned + migrated familysync_test'); + console.log('[global-setup] provisioned + migrated + reset familysync_test'); }