1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/migrations/20170211090541-add-default-strategies.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

72 lines
2.0 KiB
JavaScript

'use strict';
const strategies = require('./default-strategies.json');
const async = require('async');
function insertStrategySQL(strategy) {
return `
INSERT INTO strategies (name, description, parameters, built_in)
SELECT '${strategy.name}', '${strategy.description}', '${JSON.stringify(
strategy.parameters
)}', 1
WHERE
NOT EXISTS (
SELECT name FROM strategies WHERE name = '${strategy.name}'
);`;
}
function insertEventsSQL(strategy) {
return `
INSERT INTO events (type, created_by, data)
SELECT 'strategy-created', 'migration', '${JSON.stringify(strategy)}'
WHERE
NOT EXISTS (
SELECT name FROM strategies WHERE name = '${strategy.name}'
);`;
}
function removeEventsSQL(strategy) {
return `
INSERT INTO events (type, created_by, data)
SELECT 'strategy-deleted', 'migration', '${JSON.stringify(strategy)}'
WHERE
EXISTS (
SELECT name FROM strategies WHERE name = '${strategy.name}' AND built_in = 1
);`;
}
function removeStrategySQL(strategy) {
return `
DELETE FROM strategies
WHERE name = '${strategy.name}' AND built_in = 1`;
}
exports.up = function(db, callback) {
const insertStrategies = strategies.map(s => cb => {
async.series(
[
db.runSql.bind(db, insertEventsSQL(s)),
db.runSql.bind(db, insertStrategySQL(s)),
],
cb
);
});
async.series(insertStrategies, callback);
};
exports.down = function(db, callback) {
const removeStrategies = strategies
.filter(s => s.name !== 'default')
.map(s => cb => {
async.series(
[
db.runSql.bind(db, removeEventsSQL(s)),
db.runSql.bind(db, removeStrategySQL(s)),
],
cb
);
});
async.series(removeStrategies, callback);
};