feat: changed old server ports so it can coexists with the new server
feat: removed deleted references from old server feat: created reset and migrate mutations on gql server and correspondent functions on new settings loader feat: created front end for the config migration with reset and migrate functionalities style: add spacing between buttons Signed-off-by: Liordino Neto <liordinoneto@gmail.com>
This commit is contained in:
parent
368d528ee2
commit
df8a1804a3
6 changed files with 144 additions and 65 deletions
|
|
@ -1,21 +1,33 @@
|
|||
const _ = require('lodash/fp')
|
||||
const db = require('./db')
|
||||
const migration = require('./config-migration')
|
||||
|
||||
const OLD_SETTINGS_LOADER_SCHEMA_VERSION = 1
|
||||
const NEW_SETTINGS_LOADER_SCHEMA_VERSION = 2
|
||||
|
||||
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)
|
||||
select $1, $2, $3, $4 where $1 not in (select type from user_config)`
|
||||
function saveAccounts (accountsToSave) {
|
||||
const sql = `update user_config set data = $2, valid = $3, schema_version = $4 where type = $1;
|
||||
insert into user_config (type, data, valid, schema_version)
|
||||
select $1, $2, $3, $4 where $1 not in (select type from user_config)`
|
||||
|
||||
return loadAccounts()
|
||||
.then(currentAccounts => {
|
||||
const newAccounts = _.assign(currentAccounts, accountsToSave)
|
||||
return db.none(sql, ['accounts', { accounts: newAccounts }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
return db.none(accountsSql, ['accounts', { accounts: newAccounts }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
})
|
||||
}
|
||||
function resetAccounts (schemaVersion) {
|
||||
return db.none(
|
||||
accountsSql,
|
||||
[
|
||||
'accounts',
|
||||
{ accounts: NEW_SETTINGS_LOADER_SCHEMA_VERSION ? {} : [] },
|
||||
true,
|
||||
schemaVersion
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
function loadAccounts () {
|
||||
function loadAccounts (schemaVersion) {
|
||||
const sql = `select data
|
||||
from user_config
|
||||
where type=$1
|
||||
|
|
@ -24,22 +36,34 @@ function loadAccounts () {
|
|||
order by id desc
|
||||
limit 1`
|
||||
|
||||
return db.oneOrNone(sql, ['accounts', NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
return db.oneOrNone(sql, ['accounts', schemaVersion || NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
.then(_.compose(_.defaultTo({}), _.get('data.accounts')))
|
||||
}
|
||||
|
||||
const configSql = 'insert into user_config (type, data, valid, schema_version) values ($1, $2, $3, $4)'
|
||||
function saveConfig (config) {
|
||||
const sql = 'insert into user_config (type, data, valid, schema_version) values ($1, $2, $3, $4)'
|
||||
|
||||
console.log(config)
|
||||
return loadLatestConfigOrNone()
|
||||
.then(currentConfig => {
|
||||
const newConfig = _.assign(currentConfig, config)
|
||||
return db.none(sql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
return db.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
})
|
||||
}
|
||||
|
||||
function loadLatest () {
|
||||
return Promise.all([loadLatestConfigOrNone(), loadAccounts()])
|
||||
function resetConfig (schemaVersion) {
|
||||
return db.none(
|
||||
configSql,
|
||||
[
|
||||
'config',
|
||||
{ config: schemaVersion === NEW_SETTINGS_LOADER_SCHEMA_VERSION ? {} : [] },
|
||||
true,
|
||||
schemaVersion
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
function loadLatest (schemaVersion) {
|
||||
return Promise.all([loadLatestConfigOrNone(schemaVersion), loadAccounts(schemaVersion)])
|
||||
.then(([config, accounts]) => ({
|
||||
config,
|
||||
accounts
|
||||
|
|
@ -62,7 +86,7 @@ function loadLatestConfig () {
|
|||
})
|
||||
}
|
||||
|
||||
function loadLatestConfigOrNone () {
|
||||
function loadLatestConfigOrNone (schemaVersion) {
|
||||
const sql = `select data
|
||||
from user_config
|
||||
where type=$1
|
||||
|
|
@ -70,7 +94,7 @@ function loadLatestConfigOrNone () {
|
|||
order by id desc
|
||||
limit 1`
|
||||
|
||||
return db.oneOrNone(sql, ['config', NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
return db.oneOrNone(sql, ['config', schemaVersion || NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
.then(row => row ? row.data.config : {})
|
||||
}
|
||||
|
||||
|
|
@ -103,12 +127,26 @@ function load (versionId) {
|
|||
}))
|
||||
}
|
||||
|
||||
function migrate () {
|
||||
return loadLatest(OLD_SETTINGS_LOADER_SCHEMA_VERSION)
|
||||
.then(res => {
|
||||
const migrated = migration.migrate(res.config, res.accounts)
|
||||
saveConfig(migrated.config)
|
||||
saveAccounts(migrated.accounts)
|
||||
|
||||
return migrated
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
saveConfig,
|
||||
resetConfig,
|
||||
saveAccounts,
|
||||
resetAccounts,
|
||||
loadAccounts,
|
||||
loadLatest,
|
||||
loadLatestConfig,
|
||||
loadLatestConfigOrNone,
|
||||
load
|
||||
load,
|
||||
migrate
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue