Feat: make lamassu-migrate use async local storage
This commit is contained in:
parent
351d170c31
commit
7bbf2829de
5 changed files with 26 additions and 26 deletions
|
|
@ -4,7 +4,7 @@ const FileStore = require('migrate/lib/file-store')
|
||||||
const db = require('../lib/db')
|
const db = require('../lib/db')
|
||||||
const migrate = require('../lib/migrate')
|
const migrate = require('../lib/migrate')
|
||||||
const options = require('../lib/options')
|
const options = require('../lib/options')
|
||||||
|
const { asyncLocalStorage, defaultStore } = require('../lib/async-storage')
|
||||||
const createMigration = `CREATE TABLE IF NOT EXISTS migrations (
|
const createMigration = `CREATE TABLE IF NOT EXISTS migrations (
|
||||||
id serial PRIMARY KEY,
|
id serial PRIMARY KEY,
|
||||||
data json NOT NULL
|
data json NOT NULL
|
||||||
|
|
@ -21,20 +21,23 @@ const getMigrateFile = () => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
db.$none(createMigration)
|
const store = defaultStore()
|
||||||
.then(() => Promise.all([db.$oneOrNone(select), getMigrateFile()]))
|
asyncLocalStorage.run(store, () => {
|
||||||
.then(([qResult, migrateFile]) => {
|
db.$none(createMigration)
|
||||||
process.env.SKIP_SERVER_LOGS = !(qResult && qResult.data.migrations.find(({ title }) => title === '1572524820075-server-support-logs.js'))
|
.then(() => Promise.all([db.$oneOrNone(select), getMigrateFile()]))
|
||||||
if (!qResult && migrateFile) {
|
.then(([qResult, migrateFile]) => {
|
||||||
return db.$none('insert into migrations (id, data) values (1, $1)', [migrateFile])
|
process.env.SKIP_SERVER_LOGS = !(qResult && qResult.data.migrations.find(({ title }) => title === '1572524820075-server-support-logs.js'))
|
||||||
}
|
if (!qResult && migrateFile) {
|
||||||
})
|
return db.$none('insert into migrations (id, data) values (1, $1)', [migrateFile])
|
||||||
.then(() => migrate.run())
|
}
|
||||||
.then(() => {
|
})
|
||||||
console.log('DB Migration succeeded.')
|
.then(() => migrate.run())
|
||||||
process.exit(0)
|
.then(() => {
|
||||||
})
|
console.log('DB Migration succeeded.')
|
||||||
.catch(err => {
|
process.exit(0)
|
||||||
console.error('DB Migration failed: %s', err)
|
})
|
||||||
process.exit(1)
|
.catch(err => {
|
||||||
})
|
console.error('DB Migration failed: %s', err)
|
||||||
|
process.exit(1)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
const { AsyncLocalStorage } = require('async_hooks')
|
const { AsyncLocalStorage } = require('async_hooks')
|
||||||
const asyncLocalStorage = new AsyncLocalStorage()
|
const asyncLocalStorage = new AsyncLocalStorage()
|
||||||
|
|
||||||
const defaultStore = (a = null) => {
|
const defaultStore = () => {
|
||||||
const store = new Map()
|
const store = new Map()
|
||||||
store.set('schema', 'public')
|
store.set('schema', 'public')
|
||||||
store.set('defaultSchema', 'ERROR_SCHEMA')
|
store.set('defaultSchema', 'ERROR_SCHEMA')
|
||||||
if (a) store.set('a', 'a')
|
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,7 @@ eventBus.subscribe('log', args => {
|
||||||
(id, device_id, message, log_level, meta) values ($1, $2, $3, $4, $5) returning *`
|
(id, device_id, message, log_level, meta) values ($1, $2, $3, $4, $5) returning *`
|
||||||
// need to set AsyncLocalStorage (ALS) for this function as well
|
// need to set AsyncLocalStorage (ALS) for this function as well
|
||||||
// because this module is imported before ALS is set up on app.js
|
// because this module is imported before ALS is set up on app.js
|
||||||
const store = defaultStore(true)
|
const store = defaultStore()
|
||||||
asyncLocalStorage.run(store, () => {
|
asyncLocalStorage.run(store, () => {
|
||||||
db.$one(sql, [uuid.v4(), '', msgToSave, level, meta])
|
db.$one(sql, [uuid.v4(), '', msgToSave, level, meta])
|
||||||
.then(_.mapKeys(_.camelCase))
|
.then(_.mapKeys(_.camelCase))
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ const cookieParser = require('cookie-parser')
|
||||||
const { ApolloServer, AuthenticationError } = require('apollo-server-express')
|
const { ApolloServer, AuthenticationError } = require('apollo-server-express')
|
||||||
const _ = require('lodash/fp')
|
const _ = require('lodash/fp')
|
||||||
|
|
||||||
const asyncLocalStorage = require('../async-storage')
|
const { asyncLocalStorage, defaultStore } = require('../async-storage')
|
||||||
const options = require('../options')
|
const options = require('../options')
|
||||||
const users = require('../users')
|
const users = require('../users')
|
||||||
|
|
||||||
|
|
@ -97,10 +97,8 @@ const certOptions = {
|
||||||
}
|
}
|
||||||
|
|
||||||
function run () {
|
function run () {
|
||||||
const store = new Map()
|
const store = defaultStore()
|
||||||
asyncLocalStorage.run(store, () => {
|
asyncLocalStorage.run(store, () => {
|
||||||
store.set('schema', 'public')
|
|
||||||
store.set('defaultSchema', 'ERROR_SCHEMA')
|
|
||||||
const serverPort = devMode ? 8070 : 443
|
const serverPort = devMode ? 8070 : 443
|
||||||
|
|
||||||
const serverLog = `lamassu-admin-server listening on port ${serverPort}`
|
const serverLog = `lamassu-admin-server listening on port ${serverPort}`
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
const db = require('../lib/db')
|
const db = require('../lib/db')
|
||||||
const sequential = require('promise-sequential')
|
const sequential = require('promise-sequential')
|
||||||
|
|
||||||
module.exports = {multi}
|
module.exports = { multi }
|
||||||
|
|
||||||
function multi (sqls, cb) {
|
function multi (sqls, cb) {
|
||||||
const doQuery = s => {
|
const doQuery = s => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue