From 187c3a8b647a0bbadf8b1d219c210faa82b6a522 Mon Sep 17 00:00:00 2001 From: matt Date: Thu, 24 Feb 2022 23:16:24 +0000 Subject: [PATCH] Rewrite cleanDb to be more dynamic --- utility/db/dbTestData.js | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/utility/db/dbTestData.js b/utility/db/dbTestData.js index 1d959f0..6773d6a 100644 --- a/utility/db/dbTestData.js +++ b/utility/db/dbTestData.js @@ -357,28 +357,18 @@ const relationships = { ] }; +/** + * cleanDb() Removes all records from the tables in the database to be inserted + * into + * + * @param {dbConnectionPool} [dbConnectionPool] - The database connection + * + * @return {void} + */ async function cleanDb(dbConnectionPool) { - /* - Cleans the database of any existing records that will - conflict with the test data - - Arguments: - - database connection object - */ - - // List of table names to be cleared - const deletionList = [ - 'studentParentLink', - 'studentClassLink', - 'accountClassLink', - 'class', - 'subject', - 'parent', - 'student', - 'account' - ]; - - for (const table of deletionList) + // Remove records from tables in reverse order to which they depend on + // each other + for (const table of Object.keys(data).reverse()) await dbConnectionPool.runQuery(`DELETE FROM ${table};`); }