fix migration so it doesn't error out on existing devices
This commit is contained in:
parent
2b3e49fdab
commit
d3c2448a12
2 changed files with 29 additions and 5 deletions
|
|
@ -1,12 +1,20 @@
|
|||
const db = require('./db')
|
||||
const migrateTools = require('./migrate-tools')
|
||||
|
||||
exports.up = function (next) {
|
||||
const sql = [
|
||||
'alter table devices add column name text not null'
|
||||
]
|
||||
db.multi(sql, next)
|
||||
return migrateTools.migrateNames()
|
||||
.then(updateSql => {
|
||||
const sql = [
|
||||
'alter table devices add column name text',
|
||||
updateSql,
|
||||
'alter table devices alter column name set not null'
|
||||
]
|
||||
|
||||
return db.multi(sql, next)
|
||||
})
|
||||
}
|
||||
|
||||
exports.down = function (next) {
|
||||
next()
|
||||
const sql = ['alter table devices drop column name']
|
||||
db.multi(sql, next)
|
||||
}
|
||||
|
|
|
|||
16
migrations/migrate-tools.js
Normal file
16
migrations/migrate-tools.js
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue