update remaining code for versioned config

This commit is contained in:
Josh Harvey 2016-12-09 11:36:29 +02:00
parent 2c6177bff9
commit e6cd0d9473
6 changed files with 25 additions and 20 deletions

View file

@ -1,3 +1,2 @@
{
"typescript.tsdk": "./node_modules/typescript/lib"
}
}

View file

@ -4,6 +4,7 @@ const path = require('path')
const options = require('../options')
const db = require('../db')
const config = require('./config')
const accountRoot = options.pluginPath
const schemas = {}
@ -37,8 +38,7 @@ function selectedAccounts () {
v.fieldValue.value
const mapSchema = code => schemas[code]
return db.oneOrNone('select data from user_config where type=$1', ['config'])
.then(row => row && row.data)
return config.fetchConfig()
.then(data => {
if (!data) return []

View file

@ -19,8 +19,11 @@ function fetchSchema () {
.then(JSON.parse)
}
function dbFetchConfig () {
return db.oneOrNone('select data from user_config where type=$1', ['config'])
function fetchConfig () {
const sql = `select data from user_config where type=$1
order by id desc limit 1`
return db.oneOrNone(sql, ['config'])
.then(row => row && row.data)
}
@ -108,7 +111,7 @@ const fetchMachines = () => machines.getMachines()
.then(machineList => machineList.map(r => r.deviceId))
function validateConfig () {
return Promise.all([fetchSchema(), dbFetchConfig(), fetchMachines()])
return Promise.all([fetchSchema(), fetchConfig(), fetchMachines()])
.then(([schema, configRec, machineList]) => {
const config = configRec ? configRec.config : []
const cryptos = getCryptos(config, machineList)
@ -127,7 +130,7 @@ function validateConfig () {
function fetchConfigGroup (code) {
const fieldLocatorCodeEq = R.pathEq(['fieldLocator', 'code'])
return Promise.all([fetchSchema(), fetchData(), dbFetchConfig(), fetchMachines()])
return Promise.all([fetchSchema(), fetchData(), fetchConfig(), fetchMachines()])
.then(([schema, data, config, machineList]) => {
const configValues = config ? config.config : []
const groupSchema = schema.groups.find(r => r.code === code)
@ -211,18 +214,16 @@ function fetchData () {
}
function dbSaveConfig (config) {
return db.none('update user_config set data=$1 where type=$2', [config, 'config'])
const sql = 'insert into user_config (type, data) values ($1, $2)'
return db.none(sql, ['config', config])
}
function saveConfigGroup (results) {
return dbFetchConfig()
.then(config => {
return config
? Promise.resolve(config)
: db.none('insert into user_config (type, data) values ($1, $2)', ['config', {config: []}])
.then(dbFetchConfig)
})
if (results.values.length === 0) return fetchConfigGroup(results.groupCode)
return fetchConfig()
.then(config => {
if (!config) config = {config: []}
const oldValues = config.config
results.values.forEach(newValue => {
@ -258,5 +259,6 @@ function saveConfigGroup (results) {
module.exports = {
fetchConfigGroup,
saveConfigGroup,
validateConfig
validateConfig,
fetchConfig
}

View file

@ -58,8 +58,8 @@ function settings () {
}
function save (config) {
const sql = 'update user_config set data=$1 where type=$2'
return db.none(sql, [{config}, 'config'])
const sql = 'insert into user_config (type, data) values ($1, $2)'
return db.none(sql, ['config', config])
}
module.exports = {

View file

@ -3,6 +3,7 @@ var db = require('./db')
exports.up = function (next) {
var sql = [
'alter table devices add column user_config_id int',
'alter table user_config add column created timestamptz NOT NULL default now()',
`ALTER TABLE devices ADD CONSTRAINT user_config_id
FOREIGN KEY (user_config_id)
REFERENCES user_config (id)`

View file

@ -7,7 +7,10 @@ function pp (o) {
}
function dbFetchConfig () {
return db.oneOrNone('select data from user_config where type=$1', ['config'])
return db.oneOrNone(
'select data from user_config where type=$1 order by id desc limit 1',
['config']
)
.then(row => row && row.data)
}