From 86e48a5713d0b56a419ceb27267dd607d16a1016 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Thu, 26 Apr 2018 20:09:48 -0400 Subject: [PATCH] Implement background migration using index --- js/background.js | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/js/background.js b/js/background.js index 846fda7d1..4f5003035 100644 --- a/js/background.js +++ b/js/background.js @@ -90,18 +90,37 @@ storage.fetch(); const idleDetector = new IdleDetector(); + let isMigrationWithIndexComplete = false; + let isMigrationWithoutIndexComplete = false; idleDetector.on('idle', async () => { const NUM_MESSAGES_PER_BATCH = 1; - const database = Migrations0DatabaseWithAttachmentData.getDatabase(); - const batch = await MessageDataMigrator.processNextBatchWithoutIndex({ - databaseName: database.name, - minDatabaseVersion: database.version, - numMessagesPerBatch: NUM_MESSAGES_PER_BATCH, - upgradeMessageSchema, - }); - console.log('Upgrade message schema:', batch); - if (batch.done) { + if (!isMigrationWithIndexComplete) { + const batchWithIndex = await MessageDataMigrator.processNext({ + BackboneMessage: Whisper.Message, + BackboneMessageCollection: Whisper.MessageCollection, + numMessagesPerBatch: NUM_MESSAGES_PER_BATCH, + upgradeMessageSchema, + }); + console.log('Upgrade message schema (with index):', batchWithIndex); + isMigrationWithIndexComplete = batchWithIndex.done; + } + + if (!isMigrationWithoutIndexComplete) { + const database = Migrations0DatabaseWithAttachmentData.getDatabase(); + const batchWithoutIndex = await MessageDataMigrator.processNextBatchWithoutIndex({ + databaseName: database.name, + minDatabaseVersion: database.version, + numMessagesPerBatch: NUM_MESSAGES_PER_BATCH, + upgradeMessageSchema, + }); + console.log('Upgrade message schema (without index):', batchWithoutIndex); + isMigrationWithoutIndexComplete = batchWithoutIndex.done; + } + + const areAllMigrationsComplete = isMigrationWithIndexComplete && + isMigrationWithoutIndexComplete; + if (areAllMigrationsComplete) { idleDetector.stop(); } });