diff --git a/js/background.js b/js/background.js index 1e09efa44..e287db1c0 100644 --- a/js/background.js +++ b/js/background.js @@ -87,7 +87,13 @@ await Migrations0DatabaseWithAttachmentData.run({ Backbone }); console.log('Migrate attachments to disk'); - await MessageDataMigrator.processAll({ Backbone, upgradeMessageSchema }); + const database = Migrations0DatabaseWithAttachmentData.getDatabase(); + await MessageDataMigrator.processAll({ + Backbone, + databaseName: database.name, + databaseVersion: database.version, + upgradeMessageSchema, + }); console.log('Migrate database without attachments'); await Migrations1DatabaseWithoutAttachmentData.run({ diff --git a/js/modules/messages_data_migrator.js b/js/modules/messages_data_migrator.js index c00c27b6c..c240e7f2d 100644 --- a/js/modules/messages_data_migrator.js +++ b/js/modules/messages_data_migrator.js @@ -1,11 +1,10 @@ /* eslint-env browser */ // Module to upgrade the schema of messages, e.g. migrate attachments to disk. -// `processAll` is meant to be run after the initial database migrations -// (12 – 17) and purposely doesn’t rely on our Backbone IndexedDB adapter to -// prevent subsequent migrations to run (18+) but rather uses direct IndexedDB -// access. This includes avoiding usage of `storage` module which uses Backbone -// under the hood. +// `processAll` purposely doesn’t rely on our Backbone IndexedDB adapter to +// prevent automatic migrations. Rather, it uses direct IndexedDB access. +// This includes avoiding usage of `storage` module which uses Backbone under +// the hood. const isFunction = require('lodash/isFunction'); const isNumber = require('lodash/isNumber'); @@ -17,9 +16,6 @@ const Message = require('./types/message'); const { deferredToPromise } = require('./deferred_to_promise'); -const DATABASE_NAME = 'signal'; -// Last version with attachment data stored in database: -const EXPECTED_DATABASE_VERSION = 17; const MESSAGES_STORE_NAME = 'messages'; const ITEMS_STORE_NAME = 'items'; const NUM_MESSAGES_PER_BATCH = 50; @@ -77,16 +73,29 @@ exports.processNext = async ({ }; }; -exports.processAll = async ({ Backbone, upgradeMessageSchema } = {}) => { +exports.processAll = async ({ + Backbone, + databaseName, + databaseVersion, + upgradeMessageSchema, + } = {}) => { if (!isObject(Backbone)) { throw new TypeError('"Backbone" is required'); } + if (!isString(databaseName)) { + throw new TypeError('"databaseName" must be a string'); + } + + if (!isNumber(databaseVersion)) { + throw new TypeError('"databaseVersion" must be a number'); + } + if (!isFunction(upgradeMessageSchema)) { throw new TypeError('"upgradeMessageSchema" is required'); } - const connection = await openDatabase(DATABASE_NAME, EXPECTED_DATABASE_VERSION); + const connection = await openDatabase(databaseName, databaseVersion); const isComplete = await isMigrationComplete(connection); console.log('Is attachment migration complete?', isComplete); if (isComplete) { 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 e00f71870..02cd097ff 100644 --- a/js/modules/migrations/migrations_0_database_with_attachment_data.js +++ b/js/modules/migrations/migrations_0_database_with_attachment_data.js @@ -1,6 +1,7 @@ const isFunction = require('lodash/isFunction'); const isObject = require('lodash/isObject'); const isString = require('lodash/isString'); +const last = require('lodash/last'); const { runMigrations } = require('./run_migrations'); @@ -169,3 +170,8 @@ exports.createCollection = ({ Backbone, storeName }) => { return collection; }; + +exports.getDatabase = () => ({ + name: database.id, + version: last(migrations).version, +});