import { sql } from "drizzle-orm";

//#region src/db/clean.ts
/**
* Cleans up test database by removing all data but keeping schema
* Dynamically gets all tables from the public schema and truncates them
*/
async function cleanupDatabase(db) {
	try {
		const tables = (await db.execute(sql.raw(`
      SELECT tablename 
      FROM pg_tables 
      WHERE schemaname = 'public'
    `))).rows.map((row) => row.tablename);
		if (tables.length === 0) return;
		const tableList = tables.map((t) => `"${t}"`).join(", ");
		await db.execute(sql.raw(`TRUNCATE TABLE ${tableList} RESTART IDENTITY CASCADE`));
	} catch (error) {
		console.debug("Could not clean database:", error);
	}
}

//#endregion
export { cleanupDatabase };