2020-11-06 14:27:57 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
2020-12-17 12:33:00 +01:00
|
|
|
const createMigrationRunner = (migrationFunction, { hooks = [] } = {}, context = {}) => {
|
|
|
|
const beforeHook = async (options, context) => {
|
2020-11-06 14:27:57 +01:00
|
|
|
for (const migration of hooks) {
|
|
|
|
if (_.isFunction(migration.before)) {
|
2020-12-17 12:33:00 +01:00
|
|
|
await migration.before(options, context);
|
2020-11-06 14:27:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-17 12:33:00 +01:00
|
|
|
const afterHook = async (options, context) => {
|
2020-11-06 14:27:57 +01:00
|
|
|
for (const migration of hooks.slice(0).reverse()) {
|
|
|
|
if (_.isFunction(migration.after)) {
|
2020-12-17 12:33:00 +01:00
|
|
|
await migration.after(options, context);
|
2020-11-06 14:27:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const run = async options => {
|
2020-12-17 12:33:00 +01:00
|
|
|
await beforeHook(options, context);
|
|
|
|
await migrationFunction(options, context);
|
|
|
|
await afterHook(options, context);
|
2020-11-06 14:27:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
run,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = createMigrationRunner;
|