signal-desktop/js/modules/migrations/run_migrations.js
Daniel Gasienica fcd30cd919 Close database after migration
This is not 100% reliable as database connections are closed in a separate
thread according to the documentation:
- https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/close
- https://stackoverflow.com/a/18639298
- 80c7a06d5c/backbone-indexeddb.js (L558-L565)
2018-04-02 12:17:36 -04:00

31 lines
914 B
JavaScript

const isFunction = require('lodash/isFunction');
const isObject = require('lodash/isObject');
const isString = require('lodash/isString');
const { deferredToPromise } = require('../deferred_to_promise');
exports.runMigrations = async ({ Backbone, closeDatabase, database } = {}) => {
if (!isObject(Backbone) || !isObject(Backbone.Collection) ||
!isFunction(Backbone.Collection.extend)) {
throw new TypeError('"Backbone" is required');
}
if (!isFunction(closeDatabase)) {
throw new TypeError('"closeDatabase" 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',
}))();
await deferredToPromise(migrationCollection.fetch());
await closeDatabase();
return;
};