<commit_before>const Promise = require('bluebird')
const { exec } = require('child_process')
const _ = require('lomath')
const path = require('path')
const Sequelize = require('sequelize')
const log = require(path.join(__dirname, 'log'))
const dbConfig = require(path.join(__dirname, '..', 'config', 'db.json'))

/* istanbul ignore next */
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const sqlConfig = _.get(dbConfig, `${process.env.NODE_ENV}`)

const sequelize = new Sequelize(
  sqlConfig.database,
  sqlConfig.username,
  sqlConfig.password,
  sqlConfig)

/* istanbul ignore next */
function createDb() {
  const sysSeq = new Sequelize(
    'sys',
    sqlConfig.username,
    sqlConfig.password,
    sqlConfig)
  var nodeEnvs = ['test', 'development', 'production']
  var createDbQueries = _.map(nodeEnvs, (nodeEnv) => {
    return "CREATE DATABASE " + _.get(dbConfig, `${nodeEnv}.database`) + ";"
  })

  return Promise.any(
      _.map(createDbQueries, (createDbQuery) => {
        return sysSeq.query(createDbQuery)
      })).then(() => {
      sysSeq.close()
      log.info(`Created the aiva sql databases`)
    })
    .catch(e => { log.error(JSON.stringify(e, null, 2)) })
}

/* istanbul ignore next */
function authDb() {
  return sequelize
    .authenticate()
    .then((e) => {
      log.info('Authenticated SQL database successfully');
      sequelize.close().finally()
    })
    .catch((e) => {
      if (_.get(e, 'original.code') === 'ER_BAD_DB_ERROR') {
        createDb().then(() => { sequelize.close() })
      }
    })
}

/* istanbul ignore next */
function migrateDb() {
  return new Promise((resolve, reject) => {
    exec(`./node_modules/.bin/sequelize db:migrate --env ${process.env.NODE_ENV}`, (err, stdout, stderr) => {
      if (err) {
        log.error(stderr.toString())
        reject(err)
      }
      log.debug(stdout.toString())
      resolve()
    })
  })
}

module.exports = {
  authDb: authDb,
  migrateDb: migrateDb
}

/* istanbul ignore next */
if (require.main === module) {
  authDb()
}
<commit_msg>remove finlly from db auth<commit_after>const Promise = require('bluebird')
const { exec } = require('child_process')
const _ = require('lomath')
const path = require('path')
const Sequelize = require('sequelize')
const log = require(path.join(__dirname, 'log'))
const dbConfig = require(path.join(__dirname, '..', 'config', 'db.json'))

/* istanbul ignore next */
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const sqlConfig = _.get(dbConfig, `${process.env.NODE_ENV}`)

const sequelize = new Sequelize(
  sqlConfig.database,
  sqlConfig.username,
  sqlConfig.password,
  sqlConfig)

/* istanbul ignore next */
function createDb() {
  const sysSeq = new Sequelize(
    'sys',
    sqlConfig.username,
    sqlConfig.password,
    sqlConfig)
  var nodeEnvs = ['test', 'development', 'production']
  var createDbQueries = _.map(nodeEnvs, (nodeEnv) => {
    return "CREATE DATABASE " + _.get(dbConfig, `${nodeEnv}.database`) + ";"
  })

  return Promise.any(
      _.map(createDbQueries, (createDbQuery) => {
        return sysSeq.query(createDbQuery)
      })).then(() => {
      sysSeq.close()
      log.info(`Created the aiva sql databases`)
    })
    .catch(e => { log.error(JSON.stringify(e, null, 2)) })
}

/* istanbul ignore next */
function authDb() {
  return sequelize
    .authenticate()
    .then((e) => {
      log.info('Authenticated SQL database successfully');
      sequelize.close()
    })
    .catch((e) => {
      if (_.get(e, 'original.code') === 'ER_BAD_DB_ERROR') {
        createDb().then(() => { sequelize.close() })
      }
    })
}

/* istanbul ignore next */
function migrateDb() {
  return new Promise((resolve, reject) => {
    exec(`./node_modules/.bin/sequelize db:migrate --env ${process.env.NODE_ENV}`, (err, stdout, stderr) => {
      if (err) {
        log.error(stderr.toString())
        reject(err)
      }
      log.debug(stdout.toString())
      resolve()
    })
  })
}

module.exports = {
  authDb: authDb,
  migrateDb: migrateDb
}

/* istanbul ignore next */
if (require.main === module) {
  authDb()
}

{'fraction_non_alphanumeric': 0.16986634264884568, 'fraction_numerical': 0.0004860267314702309, 'mean_word_length': 3.2964509394572024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3562359', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1367, 'n_words': 284}