Extract runMigrations

This commit is contained in:
Daniel Gasienica 2018-03-26 15:17:19 -04:00
parent d16178638e
commit d7c8d33edb
2 changed files with 30 additions and 19 deletions

View file

@ -1,5 +1,4 @@
const isFunction = require('lodash/isFunction');
const isObject = require('lodash/isObject');
const { runMigrations } = require('./run_migrations');
// IMPORTANT: The migrations below are run on a database that may be very large
@ -147,20 +146,5 @@ const database = {
migrations,
};
exports.run = ({ Backbone } = {}) => {
if (!isObject(Backbone) || !isObject(Backbone.Collection) ||
!isFunction(Backbone.Collection.extend)) {
throw new TypeError('"Backbone" is required');
}
const migrationCollection = new (Backbone.Collection.extend({
database,
storeName: 'conversations',
}))();
return new Promise((resolve) => {
// NOTE: This `then` refers to a jQuery `Deferred`:
// eslint-disable-next-line more/no-then
migrationCollection.fetch().then(() => resolve());
});
};
exports.run = ({ Backbone } = {}) =>
runMigrations({ Backbone, database });

View file

@ -0,0 +1,27 @@
const isFunction = require('lodash/isFunction');
const isObject = require('lodash/isObject');
const isString = require('lodash/isString');
exports.runMigrations = ({ Backbone, database } = {}) => {
if (!isObject(Backbone) || !isObject(Backbone.Collection) ||
!isFunction(Backbone.Collection.extend)) {
throw new TypeError('"Backbone" is required');
}
if (!isObject(database) || !isString(database.id) ||
!Array.isArray(database.migrations)) {
throw new TypeError('"database" is required');
}
const migrationCollection = new (Backbone.Collection.extend({
database,
storeName: 'items',
}))();
return new Promise((resolve) => {
// NOTE: This `then` refers to a jQuery `Deferred`:
// eslint-disable-next-line more/no-then
migrationCollection.fetch().then(() => resolve());
});
};