feat: created migration to add a schema_version column on the user_config table
feat: adapted the new settings loader to work with the db instead of the json file fix: fixed the schema migration file fix: updated accounts to work with the db fix: fetch only data with the new schema version fix: change the account saving process so there's no more than one account record chore: removed unnecessary TODO comments fix: replaced an error throwing with a Promise reject fix: when looking for the latest config, don't limit it to the new ones fix: fix function names on gql server fix: remove duplicate import fix: return an empty object when there's no schema_version 2 config yet
This commit is contained in:
parent
29502ee303
commit
6e356217ae
3 changed files with 107 additions and 54 deletions
|
|
@ -258,8 +258,8 @@ const resolvers = {
|
||||||
serverLogs.getServerLogs(from, until, limit, offset),
|
serverLogs.getServerLogs(from, until, limit, offset),
|
||||||
transactions: (...[, { from, until, limit, offset }]) =>
|
transactions: (...[, { from, until, limit, offset }]) =>
|
||||||
transactions.batch(from, until, limit, offset),
|
transactions.batch(from, until, limit, offset),
|
||||||
config: () => settingsLoader.getConfig(),
|
config: () => settingsLoader.loadLatestConfig(),
|
||||||
accounts: () => settingsLoader.getAccounts()
|
accounts: () => settingsLoader.loadAccounts()
|
||||||
},
|
},
|
||||||
Mutation: {
|
Mutation: {
|
||||||
machineAction: (...[, { deviceId, action, cassette1, cassette2 }]) => machineAction({ deviceId, action, cassette1, cassette2 }),
|
machineAction: (...[, { deviceId, action, cassette1, cassette2 }]) => machineAction({ deviceId, action, cassette1, cassette2 }),
|
||||||
|
|
|
||||||
|
|
@ -1,71 +1,111 @@
|
||||||
const _ = require('lodash/fp')
|
const _ = require('lodash/fp')
|
||||||
const low = require('lowdb')
|
const db = require('./db')
|
||||||
const FileAsync = require('lowdb/adapters/FileAsync')
|
|
||||||
|
|
||||||
const adapter = new FileAsync('db.json')
|
const NEW_SETTINGS_LOADER_SCHEMA_VERSION = 2
|
||||||
let db = null
|
|
||||||
|
|
||||||
// TODO new-admin save to actual db, like the old settings
|
|
||||||
low(adapter).then(it => {
|
|
||||||
db = it
|
|
||||||
})
|
|
||||||
|
|
||||||
function saveAccounts (accountsToSave) {
|
function saveAccounts (accountsToSave) {
|
||||||
const currentState = db.getState() || {}
|
const sql = `update user_config set data = $2, valid = $3, schema_version = $4 where type = $1;
|
||||||
const accounts = currentState.accounts || {}
|
insert into user_config (type, data, valid, schema_version)
|
||||||
|
select $1, $2, $3, $4 where $1 not in (select type from user_config)`
|
||||||
|
|
||||||
const newAccounts = _.assign(accounts)(accountsToSave)
|
return loadAccounts()
|
||||||
|
.then(currentAccounts => {
|
||||||
const newState = _.set('accounts', newAccounts, currentState)
|
const newAccounts = _.assign(currentAccounts, accountsToSave)
|
||||||
db.setState(newState)
|
return db.none(sql, ['accounts', { accounts: newAccounts }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||||
return db.write()
|
})
|
||||||
.then(() => newState.accounts)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAccounts () {
|
function loadAccounts () {
|
||||||
const state = db.getState()
|
const toFields = fieldArr => _.fromPairs(_.map(r => [r.code, r.value], fieldArr))
|
||||||
return state ? state.accounts : null
|
const toPairs = r => [r.code, toFields(r.fields)]
|
||||||
|
|
||||||
|
const sql = `select data
|
||||||
|
from user_config
|
||||||
|
where type=$1
|
||||||
|
and schema_version=$2
|
||||||
|
and valid
|
||||||
|
order by id desc
|
||||||
|
limit 1`
|
||||||
|
|
||||||
|
return db.oneOrNone(sql, ['accounts', NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||||
|
.then(row => {
|
||||||
|
if (!row) return {}
|
||||||
|
return _.fromPairs(_.map(toPairs, row.data.accounts))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveConfig (config) {
|
function saveConfig (config) {
|
||||||
const currentState = db.getState() || {}
|
const sql = 'insert into user_config (type, data, valid, schema_version) values ($1, $2, $3, $4)'
|
||||||
const currentConfig = currentState.config || {}
|
|
||||||
const newConfig = _.assign(currentConfig, config)
|
|
||||||
|
|
||||||
const newState = _.set('config', newConfig, currentState)
|
return loadLatestConfig()
|
||||||
db.setState(newState)
|
.then(currentConfig => {
|
||||||
return db.write()
|
const newConfig = _.assign(currentConfig, config)
|
||||||
.then(() => newState.config)
|
return db.none(sql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||||
}
|
})
|
||||||
|
|
||||||
function getConfig () {
|
|
||||||
const state = db.getState()
|
|
||||||
return (state && state.config) || {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadLatest () {
|
function loadLatest () {
|
||||||
return new Promise((resolve) => {
|
return Promise.all([loadLatestConfig(), loadAccounts()])
|
||||||
if (!db) {
|
.then(([config, accounts]) => ({
|
||||||
setTimeout(() => {
|
config,
|
||||||
return resolve(db.getState())
|
accounts
|
||||||
}, 1000)
|
}))
|
||||||
} else {
|
}
|
||||||
return resolve(db.getState())
|
|
||||||
}
|
function loadLatestConfig () {
|
||||||
})
|
const sql = `select data
|
||||||
|
from user_config
|
||||||
|
where type=$1
|
||||||
|
and schema_version=$2
|
||||||
|
and valid
|
||||||
|
order by id desc
|
||||||
|
limit 1`
|
||||||
|
|
||||||
|
return db.oneOrNone(sql, ['config', NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||||
|
.then(row => !row ? {} : row.data.config)
|
||||||
|
.catch(err => {
|
||||||
|
if (err.name === 'QueryResultError') {
|
||||||
|
throw new Error('lamassu-server is not configured')
|
||||||
|
}
|
||||||
|
|
||||||
|
throw err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadConfig (versionId) {
|
||||||
|
const sql = `select data
|
||||||
|
from user_config
|
||||||
|
where id=$1
|
||||||
|
and type=$2
|
||||||
|
and schema_version=$3
|
||||||
|
and valid`
|
||||||
|
|
||||||
|
return db.one(sql, [versionId, 'config', NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||||
|
.then(row => row.data.config)
|
||||||
|
.catch(err => {
|
||||||
|
if (err.name === 'QueryResultError') {
|
||||||
|
throw new Error('No such config version: ' + versionId)
|
||||||
|
}
|
||||||
|
|
||||||
|
throw err
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO new-admin: grab correct version
|
|
||||||
function load (versionId) {
|
function load (versionId) {
|
||||||
return new Promise((resolve) => {
|
if (!versionId) Promise.reject('versionId is required')
|
||||||
if (!db) {
|
|
||||||
setTimeout(() => {
|
return Promise.all([loadConfig(versionId), loadAccounts()])
|
||||||
return resolve(db.getState())
|
.then(([config, accounts]) => ({
|
||||||
}, 1000)
|
config,
|
||||||
} else {
|
accounts
|
||||||
return resolve(db.getState())
|
}))
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { getConfig, saveConfig, saveAccounts, getAccounts, loadLatest, load }
|
module.exports = {
|
||||||
|
saveConfig,
|
||||||
|
saveAccounts,
|
||||||
|
loadAccounts,
|
||||||
|
loadLatest,
|
||||||
|
loadLatestConfig,
|
||||||
|
load
|
||||||
|
}
|
||||||
|
|
|
||||||
13
migrations/1595295132361-schema-version-on-user-config.js
Normal file
13
migrations/1595295132361-schema-version-on-user-config.js
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
const db = require('./db')
|
||||||
|
|
||||||
|
module.exports.up = function (next) {
|
||||||
|
var sql = [
|
||||||
|
'alter table user_config add column schema_version smallint DEFAULT 1'
|
||||||
|
]
|
||||||
|
|
||||||
|
db.multi(sql, next)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.down = function (next) {
|
||||||
|
next()
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue