1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/migrations/20161212101749-better-strategy-parameter-definitions.js
Simen Bekkhus d1ec402066 Lint commit (#239)
* Upgrade eslint configs to get rid of peer warning

* Add pre-commit hook to format code

* Lint whole project, not just lib
2017-06-28 14:10:32 +02:00

84 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
);
};