lamassu-server/migrations/migrate-tools.js
Neal Conner fd659c88c6 Fix migrate-tools for multiple devices (#113)
Don't fail with duplicate key errors if there is more than one row already in the devices table, since it attempts to update the device_id column.
2018-05-03 11:46:10 +03:00

16 lines
566 B
JavaScript

const pgp = require('pg-promise')()
const _ = require('lodash/fp')
const settingsLoader = require('../lib/settings-loader')
const machineLoader = require('../lib/machine-loader')
module.exports = {migrateNames}
function migrateNames () {
const cs = new pgp.helpers.ColumnSet(['?device_id', 'name'], {table: 'devices'})
return settingsLoader.loadLatest()
.then(r => machineLoader.getMachineNames(r.config))
.then(_.map(r => ({device_id: r.deviceId, name: r.name})))
.then(data => pgp.helpers.update(data, cs) + ' WHERE t.device_id=v.device_id')
}