load latest *good* config

This commit is contained in:
Josh Harvey 2017-04-24 18:52:51 +03:00
parent dd7d06f38b
commit 5f0b70ca42
4 changed files with 31 additions and 13 deletions

View file

@ -82,12 +82,20 @@ function loadLatestConfig () {
const sql = `select data
from user_config
where type=$1
and valid
order by id desc
limit 1`
return db.oneOrNone(sql, ['config'])
.then(row => row ? row.data.config : [])
return db.one(sql, ['config'])
.then(row => row.data.config)
.then(validate)
.catch(err => {
if (err.name === 'QueryResultError') {
throw new Error('lamassu-server is not configured')
}
throw err
})
}
function checkConstraint (entry, constraint) {
@ -114,9 +122,11 @@ function validateEntry (entry) {
return _.every(constraint => validateConstraint(entry, constraint), validations)
}
function isValid (config) {
return _.every(validateEntry, config)
}
function validate (config) {
const isValid = _.every(validateEntry, config)
if (!isValid) throw new Error('Invalid config')
if (!isValid(config)) throw new Error('Invalid config')
return config
}
@ -136,8 +146,8 @@ function settings () {
}
function save (config) {
const sql = 'insert into user_config (type, data) values ($1, $2)'
return db.none(sql, ['config', config])
const sql = 'insert into user_config (type, data, valid) values ($1, $2, $3)'
return db.none(sql, ['config', {config}, isValid(config)])
}
module.exports = {