All files / helpers/wrappers migrations.js

95.23% Statements 20/21
88.88% Branches 8/9
75% Functions 3/4
95.23% Lines 20/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54    1x 1x   1x               1x           5x 5x 4x     4x   4x 4x   4x 4x 2x     4x 4x 4x   4x   13x     2x     2x              
/* eslint-disable no-console */
 
const YELLOW = '\x1b[33m';
const RESET = '\x1b[0m';
 
const ERROR_TYPES_BYPASS = [
  'ER_DUP_FIELDNAME', // Campo duplicado
  'ER_CANT_DROP_FIELD_OR_KEY', // Nao pode remover campo ou chave
  'ER_NO_SUCH_TABLE', // Tabela nao existe
  "doesn't have the column", // Coluna nao existe
  'Check the table name and schema', // Verifique o nome da tabela e o esquema (Tabela nao existe)
];
 
export const wrapperUp = async (
  queryInterface,
  callback = () => {
    return false;
  }
) => {
  try {
    await queryInterface.sequelize.transaction((transaction) => {
      return callback(transaction);
    });
  } catch (e) {
    const { stack } = new Error();
 
    const stackArray = stack.split('\n');
    const lastItem = stackArray[stackArray.length - 1].trim(); // Pega o Ășltimo item do array
 
    let errorCodeOrMessage = e.parent ? e.parent.code : '';
    if (!errorCodeOrMessage && e.name.toLowerCase() === 'error') {
      errorCodeOrMessage = e.message;
    }
 
    console.warn(`${YELLOW}>>[WARN]<<${RESET}`, lastItem);
    console.warn(`${YELLOW}>>[WARN]<<${RESET}`, e.message);
    console.warn(`${YELLOW}>>[WARN]<<${RESET}`, errorCodeOrMessage);
 
    if (
      ERROR_TYPES_BYPASS.find((eCode) =>
        errorCodeOrMessage.toLowerCase().includes(eCode.toLowerCase())
      )
    ) {
      return;
    }
 
    throw e;
  }
};
 
export default {
  wrapperUp,
};