Convert message schema migrator to TypeScript

This commit is contained in:
Evan Hahn 2022-05-31 23:56:25 +00:00 committed by GitHub
parent 2a2f44a73a
commit c58f1eb503
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 37 deletions

View File

@ -129,9 +129,6 @@ const { QualifiedAddress } = require('../../ts/types/QualifiedAddress');
// Views
const Initialization = require('./views/initialization');
// Workflow
const MessageDataMigrator = require('./messages_data_migrator');
// Processes / Services
const {
initializeGroupCredentialFetcher,
@ -412,10 +409,6 @@ exports.setup = (options = {}) => {
Initialization,
};
const Workflow = {
MessageDataMigrator,
};
return {
Backbone,
Components,
@ -436,6 +429,5 @@ exports.setup = (options = {}) => {
Types,
Util,
Views,
Workflow,
};
};

View File

@ -50,6 +50,7 @@ import { isMoreRecentThan, isOlderThan, toDayMillis } from './util/timestamp';
import { isValidReactionEmoji } from './reactions/isValidReactionEmoji';
import type { ConversationModel } from './models/conversations';
import { getContact } from './messages/helpers';
import { migrateMessageData } from './messages/migrateMessageData';
import { createBatcher } from './util/batcher';
import { updateConversationsWithUuidLookup } from './updateConversationsWithUuidLookup';
import { initializeAllJobQueues } from './jobs/initializeAllJobQueues';
@ -493,7 +494,6 @@ export async function startApp(): Promise<void> {
// of preload.js processing
window.setImmediate = window.nodeSetImmediate;
const { MessageDataMigrator } = window.Signal.Workflow;
const { removeDatabase: removeIndexedDB, doesDatabaseExist } =
window.Signal.IndexedDB;
const { Message } = window.Signal.Types;
@ -827,8 +827,7 @@ export async function startApp(): Promise<void> {
const NUM_MESSAGES_PER_BATCH = 1;
if (!isMigrationWithIndexComplete) {
const batchWithIndex = await MessageDataMigrator.processNext({
BackboneMessageCollection: window.Whisper.MessageCollection,
const batchWithIndex = await migrateMessageData({
numMessagesPerBatch: NUM_MESSAGES_PER_BATCH,
upgradeMessageSchema,
getMessagesNeedingUpgrade:

View File

@ -1,29 +1,49 @@
// Copyright 2018-2020 Signal Messenger, LLC
// Copyright 2018-2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
// Ensures that messages in database are at the right schema.
import { isFunction, isNumber } from 'lodash';
import * as Message from '../../js/modules/types/message';
import type { MessageAttributesType } from '../model-types.d';
import type { UUIDStringType } from '../types/UUID';
/* global window */
const { isFunction, isNumber } = require('lodash');
const Message = require('./types/message');
exports.processNext = async ({
BackboneMessageCollection,
/**
* Ensures that messages in database are at the right schema.
*/
export async function migrateMessageData({
numMessagesPerBatch,
upgradeMessageSchema,
getMessagesNeedingUpgrade,
saveMessage,
maxVersion = Message.CURRENT_SCHEMA_VERSION,
} = {}) => {
if (!isFunction(BackboneMessageCollection)) {
throw new TypeError(
"'BackboneMessageCollection' (Whisper.MessageCollection)" +
' constructor is required'
);
}
}: Readonly<{
numMessagesPerBatch: number;
upgradeMessageSchema: (
message: MessageAttributesType,
options: { maxVersion: number }
) => Promise<MessageAttributesType>;
getMessagesNeedingUpgrade: (
limit: number,
options: { maxVersion: number }
) => Promise<Array<MessageAttributesType>>;
saveMessage: (
data: MessageAttributesType,
options: { ourUuid: UUIDStringType }
) => Promise<string>;
maxVersion?: number;
}>): Promise<
| {
done: true;
numProcessed: 0;
}
| {
done: boolean;
numProcessed: number;
fetchDuration: number;
upgradeDuration: number;
saveDuration: number;
totalDuration: number;
}
> {
if (!isNumber(numMessagesPerBatch)) {
throw new TypeError("'numMessagesPerBatch' is required");
}
@ -39,10 +59,7 @@ exports.processNext = async ({
try {
messagesRequiringSchemaUpgrade = await getMessagesNeedingUpgrade(
numMessagesPerBatch,
{
maxVersion,
MessageCollection: BackboneMessageCollection,
}
{ maxVersion }
);
} catch (error) {
window.SignalContext.log.error(
@ -85,4 +102,4 @@ exports.processNext = async ({
saveDuration,
totalDuration,
};
};
}

3
ts/window.d.ts vendored
View File

@ -401,9 +401,6 @@ declare global {
WhatsNewLink: typeof WhatsNewLink;
};
OS: typeof OS;
Workflow: {
MessageDataMigrator: WhatIsThis;
};
IndexedDB: {
removeDatabase: WhatIsThis;
doesDatabaseExist: WhatIsThis;