2017-02-11 11:17:11 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const strategies = require('./default-strategies.json');
|
|
|
|
const async = require('async');
|
|
|
|
|
2017-06-28 14:10:32 +02:00
|
|
|
function insertStrategySQL(strategy) {
|
2017-02-11 11:17:11 +01:00
|
|
|
return `
|
|
|
|
INSERT INTO strategies (name, description, parameters, built_in)
|
2017-06-28 14:10:32 +02:00
|
|
|
SELECT '${strategy.name}', '${strategy.description}', '${JSON.stringify(
|
|
|
|
strategy.parameters
|
|
|
|
)}', 1
|
2017-02-11 11:17:11 +01:00
|
|
|
WHERE
|
|
|
|
NOT EXISTS (
|
|
|
|
SELECT name FROM strategies WHERE name = '${strategy.name}'
|
|
|
|
);`;
|
|
|
|
}
|
|
|
|
|
2017-06-28 14:10:32 +02:00
|
|
|
function insertEventsSQL(strategy) {
|
2017-02-11 11:17:11 +01:00
|
|
|
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}'
|
|
|
|
);`;
|
|
|
|
}
|
|
|
|
|
2017-06-28 14:10:32 +02:00
|
|
|
function removeEventsSQL(strategy) {
|
2017-02-11 14:45:17 +01:00
|
|
|
return `
|
|
|
|
INSERT INTO events (type, created_by, data)
|
|
|
|
SELECT 'strategy-deleted', 'migration', '${JSON.stringify(strategy)}'
|
|
|
|
WHERE
|
|
|
|
EXISTS (
|
2017-11-11 08:43:08 +01:00
|
|
|
SELECT name FROM strategies WHERE name = '${
|
|
|
|
strategy.name
|
|
|
|
}' AND built_in = 1
|
2017-02-11 14:45:17 +01:00
|
|
|
);`;
|
|
|
|
}
|
|
|
|
|
2017-06-28 14:10:32 +02:00
|
|
|
function removeStrategySQL(strategy) {
|
2017-02-11 14:45:17 +01:00
|
|
|
return `
|
|
|
|
DELETE FROM strategies
|
|
|
|
WHERE name = '${strategy.name}' AND built_in = 1`;
|
|
|
|
}
|
|
|
|
|
2017-06-28 14:10:32 +02:00
|
|
|
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
|
|
|
|
);
|
2017-02-11 11:17:11 +01:00
|
|
|
});
|
|
|
|
async.series(insertStrategies, callback);
|
|
|
|
};
|
|
|
|
|
2017-06-28 14:10:32 +02:00
|
|
|
exports.down = function(db, callback) {
|
2017-02-11 14:45:17 +01:00
|
|
|
const removeStrategies = strategies
|
|
|
|
.filter(s => s.name !== 'default')
|
2017-06-28 14:10:32 +02:00
|
|
|
.map(s => cb => {
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
db.runSql.bind(db, removeEventsSQL(s)),
|
|
|
|
db.runSql.bind(db, removeStrategySQL(s)),
|
|
|
|
],
|
|
|
|
cb
|
|
|
|
);
|
2017-02-11 14:45:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
async.series(removeStrategies, callback);
|
2017-02-11 11:17:11 +01:00
|
|
|
};
|