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)
This commit is contained in:
parent
c765422fa1
commit
fcd30cd919
4 changed files with 18 additions and 11 deletions
|
@ -85,11 +85,14 @@
|
||||||
console.log('Start IndexedDB migrations');
|
console.log('Start IndexedDB migrations');
|
||||||
|
|
||||||
console.log('Migrate database with attachments');
|
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');
|
console.log('Migrate database without attachments');
|
||||||
await Migrations1DatabaseWithoutAttachmentData.run({
|
await Migrations1DatabaseWithoutAttachmentData.run({
|
||||||
Backbone,
|
Backbone,
|
||||||
|
closeDatabase,
|
||||||
Database: Whisper.Database,
|
Database: Whisper.Database,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -146,5 +146,5 @@ const database = {
|
||||||
migrations,
|
migrations,
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.run = ({ Backbone } = {}) =>
|
exports.run = ({ Backbone, closeDatabase } = {}) =>
|
||||||
runMigrations({ Backbone, database });
|
runMigrations({ Backbone, closeDatabase, database });
|
||||||
|
|
|
@ -11,5 +11,5 @@ exports.migrations = [
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
exports.run = ({ Backbone, Database } = {}) =>
|
exports.run = ({ Backbone, closeDatabase, Database } = {}) =>
|
||||||
runMigrations({ Backbone, database: Database });
|
runMigrations({ Backbone, closeDatabase, database: Database });
|
||||||
|
|
|
@ -2,13 +2,19 @@ const isFunction = require('lodash/isFunction');
|
||||||
const isObject = require('lodash/isObject');
|
const isObject = require('lodash/isObject');
|
||||||
const isString = require('lodash/isString');
|
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) ||
|
if (!isObject(Backbone) || !isObject(Backbone.Collection) ||
|
||||||
!isFunction(Backbone.Collection.extend)) {
|
!isFunction(Backbone.Collection.extend)) {
|
||||||
throw new TypeError('"Backbone" is required');
|
throw new TypeError('"Backbone" is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isFunction(closeDatabase)) {
|
||||||
|
throw new TypeError('"closeDatabase" is required');
|
||||||
|
}
|
||||||
|
|
||||||
if (!isObject(database) || !isString(database.id) ||
|
if (!isObject(database) || !isString(database.id) ||
|
||||||
!Array.isArray(database.migrations)) {
|
!Array.isArray(database.migrations)) {
|
||||||
throw new TypeError('"database" is required');
|
throw new TypeError('"database" is required');
|
||||||
|
@ -19,9 +25,7 @@ exports.runMigrations = ({ Backbone, database } = {}) => {
|
||||||
storeName: 'items',
|
storeName: 'items',
|
||||||
}))();
|
}))();
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
await deferredToPromise(migrationCollection.fetch());
|
||||||
// NOTE: This `then` refers to a jQuery `Deferred`:
|
await closeDatabase();
|
||||||
// eslint-disable-next-line more/no-then
|
return;
|
||||||
migrationCollection.fetch().then(() => resolve());
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue