refactor migration
This commit is contained in:
parent
272405b4b1
commit
916a3629a8
7 changed files with 84 additions and 58 deletions
|
|
@ -1,50 +1,8 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const assert = require('assert')
|
const applyDefaults = require('../lib/apply-defaults')
|
||||||
const _ = require('lodash/fp')
|
|
||||||
|
|
||||||
// const db = require('../lib/db')
|
applyDefaults.run()
|
||||||
const settingsLoader = require('../lib/settings-loader')
|
|
||||||
const schema = require('../lamassu-schema.json')
|
|
||||||
const newFields = []
|
|
||||||
|
|
||||||
const DEFAULT_CRYPTO = _.first(_.find(['code', 'cryptoCurrencies'], schema.fields).default)
|
|
||||||
|
|
||||||
assert(DEFAULT_CRYPTO)
|
|
||||||
|
|
||||||
Promise.resolve()
|
|
||||||
.then(() => {
|
|
||||||
schema.groups.forEach(group => {
|
|
||||||
return group.fields.forEach(fieldCode => {
|
|
||||||
const field = schema.fields.find(r => r.code === fieldCode)
|
|
||||||
if (!field) throw new Error('No such field: ' + fieldCode)
|
|
||||||
if (_.isNil(field.default)) return
|
|
||||||
if (group.machineScope === 'specific') return
|
|
||||||
|
|
||||||
const crypto = group.cryptoScope === 'specific'
|
|
||||||
? DEFAULT_CRYPTO
|
|
||||||
: 'global'
|
|
||||||
|
|
||||||
return newFields.push({
|
|
||||||
fieldLocator: {
|
|
||||||
fieldScope: {
|
|
||||||
crypto,
|
|
||||||
machine: 'global'
|
|
||||||
},
|
|
||||||
code: fieldCode,
|
|
||||||
fieldType: field.fieldType,
|
|
||||||
fieldClass: field.fieldClass
|
|
||||||
},
|
|
||||||
fieldValue: {
|
|
||||||
fieldType: field.fieldType,
|
|
||||||
value: field.default
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
return settingsLoader.save(newFields)
|
|
||||||
})
|
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log('Success.')
|
console.log('Success.')
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,13 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const path = require('path')
|
const migrate = require('../lib/migrate')
|
||||||
const migrate = require('migrate')
|
|
||||||
const options = require('../lib/options')
|
|
||||||
|
|
||||||
const migrateDir = path.resolve(__dirname, '..', 'migrations')
|
|
||||||
const migration = migrate.load(options.migrateStatePath, migrateDir)
|
|
||||||
|
|
||||||
console.log(options.migrateStatePath)
|
|
||||||
migration.up(err => {
|
|
||||||
if (err) {
|
|
||||||
console.error('DB Migration failed: %s', err)
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
migrate.run()
|
||||||
|
.then(() => {
|
||||||
console.log('DB Migration succeeded.')
|
console.log('DB Migration succeeded.')
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
})
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error('DB Migration failed: %s', err)
|
||||||
|
process.exit(1)
|
||||||
|
})
|
||||||
|
|
|
||||||
48
lib/apply-defaults.js
Normal file
48
lib/apply-defaults.js
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
const assert = require('assert')
|
||||||
|
const _ = require('lodash/fp')
|
||||||
|
|
||||||
|
const settingsLoader = require('../lib/settings-loader')
|
||||||
|
const schema = require('../lamassu-schema.json')
|
||||||
|
const newFields = []
|
||||||
|
|
||||||
|
const DEFAULT_CRYPTO = _.first(_.find(['code', 'cryptoCurrencies'], schema.fields).default)
|
||||||
|
|
||||||
|
assert(DEFAULT_CRYPTO)
|
||||||
|
|
||||||
|
module.exports = {run}
|
||||||
|
|
||||||
|
function run () {
|
||||||
|
return Promise.resolve()
|
||||||
|
.then(() => {
|
||||||
|
schema.groups.forEach(group => {
|
||||||
|
return group.fields.forEach(fieldCode => {
|
||||||
|
const field = schema.fields.find(r => r.code === fieldCode)
|
||||||
|
if (!field) throw new Error('No such field: ' + fieldCode)
|
||||||
|
if (_.isNil(field.default)) return
|
||||||
|
if (group.machineScope === 'specific') return
|
||||||
|
|
||||||
|
const crypto = group.cryptoScope === 'specific'
|
||||||
|
? DEFAULT_CRYPTO
|
||||||
|
: 'global'
|
||||||
|
|
||||||
|
return newFields.push({
|
||||||
|
fieldLocator: {
|
||||||
|
fieldScope: {
|
||||||
|
crypto,
|
||||||
|
machine: 'global'
|
||||||
|
},
|
||||||
|
code: fieldCode,
|
||||||
|
fieldType: field.fieldType,
|
||||||
|
fieldClass: field.fieldClass
|
||||||
|
},
|
||||||
|
fieldValue: {
|
||||||
|
fieldType: field.fieldType,
|
||||||
|
value: field.default
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return settingsLoader.save(newFields)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,6 @@ module.exports = {setup}
|
||||||
function setup (dataDir) {
|
function setup (dataDir) {
|
||||||
const coinRec = coinUtils.getCryptoCurrency('ETH')
|
const coinRec = coinUtils.getCryptoCurrency('ETH')
|
||||||
common.firewall([coinRec.defaultPort])
|
common.firewall([coinRec.defaultPort])
|
||||||
const cmd = `/usr/local/bin/${coinRec.daemon} --datadir "${dataDir}" --cache 500`
|
const cmd = `/usr/local/bin/${coinRec.daemon} --datadir "${dataDir}" --cache 500 --rpc`
|
||||||
common.writeSupervisorConfig(coinRec, cmd)
|
common.writeSupervisorConfig(coinRec, cmd)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
lib/migrate.js
Normal file
19
lib/migrate.js
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
const path = require('path')
|
||||||
|
const migrate = require('migrate')
|
||||||
|
|
||||||
|
const options = require('./options')
|
||||||
|
|
||||||
|
const migrateDir = path.resolve(__dirname, '..', 'migrations')
|
||||||
|
const migration = migrate.load(options.migrateStatePath, migrateDir)
|
||||||
|
|
||||||
|
module.exports = {run}
|
||||||
|
|
||||||
|
function run () {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
migration.up(err => {
|
||||||
|
if (err) return reject(err)
|
||||||
|
return resolve(0)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -5,6 +5,11 @@ const _ = require('lodash/fp')
|
||||||
const argv = require('minimist')(process.argv.slice(2))
|
const argv = require('minimist')(process.argv.slice(2))
|
||||||
|
|
||||||
function load () {
|
function load () {
|
||||||
|
if (process.env.LAMASSU_CONFIG) {
|
||||||
|
const configPath = process.env.LAMASSU_CONFIG
|
||||||
|
return JSON.parse(fs.readFileSync(configPath))
|
||||||
|
}
|
||||||
|
|
||||||
if (argv.f) {
|
if (argv.f) {
|
||||||
const configPath = argv.f
|
const configPath = argv.f
|
||||||
return JSON.parse(fs.readFileSync(configPath))
|
return JSON.parse(fs.readFileSync(configPath))
|
||||||
|
|
|
||||||
|
|
@ -135,6 +135,8 @@ function settings () {
|
||||||
function save (config) {
|
function save (config) {
|
||||||
const sql = 'insert into user_config (type, data, valid) values ($1, $2, $3)'
|
const sql = 'insert into user_config (type, data, valid) values ($1, $2, $3)'
|
||||||
|
|
||||||
|
console.log('DEBUG800: %s', sql)
|
||||||
|
|
||||||
return configValidate.validate(config)
|
return configValidate.validate(config)
|
||||||
.then(() => db.none(sql, ['config', {config}, true]))
|
.then(() => db.none(sql, ['config', {config}, true]))
|
||||||
.catch(() => db.none(sql, ['config', {config}, false]))
|
.catch(() => db.none(sql, ['config', {config}, false]))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue