From c5c94bc3ab0deb8a6848d93943287f10f4bd1948 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Wed, 28 Mar 2018 13:05:49 -0400 Subject: [PATCH] Extract `getMigrationVersions` --- js/modules/migrations/run_migrations.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/js/modules/migrations/run_migrations.js b/js/modules/migrations/run_migrations.js index ac130d847..84635542a 100644 --- a/js/modules/migrations/run_migrations.js +++ b/js/modules/migrations/run_migrations.js @@ -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 }; +};