All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 27m55s
57 lines
2.3 KiB
TypeScript
57 lines
2.3 KiB
TypeScript
// src/tests/utils/cleanup.ts
|
|
import { getPool } from '../../services/db/connection.db';
|
|
import { logger } from '../../services/logger.server';
|
|
|
|
interface CleanupOptions {
|
|
userIds?: (string | null | undefined)[];
|
|
flyerIds?: (number | null | undefined)[];
|
|
storeIds?: (number | null | undefined)[];
|
|
recipeIds?: (number | null | undefined)[];
|
|
budgetIds?: (number | null | undefined)[];
|
|
}
|
|
|
|
/**
|
|
* A centralized utility to clean up database records created during tests.
|
|
* It deletes records in an order that respects foreign key constraints.
|
|
* It performs operations on a single client connection but does not use a
|
|
* transaction, ensuring that a failure to delete from one table does not
|
|
* prevent cleanup attempts on others.
|
|
*/
|
|
export const cleanupDb = async (options: CleanupOptions) => {
|
|
const pool = getPool();
|
|
const client = await pool.connect();
|
|
|
|
try {
|
|
// Order of deletion matters to avoid foreign key violations.
|
|
// Children entities first, then parents.
|
|
|
|
if (options.budgetIds?.filter(Boolean).length) {
|
|
await client.query('DELETE FROM public.budgets WHERE budget_id = ANY($1::int[])', [options.budgetIds]);
|
|
logger.debug(`Cleaned up ${options.budgetIds.length} budget(s).`);
|
|
}
|
|
|
|
if (options.recipeIds?.filter(Boolean).length) {
|
|
await client.query('DELETE FROM public.recipes WHERE recipe_id = ANY($1::int[])', [options.recipeIds]);
|
|
logger.debug(`Cleaned up ${options.recipeIds.length} recipe(s).`);
|
|
}
|
|
|
|
if (options.flyerIds?.filter(Boolean).length) {
|
|
await client.query('DELETE FROM public.flyers WHERE flyer_id = ANY($1::int[])', [options.flyerIds]);
|
|
logger.debug(`Cleaned up ${options.flyerIds.length} flyer(s).`);
|
|
}
|
|
|
|
if (options.storeIds?.filter(Boolean).length) {
|
|
await client.query('DELETE FROM public.stores WHERE store_id = ANY($1::int[])', [options.storeIds]);
|
|
logger.debug(`Cleaned up ${options.storeIds.length} store(s).`);
|
|
}
|
|
|
|
if (options.userIds?.filter(Boolean).length) {
|
|
await client.query('DELETE FROM public.users WHERE user_id = ANY($1::uuid[])', [options.userIds]);
|
|
logger.debug(`Cleaned up ${options.userIds.length} user(s).`);
|
|
}
|
|
} catch (error) {
|
|
logger.error({ error, options }, 'A database cleanup operation failed.');
|
|
} finally {
|
|
client.release();
|
|
}
|
|
}; |