1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/migrations/20161212101749-better-strategy-parameter-definitions.js
Ivar Conradi Østhus d01c9d2dac
fix: use airbnb lint rules directly (#583)
This drops usage of finn-eslint rules as they are no
longer maintained.
2020-04-14 22:29:11 +02:00

85 lines
2.6 KiB
JavaScript

/* eslint camelcase: "off" */
'use strict';
const async = require('async');
exports.up = function(db, callback) {
const populateNewData = cb => {
db.all(
'select name, parameters_template from strategies',
(err, results) => {
const updateSQL = results
.map(({ name, parameters_template }) => {
const parameters = [];
Object.keys(parameters_template || {}).forEach(p => {
parameters.push({
name: p,
type: parameters_template[p],
description: '',
required: false,
});
});
return { name, parameters };
})
.map(
strategy => `
UPDATE strategies
SET parameters='${JSON.stringify(strategy.parameters)}'
WHERE name='${strategy.name}';`,
)
.join('\n');
db.runSql(updateSQL, cb);
},
);
};
async.series(
[
db.addColumn.bind(db, 'strategies', 'parameters', { type: 'json' }),
populateNewData.bind(db),
db.removeColumn.bind(db, 'strategies', 'parameters_template'),
],
callback,
);
};
exports.down = function(db, callback) {
const populateOldData = cb => {
db.all('select name, parameters from strategies', (err, results) => {
const updateSQL = results
.map(({ name, parameters }) => {
const parameters_template = {};
parameters.forEach(p => {
parameters_template[p.name] = p.type;
});
return { name, parameters_template };
})
.map(
strategy => `
UPDATE strategies
SET parameters_template='${JSON.stringify(
strategy.parameters_template,
)}'
WHERE name='${strategy.name}';`,
)
.join('\n');
db.runSql(updateSQL, cb);
});
};
async.series(
[
db.addColumn.bind(db, 'strategies', 'parameters_template', {
type: 'json',
}),
populateOldData.bind(db),
db.removeColumn.bind(db, 'strategies', 'parameters'),
],
callback,
);
};