filo_caspar/script/setup.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-04-10 08:37:05 +00:00
#!/usr/bin/env node
/* eslint-disable no-console */
'use strict'
const _ = require('lodash')
2018-07-15 03:19:58 +00:00
const config = require('../config')
let log = require('../log').default
2016-04-10 08:37:05 +00:00
// This is important for setup to run cleanly.
let knexConfig = _.cloneDeep(config.get('knex'))
knexConfig.pool = { min: 1, max: 1 }
let knex = require('knex')(knexConfig)
log.info(knexConfig, 'Connected to database')
let setup = module.exports = () =>
knex.migrate.latest({
2018-07-15 03:19:58 +00:00
directory: './migrations',
2016-04-10 08:37:05 +00:00
})
.then((result) => {
if (result[1].length === 0) {
return log.info('Database is up to date')
}
for (let i = 0; i < result[1].length; i++) {
log.info('Applied migration from', result[1][i].substr(result[1][i].lastIndexOf('\\') + 1))
}
return knex.destroy()
})
if (require.main === module) {
// Since we're running this as a script, we should output
// directly to the console.
log = console
log.info = console.log.bind(console)
setup().then(() => {
log.info('Setup ran successfully.')
}).catch((error) => {
log.error(error, 'Error while running setup.')
}).then(() => {
process.exit(0)
})
}