setupSqliteDB = function (db, {setup, migrations}, logTag) {
  return new Promise((resolve, reject) => {
    // configure connection
    db.run('PRAGMA foreign_keys = ON;', (err) => {
      if (err) {
        console.error('Failed to enable FK support in SQLite', err)
      }
    })

    // run migrations
    db.get('PRAGMA user_version;', (err, res) => {
      if (err) return reject(err)

      var version = (res && res.user_version) ? +res.user_version : 0
      var neededMigrations = (version === 0 && setup) ? [setup] : migrations.slice(version)
      if (neededMigrations.length == 0) { return resolve() }

      logger.info(`${logTag} Database at version ${version}; Running ${neededMigrations.length} migrations`)
      db.run('PRAGMA SYNCHRONOUS = OFF;') // turn off fsync to speed up migrations
      runNeededMigrations()
      function runNeededMigrations (err) {
        if (err) {
          logger.error(`${logTag} Failed migration`)
          console.log(err)
          db.run('PRAGMA SYNCHRONOUS = FULL;') // turn fsync back on
          return reject(err)
        }

        var migration = neededMigrations.shift()
        if (!migration) {
          // done
          db.run('PRAGMA SYNCHRONOUS = FULL;') // turn fsync back on
          resolve()
          return logger.info(`${logTag} Database migrations completed without error`)
        }

        migration(runNeededMigrations)
      }
    })
  })
}
Configures SQLite db and runs needed migrations.
@param {any} db
@param {Object} opts
@param {Function} [opts.setup]
@param {Function[]} [opts.migrations]