2016-12-12 16:52:20 +01:00
|
|
|
/* eslint camelcase: "off" */
|
2020-04-14 22:29:11 +02:00
|
|
|
|
2016-12-12 16:52:20 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const async = require('async');
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
exports.up = function (db, callback) {
|
|
|
|
const populateNewData = (cb) => {
|
2017-06-28 14:10:32 +02:00
|
|
|
db.all(
|
|
|
|
'select name, parameters_template from strategies',
|
|
|
|
(err, results) => {
|
|
|
|
const updateSQL = results
|
|
|
|
.map(({ name, parameters_template }) => {
|
|
|
|
const parameters = [];
|
2021-08-12 15:04:37 +02:00
|
|
|
Object.keys(parameters_template || {}).forEach((p) => {
|
2017-06-28 14:10:32 +02:00
|
|
|
parameters.push({
|
|
|
|
name: p,
|
|
|
|
type: parameters_template[p],
|
|
|
|
description: '',
|
|
|
|
required: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return { name, parameters };
|
|
|
|
})
|
|
|
|
.map(
|
2021-08-12 15:04:37 +02:00
|
|
|
(strategy) => `
|
2016-12-12 16:52:20 +01:00
|
|
|
UPDATE strategies
|
|
|
|
SET parameters='${JSON.stringify(strategy.parameters)}'
|
2020-04-14 22:29:11 +02:00
|
|
|
WHERE name='${strategy.name}';`,
|
2017-06-28 14:10:32 +02:00
|
|
|
)
|
|
|
|
.join('\n');
|
2016-12-12 16:52:20 +01:00
|
|
|
|
2017-06-28 14:10:32 +02:00
|
|
|
db.runSql(updateSQL, cb);
|
2020-04-14 22:29:11 +02:00
|
|
|
},
|
2017-06-28 14:10:32 +02:00
|
|
|
);
|
2016-12-12 16:52:20 +01:00
|
|
|
};
|
|
|
|
|
2017-06-28 14:10:32 +02:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
db.addColumn.bind(db, 'strategies', 'parameters', { type: 'json' }),
|
|
|
|
populateNewData.bind(db),
|
|
|
|
db.removeColumn.bind(db, 'strategies', 'parameters_template'),
|
|
|
|
],
|
2020-04-14 22:29:11 +02:00
|
|
|
callback,
|
2017-06-28 14:10:32 +02:00
|
|
|
);
|
2016-12-12 16:52:20 +01:00
|
|
|
};
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
exports.down = function (db, callback) {
|
|
|
|
const populateOldData = (cb) => {
|
2016-12-12 16:52:20 +01:00
|
|
|
db.all('select name, parameters from strategies', (err, results) => {
|
|
|
|
const updateSQL = results
|
2017-06-28 14:10:32 +02:00
|
|
|
.map(({ name, parameters }) => {
|
|
|
|
const parameters_template = {};
|
2021-08-12 15:04:37 +02:00
|
|
|
parameters.forEach((p) => {
|
2017-06-28 14:10:32 +02:00
|
|
|
parameters_template[p.name] = p.type;
|
|
|
|
});
|
|
|
|
|
|
|
|
return { name, parameters_template };
|
|
|
|
})
|
|
|
|
.map(
|
2021-08-12 15:04:37 +02:00
|
|
|
(strategy) => `
|
2016-12-12 16:52:20 +01:00
|
|
|
UPDATE strategies
|
2017-06-28 14:10:32 +02:00
|
|
|
SET parameters_template='${JSON.stringify(
|
2020-04-14 22:29:11 +02:00
|
|
|
strategy.parameters_template,
|
2017-06-28 14:10:32 +02:00
|
|
|
)}'
|
2020-04-14 22:29:11 +02:00
|
|
|
WHERE name='${strategy.name}';`,
|
2017-06-28 14:10:32 +02:00
|
|
|
)
|
|
|
|
.join('\n');
|
2016-12-12 16:52:20 +01:00
|
|
|
|
|
|
|
db.runSql(updateSQL, cb);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-28 14:10:32 +02:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
db.addColumn.bind(db, 'strategies', 'parameters_template', {
|
|
|
|
type: 'json',
|
|
|
|
}),
|
|
|
|
populateOldData.bind(db),
|
|
|
|
db.removeColumn.bind(db, 'strategies', 'parameters'),
|
|
|
|
],
|
2020-04-14 22:29:11 +02:00
|
|
|
callback,
|
2017-06-28 14:10:32 +02:00
|
|
|
);
|
2016-12-12 16:52:20 +01:00
|
|
|
};
|