signal-desktop/js/modules/messages_data_migrator.js

94 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2018-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2020-04-28 21:18:41 +00:00
// Ensures that messages in database are at the right schema.
2020-04-28 21:18:41 +00:00
/* global window */
2020-04-28 21:18:41 +00:00
const { isFunction, isNumber } = require('lodash');
2018-03-21 23:37:39 +00:00
const Message = require('./types/message');
2018-03-26 20:32:22 +00:00
exports.processNext = async ({
2018-03-21 23:37:39 +00:00
BackboneMessage,
BackboneMessageCollection,
numMessagesPerBatch,
2018-03-21 23:37:39 +00:00
upgradeMessageSchema,
getMessagesNeedingUpgrade,
saveMessage,
maxVersion = Message.CURRENT_SCHEMA_VERSION,
2018-03-21 23:37:39 +00:00
} = {}) => {
if (!isFunction(BackboneMessage)) {
2018-04-27 21:25:04 +00:00
throw new TypeError(
"'BackboneMessage' (Whisper.Message) constructor is required"
);
2018-03-21 23:37:39 +00:00
}
if (!isFunction(BackboneMessageCollection)) {
2018-04-27 21:25:04 +00:00
throw new TypeError(
"'BackboneMessageCollection' (Whisper.MessageCollection)" +
' constructor is required'
);
2018-03-21 23:37:39 +00:00
}
if (!isNumber(numMessagesPerBatch)) {
2018-04-11 19:44:52 +00:00
throw new TypeError("'numMessagesPerBatch' is required");
2018-03-21 23:37:39 +00:00
}
if (!isFunction(upgradeMessageSchema)) {
2018-04-11 19:44:52 +00:00
throw new TypeError("'upgradeMessageSchema' is required");
2018-03-21 23:37:39 +00:00
}
const startTime = Date.now();
2018-03-27 16:19:55 +00:00
const fetchStartTime = Date.now();
2018-09-21 01:47:19 +00:00
let messagesRequiringSchemaUpgrade;
try {
messagesRequiringSchemaUpgrade = await getMessagesNeedingUpgrade(
numMessagesPerBatch,
{
maxVersion,
MessageCollection: BackboneMessageCollection,
}
);
} catch (error) {
window.log.error(
'processNext error:',
error && error.stack ? error.stack : error
);
return {
done: true,
numProcessed: 0,
};
}
2018-03-27 16:19:55 +00:00
const fetchDuration = Date.now() - fetchStartTime;
2018-03-21 23:37:39 +00:00
2018-03-27 16:19:55 +00:00
const upgradeStartTime = Date.now();
2018-04-27 21:25:04 +00:00
const upgradedMessages = await Promise.all(
messagesRequiringSchemaUpgrade.map(message =>
upgradeMessageSchema(message, { maxVersion })
)
2018-04-27 21:25:04 +00:00
);
2018-03-27 16:19:55 +00:00
const upgradeDuration = Date.now() - upgradeStartTime;
2018-03-21 23:37:39 +00:00
2018-03-27 16:19:55 +00:00
const saveStartTime = Date.now();
await Promise.all(
upgradedMessages.map(message =>
saveMessage(message, { Message: BackboneMessage })
)
);
2018-03-27 16:19:55 +00:00
const saveDuration = Date.now() - saveStartTime;
2018-03-21 23:37:39 +00:00
const totalDuration = Date.now() - startTime;
const numProcessed = messagesRequiringSchemaUpgrade.length;
const done = numProcessed < numMessagesPerBatch;
2018-03-21 23:37:39 +00:00
return {
done,
2018-03-21 23:37:39 +00:00
numProcessed,
fetchDuration,
upgradeDuration,
saveDuration,
totalDuration,
};
};