From fcd30cd91943036d94473e14731d3d1ee09d9bed Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 26 Mar 2018 15:34:36 -0400 Subject: [PATCH] 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 - https://github.com/superfeedr/indexeddb-backbonejs-adapter/blob/80c7a06d5c0a75296106e9a62663230459cef857/backbone-indexeddb.js#L558-L565 --- js/background.js | 5 ++++- ...migrations_0_database_with_attachment_data.js | 4 ++-- ...rations_1_database_without_attachment_data.js | 4 ++-- js/modules/migrations/run_migrations.js | 16 ++++++++++------ 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/js/background.js b/js/background.js index e21c45e28758..7eea361d9702 100644 --- a/js/background.js +++ b/js/background.js @@ -85,11 +85,14 @@ console.log('Start IndexedDB migrations'); console.log('Migrate database with attachments'); - await Migrations0DatabaseWithAttachmentData.run({ Backbone }); + const closeDatabase = () => + Whisper.Database.close(); + await Migrations0DatabaseWithAttachmentData.run({ Backbone, closeDatabase }); console.log('Migrate database without attachments'); await Migrations1DatabaseWithoutAttachmentData.run({ Backbone, + closeDatabase, Database: Whisper.Database, }); diff --git a/js/modules/migrations/migrations_0_database_with_attachment_data.js b/js/modules/migrations/migrations_0_database_with_attachment_data.js index 6f8f99079bf4..0fa12cc97bdb 100644 --- a/js/modules/migrations/migrations_0_database_with_attachment_data.js +++ b/js/modules/migrations/migrations_0_database_with_attachment_data.js @@ -146,5 +146,5 @@ const database = { migrations, }; -exports.run = ({ Backbone } = {}) => - runMigrations({ Backbone, database }); +exports.run = ({ Backbone, closeDatabase } = {}) => + runMigrations({ Backbone, closeDatabase, database }); diff --git a/js/modules/migrations/migrations_1_database_without_attachment_data.js b/js/modules/migrations/migrations_1_database_without_attachment_data.js index 04bacb8709e2..46ca5aedda06 100644 --- a/js/modules/migrations/migrations_1_database_without_attachment_data.js +++ b/js/modules/migrations/migrations_1_database_without_attachment_data.js @@ -11,5 +11,5 @@ exports.migrations = [ }, ]; -exports.run = ({ Backbone, Database } = {}) => - runMigrations({ Backbone, database: Database }); +exports.run = ({ Backbone, closeDatabase, Database } = {}) => + runMigrations({ Backbone, closeDatabase, database: Database }); diff --git a/js/modules/migrations/run_migrations.js b/js/modules/migrations/run_migrations.js index cdd26dd97a39..878ef3badd42 100644 --- a/js/modules/migrations/run_migrations.js +++ b/js/modules/migrations/run_migrations.js @@ -2,13 +2,19 @@ const isFunction = require('lodash/isFunction'); const isObject = require('lodash/isObject'); const isString = require('lodash/isString'); +const { deferredToPromise } = require('../deferred_to_promise'); -exports.runMigrations = ({ Backbone, database } = {}) => { + +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'); @@ -19,9 +25,7 @@ exports.runMigrations = ({ Backbone, 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()); - }); + await deferredToPromise(migrationCollection.fetch()); + await closeDatabase(); + return; };