botpress
Version:
The world's first CMS for bots. Easily create, manage and extend chatbots.
82 lines (65 loc) • 4.82 kB
JavaScript
;
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fs = require('fs');
var _helpers = require('./helpers');
var _helpers2 = _interopRequireDefault(_helpers);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } /*
It's module for run migration for every module which has dir "/migrations"
in module root folder. Every files in dir must be
[timestamp_miliseconds]__[name].js (example: 1538478025618__hitl_messages.js)
*/
const getMigrationDirIfExist = (dirList, { root }) => {
const migrationPath = _path2.default.resolve(root, './migrations');
return (0, _fs.existsSync)(migrationPath) ? [...dirList, migrationPath] : dirList;
};
const runUp = (() => {
var _ref = _asyncToGenerator(function* (knex, dir) {
const passedMigrations = (yield knex('knex_module_migrations').select('name')).map(function ({ name }) {
return name;
});
const dirFiles = (0, _fs.readdirSync)(dir).filter(function (file) {
return (/^\d+__.+\.js$/.test(file) && !passedMigrations.includes(file)
);
}).sort();
for (let index = 0; index < dirFiles.length; index++) {
const name = dirFiles[index];
const migration = require(_path2.default.resolve(dir, name));
migration.up && (yield migration.up(knex));
yield knex('knex_module_migrations').insert({ name, migration_time: new Date() });
}
});
return function runUp(_x, _x2) {
return _ref.apply(this, arguments);
};
})();
module.exports = (() => {
var _ref2 = _asyncToGenerator(function* (db, moduleDefinitions = []) {
const migrationsDirList = moduleDefinitions.reduce(getMigrationDirIfExist, []);
const knex = yield db.get();
yield (0, _helpers2.default)(knex).createTableIfNotExists('knex_module_migrations', function (table) {
table.increments('id').primary();
table.string('name');
table.timestamp('migration_time');
});
return {
up: function () {
return Promise.all(migrationsDirList.map(function (dir) {
return runUp(knex, dir);
}));
},
/*
TODO: write it only with changes in "botpress CL"
maybe it will be make this command: botpress migrate:[up||down] [module_name]
*/
down: function () {
return {};
} // TODO: write it only with changes in "botpress CL"
};
});
return function (_x3) {
return _ref2.apply(this, arguments);
};
})();
//# sourceMappingURL=migration.js.map