Implement background migration using index

This commit is contained in:
Daniel Gasienica 2018-04-26 20:09:48 -04:00
parent 9ca6fd6498
commit 86e48a5713

View file

@ -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();
}
});