<commit_before>const pg = require("pg")
const fs = require("fs")
const crypto = require("crypto")
const bluebird = require("bluebird")
const path = require("path")
const SQL = require("sql-template-strings")
const dedent = require("dedent-js")

module.exports = migrate

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

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

  const client = bluebird.promisifyAll(new pg.Client(dbConfig))

  log("Attempting database migration")

  return client.connectAsync()
    .then(() => log("Connected to database"))
    .then(() => loadMigrationFiles(migrationsDirectory, log))
    .then(filterMigrations(client))
    .then(runMigrations(client, log))
    .then(() => client.end())
    .then(() => log("Successfully applied all migrations"))
    .catch((err) => {
      log(`Migration failed. Reason: ${err.message}`)
      throw err
    })
}

// Work out which migrations to apply
function filterMigrations(client) {
  return (migrations) => {
    // Arrange in ID order
    const orderedMigrations = migrations.sort((a, b) => a.id - b.id)

    // Assert their IDs are consecutive integers
    migrations.forEach((mig, i) => {
      if (mig.id !== i) {
        throw new Error("Found a non-consecutive migration ID")
      }<commit_msg>Log and return which migrations were applied<commit_after>const pg = require("pg")
const fs = require("fs")
const crypto = require("crypto")
const bluebird = require("bluebird")
const path = require("path")
const SQL = require("sql-template-strings")
const dedent = require("dedent-js")

module.exports = migrate

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

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

  const client = bluebird.promisifyAll(new pg.Client(dbConfig))

  log("Attempting database migration")

  return client.connectAsync()
    .then(() => log("Connected to database"))
    .then(() => loadMigrationFiles(migrationsDirectory, log))
    .then(filterMigrations(client))
    .then(runMigrations(client, log))
    .then(finalise(client, log))
    .catch((err) => {
      log(`Migration failed. Reason: ${err.message}`)
      throw err
    })
}

function finalise(client, log) {
  return (completedMigrations) => {
    if (completedMigrations.length === 0) {
      log("No migrations applied")
    } else {
      const names = completedMigrations.map((m) => m.name)
      log(`Successfully applied migrations: ${names}`)
    }

    return client.endAsync()
      .then(() => completedMigrations)
  }
}

// Work out which migrations to apply
function filterMigrations(client) {
  return (migrations) => {
    // Arrange in ID order
    const orderedMigrations = migrations.sort((a, b) => a.id - b.id)

    // Assert their IDs are consecutive integers
    migrations.forEach((mig, i) => {
      if (mig.id !== i) {
        throw new Error("Found a non-consecutive migration ID")
      }
{'fraction_non_alphanumeric': 0.1503267973856209, 'fraction_numerical': 0.00026143790849673205, 'mean_word_length': 3.6319612590799033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '5275440', 'n_tokens_mistral': 1125, 'n_tokens_neox': 1127, 'n_words': 299}