Extract getMigrationVersions

This commit is contained in:
Daniel Gasienica 2018-03-28 13:05:49 -04:00
parent 6aea36240d
commit c5c94bc3ab

View file

@ -3,6 +3,8 @@
const isFunction = require('lodash/isFunction');
const isObject = require('lodash/isObject');
const isString = require('lodash/isString');
const head = require('lodash/head');
const last = require('lodash/last');
const { deferredToPromise } = require('../deferred_to_promise');
@ -30,3 +32,17 @@ exports.runMigrations = async ({ Backbone, database } = {}) => {
console.log('Close database connection');
await closeDatabaseConnection({ Backbone });
};
const getMigrationVersions = (database) => {
if (!isObject(database) || !Array.isArray(database.migrations)) {
throw new TypeError('"database" is required');
}
const firstMigration = head(database.migrations);
const lastMigration = last(database.migrations);
const firstVersion = firstMigration ? parseInt(firstMigration.version, 10) : null;
const lastVersion = lastMigration ? parseInt(lastMigration.version, 10) : null;
return { firstVersion, lastVersion };
};