chore: SQL formatting

This commit is contained in:
siiky 2024-09-12 16:38:37 +01:00
parent 083a7764d2
commit 446ac9f8db

View file

@ -56,9 +56,10 @@ const addTermsHash = configs => {
)(terms) )(terms)
} }
const accountsSql = `update user_config set data = $2, valid = $3, schema_version = $4 where type = $1; const accountsSql = `UPDATE user_config SET data = $2, valid = $3, schema_version = $4 WHERE type = $1;
insert into user_config (type, data, valid, schema_version) INSERT INTO user_config (type, data, valid, schema_version)
select $1, $2, $3, $4 where $1 not in (select type from user_config)` SELECT $1, $2, $3, $4 WHERE $1 NOT IN (SELECT type FROM user_config)`
function saveAccounts (accounts) { function saveAccounts (accounts) {
return Promise.all([loadAccounts(), getOperatorId('middleware')]) return Promise.all([loadAccounts(), getOperatorId('middleware')])
.then(([currentAccounts, operatorId]) => { .then(([currentAccounts, operatorId]) => {
@ -92,13 +93,13 @@ function resetAccounts (schemaVersion) {
} }
function loadAccounts (schemaVersion) { function loadAccounts (schemaVersion) {
const sql = `select data const sql = `SELECT data
from user_config FROM user_config
where type=$1 WHERE type = $1
and schema_version=$2 AND schema_version = $2
and valid AND valid
order by id desc ORDER BY id DESC
limit 1` LIMIT 1`
return db.oneOrNone(sql, ['accounts', schemaVersion || NEW_SETTINGS_LOADER_SCHEMA_VERSION]) return db.oneOrNone(sql, ['accounts', schemaVersion || NEW_SETTINGS_LOADER_SCHEMA_VERSION])
.then(_.compose(_.defaultTo({}), _.get('data.accounts'))) .then(_.compose(_.defaultTo({}), _.get('data.accounts')))
@ -116,7 +117,7 @@ function showAccounts (schemaVersion) {
}) })
} }
const configSql = 'insert into user_config (type, data, valid, schema_version) values ($1, $2, $3, $4)' const configSql = 'INSERT INTO user_config (type, data, valid, schema_version) VALUES ($1, $2, $3, $4)'
function saveConfig (config) { function saveConfig (config) {
return Promise.all([loadLatestConfigOrNone(), getOperatorId('middleware')]) return Promise.all([loadLatestConfigOrNone(), getOperatorId('middleware')])
.then(([currentConfig, operatorId]) => { .then(([currentConfig, operatorId]) => {
@ -170,13 +171,13 @@ function loadLatest (schemaVersion) {
} }
function loadLatestConfig () { function loadLatestConfig () {
const sql = `select data const sql = `SELECT data
from user_config FROM user_config
where type=$1 WHERE type = $1
and schema_version=$2 AND schema_version = $2
and valid AND valid
order by id desc ORDER BY id DESC
limit 1` LIMIT 1`
return db.one(sql, ['config', NEW_SETTINGS_LOADER_SCHEMA_VERSION]) return db.one(sql, ['config', NEW_SETTINGS_LOADER_SCHEMA_VERSION])
.then(row => row ? row.data.config : {}) .then(row => row ? row.data.config : {})
@ -186,36 +187,36 @@ function loadLatestConfig () {
} }
function loadLatestConfigOrNoneReturningVersion (schemaVersion) { function loadLatestConfigOrNoneReturningVersion (schemaVersion) {
const sql = `select data, id const sql = `SELECT data, id
from user_config FROM user_config
where type=$1 WHERE type = $1
and schema_version=$2 AND schema_version = $2
order by id desc ORDER BY id DESC
limit 1` LIMIT 1`
return db.oneOrNone(sql, ['config', schemaVersion || NEW_SETTINGS_LOADER_SCHEMA_VERSION]) return db.oneOrNone(sql, ['config', schemaVersion || NEW_SETTINGS_LOADER_SCHEMA_VERSION])
.then(row => row ? { config: row.data.config, version: row.id } : {}) .then(row => row ? { config: row.data.config, version: row.id } : {})
} }
function loadLatestConfigOrNone (schemaVersion) { function loadLatestConfigOrNone (schemaVersion) {
const sql = `select data const sql = `SELECT data
from user_config FROM user_config
where type=$1 WHERE type = $1
and schema_version=$2 AND schema_version = $2
order by id desc ORDER BY id DESC
limit 1` LIMIT 1`
return db.oneOrNone(sql, ['config', schemaVersion || NEW_SETTINGS_LOADER_SCHEMA_VERSION]) return db.oneOrNone(sql, ['config', schemaVersion || NEW_SETTINGS_LOADER_SCHEMA_VERSION])
.then(row => row ? row.data.config : {}) .then(row => row ? row.data.config : {})
} }
function loadConfig (versionId) { function loadConfig (versionId) {
const sql = `select data const sql = `SELECT data
from user_config FROM user_config
where id=$1 WHERE id = $1
and type=$2 AND type = $2
and schema_version=$3 AND schema_version = $3
and valid` AND valid`
return db.one(sql, [versionId, 'config', NEW_SETTINGS_LOADER_SCHEMA_VERSION]) return db.one(sql, [versionId, 'config', NEW_SETTINGS_LOADER_SCHEMA_VERSION])
.then(row => row.data.config) .then(row => row.data.config)