diff --git a/lib/new-settings-loader.js b/lib/new-settings-loader.js index b9a5cefb..9d4d5d50 100644 --- a/lib/new-settings-loader.js +++ b/lib/new-settings-loader.js @@ -106,15 +106,20 @@ function showAccounts (schemaVersion) { }) } -const configSql = 'INSERT INTO user_config (type, data, valid, schema_version) VALUES ($1, $2, $3, $4)' +const insertConfigRow = (dbOrTx, data) => + dbOrTx.none( + "INSERT INTO user_config (type, data, valid, schema_version) VALUES ('config', $1, TRUE, $2)", + [data, NEW_SETTINGS_LOADER_SCHEMA_VERSION] + ) + function saveConfig (config) { return Promise.all([loadLatestConfigOrNone(), getOperatorId('middleware')]) .then(([currentConfig, operatorId]) => { const newConfig = addTermsHash(_.assign(currentConfig, config)) - return db.tx(t => { - return t.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION]) + return db.tx(t => + insertConfigRow(t, { config: newConfig }) .then(() => t.none('NOTIFY $1:name, $2', ['reload', JSON.stringify({ schema: asyncLocalStorage.getStore().get('schema'), operatorId })])) - }).catch(console.error) + ).catch(console.error) }) } @@ -122,10 +127,10 @@ function removeFromConfig (fields) { return Promise.all([loadLatestConfigOrNone(), getOperatorId('middleware')]) .then(([currentConfig, operatorId]) => { const newConfig = _.omit(fields, currentConfig) - return db.tx(t => { - return t.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION]) + return db.tx(t => + insertConfigRow(t, { config: newConfig }) .then(() => t.none('NOTIFY $1:name, $2', ['reload', JSON.stringify({ schema: asyncLocalStorage.getStore().get('schema'), operatorId })])) - }).catch(console.error) + ).catch(console.error) }) } @@ -133,7 +138,7 @@ function migrationSaveConfig (config) { return loadLatestConfigOrNone() .then(currentConfig => { const newConfig = _.assign(currentConfig, config) - return db.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION]) + return insertConfigRow(db, { config: newConfig }) .catch(console.error) }) }