From b90e3675639467943226d16b1c8e96baa2f161f5 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 17 Mar 2022 09:04:57 +0000 Subject: [PATCH] Fixed error handling on db truncate in tests - if the database does not exist, ignore the error - tables that don't exist don't need truncating - instead wait until the next thing calls init, that will cause a fresh db to be created in the correct state - inside of reset, if the truncate fails then do a full reset instead --- test/utils/db-utils.js | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/test/utils/db-utils.js b/test/utils/db-utils.js index 5d47293516..daa479ab49 100644 --- a/test/utils/db-utils.js +++ b/test/utils/db-utils.js @@ -47,10 +47,15 @@ module.exports.reset = async ({truncate} = {truncate: false}) => { } } else { if (truncate) { - // Perform a fast reset by tearing down all the tables and - // inserting the fixtures - await module.exports.teardown(); - await knexMigrator.init({only: 2}); + // Perform a fast reset by tearing down all the tables and inserting the fixtures + try { + await module.exports.teardown(); + await knexMigrator.init({only: 2}); + } catch (err) { + // If it fails, try a normal restore + await knexMigrator.reset({force: true}); + await knexMigrator.init(); + } } else { // Do a full database reset + initialisation await knexMigrator.reset({force: true}); @@ -140,12 +145,25 @@ module.exports.teardown = () => { return db.knex.raw('SET FOREIGN_KEY_CHECKS=1;').transacting(trx); }) .catch(function (err) { - // CASE: table does not exist - if (err.errno === 1146) { + // CASE: table does not exist || DB does not exist + // If the table or DB are not present, we can safely ignore + if (err.errno === 1146 || err.errno === 1049) { return Promise.resolve(); } throw err; + }) + .finally(() => { + debug('Database teardown end'); }); - }); + }) + .catch(function (err) { + // CASE: table does not exist || DB does not exist + // If the table or DB are not present, we can safely ignore + if (err.errno === 1146 || err.errno === 1049) { + return Promise.resolve(); + } + + throw err; + }); };