<commit_before>const pgtools = require("pgtools")

module.exports = createDb

// Time out after 10 seconds - should probably be able to override this
const DEFAULT_TIMEOUT = 10000

function createDb(dbName, dbConfig = {}, config = {}) { // eslint-disable-line complexity
  const {user, password, host, port} = dbConfig
  if (typeof dbName !== "string") {
    return Promise.reject(new Error("Must pass database name as a string"))
  }
  if (
    typeof user !== "string" ||
    typeof password !== "string" ||
    typeof host !== "string" ||
    typeof port !== "number"
  ) {
    return Promise.reject(new Error("Database config problem"))
  }

  const log = config.logger || (() => {})

  log(`Attempting to create database: ${dbName}`)

  // pgtools mutates its inputs (tut tut) so create our own object here
  const pgtoolsConfig = {
    database: dbConfig.defaultDatabase || "postgres",
    user,
    password,
    host,
    port,
  }

  return pgtools.createdb(
    pgtoolsConfig,
    dbName
  )
  .timeout(DEFAULT_TIMEOUT, `Timed out trying to create database: ${dbName}`) // pgtools uses Bluebird
  .then(() => log(`Created database: ${dbName}`))
  .catch((err) => {
    if (err) {
      // we are not worried about duplicate db errors
      if (err.name !== "duplicate_database") {
        log(err)
        throw new Error(`Error creating database. Caused by: '${err.name}: ${err.message}'`)
      } else {
        log(`'${dbName}' database already exists`)
      }
    }
  })
}<commit_msg>Refactoring createDb to use async/await<commit_after>const pgtools = require("pgtools")

module.exports = createDb

// Time out after 10 seconds - should probably be able to override this
const DEFAULT_TIMEOUT = 10000

async function createDb(dbName, dbConfig = {}, config = {}) { // eslint-disable-line complexity
  const {user, password, host, port} = dbConfig
  if (typeof dbName !== "string") {
    throw new Error("Must pass database name as a string")
  }
  if (
    typeof user !== "string" ||
    typeof password !== "<PASSWORD>" ||
    typeof host !== "string" ||
    typeof port !== "number"
  ) {
    throw new Error("Database config problem")
  }

  const log = config.logger || (() => {})

  log(`Attempting to create database: ${dbName}`)

  // pgtools mutates its inputs (tut tut) so create our own object here
  const pgtoolsConfig = {
    database: dbConfig.defaultDatabase || "postgres",
    user,
    password,
    host,
    port,
  }

  try {
    await pgtools.createdb(pgtoolsConfig, dbName)
      .timeout(DEFAULT_TIMEOUT, `Timed out trying to create database: ${dbName}`) // pgtools uses Bluebird
    log(`Created database: ${dbName}`)
  } catch (err) {
    if (err) {
      // we are not worried about duplicate db errors
      if (err.name !== "duplicate_database") {
        log(err)
        throw new Error(`Error creating database. Caused by: '${err.name}: ${err.message}'`)
      } else {
        log(`'${dbName}' database already exists`)
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.13826581854703715, 'fraction_numerical': 0.004686976899899564, 'mean_word_length': 3.081967213114754, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, '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': '6045717', 'n_tokens_mistral': 903, 'n_tokens_neox': 886, 'n_words': 285}