Convert message schema migrator to TypeScript
This commit is contained in:
parent
2a2f44a73a
commit
c58f1eb503
|
@ -129,9 +129,6 @@ const { QualifiedAddress } = require('../../ts/types/QualifiedAddress');
|
||||||
// Views
|
// Views
|
||||||
const Initialization = require('./views/initialization');
|
const Initialization = require('./views/initialization');
|
||||||
|
|
||||||
// Workflow
|
|
||||||
const MessageDataMigrator = require('./messages_data_migrator');
|
|
||||||
|
|
||||||
// Processes / Services
|
// Processes / Services
|
||||||
const {
|
const {
|
||||||
initializeGroupCredentialFetcher,
|
initializeGroupCredentialFetcher,
|
||||||
|
@ -412,10 +409,6 @@ exports.setup = (options = {}) => {
|
||||||
Initialization,
|
Initialization,
|
||||||
};
|
};
|
||||||
|
|
||||||
const Workflow = {
|
|
||||||
MessageDataMigrator,
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
Backbone,
|
Backbone,
|
||||||
Components,
|
Components,
|
||||||
|
@ -436,6 +429,5 @@ exports.setup = (options = {}) => {
|
||||||
Types,
|
Types,
|
||||||
Util,
|
Util,
|
||||||
Views,
|
Views,
|
||||||
Workflow,
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -50,6 +50,7 @@ import { isMoreRecentThan, isOlderThan, toDayMillis } from './util/timestamp';
|
||||||
import { isValidReactionEmoji } from './reactions/isValidReactionEmoji';
|
import { isValidReactionEmoji } from './reactions/isValidReactionEmoji';
|
||||||
import type { ConversationModel } from './models/conversations';
|
import type { ConversationModel } from './models/conversations';
|
||||||
import { getContact } from './messages/helpers';
|
import { getContact } from './messages/helpers';
|
||||||
|
import { migrateMessageData } from './messages/migrateMessageData';
|
||||||
import { createBatcher } from './util/batcher';
|
import { createBatcher } from './util/batcher';
|
||||||
import { updateConversationsWithUuidLookup } from './updateConversationsWithUuidLookup';
|
import { updateConversationsWithUuidLookup } from './updateConversationsWithUuidLookup';
|
||||||
import { initializeAllJobQueues } from './jobs/initializeAllJobQueues';
|
import { initializeAllJobQueues } from './jobs/initializeAllJobQueues';
|
||||||
|
@ -493,7 +494,6 @@ export async function startApp(): Promise<void> {
|
||||||
// of preload.js processing
|
// of preload.js processing
|
||||||
window.setImmediate = window.nodeSetImmediate;
|
window.setImmediate = window.nodeSetImmediate;
|
||||||
|
|
||||||
const { MessageDataMigrator } = window.Signal.Workflow;
|
|
||||||
const { removeDatabase: removeIndexedDB, doesDatabaseExist } =
|
const { removeDatabase: removeIndexedDB, doesDatabaseExist } =
|
||||||
window.Signal.IndexedDB;
|
window.Signal.IndexedDB;
|
||||||
const { Message } = window.Signal.Types;
|
const { Message } = window.Signal.Types;
|
||||||
|
@ -827,8 +827,7 @@ export async function startApp(): Promise<void> {
|
||||||
const NUM_MESSAGES_PER_BATCH = 1;
|
const NUM_MESSAGES_PER_BATCH = 1;
|
||||||
|
|
||||||
if (!isMigrationWithIndexComplete) {
|
if (!isMigrationWithIndexComplete) {
|
||||||
const batchWithIndex = await MessageDataMigrator.processNext({
|
const batchWithIndex = await migrateMessageData({
|
||||||
BackboneMessageCollection: window.Whisper.MessageCollection,
|
|
||||||
numMessagesPerBatch: NUM_MESSAGES_PER_BATCH,
|
numMessagesPerBatch: NUM_MESSAGES_PER_BATCH,
|
||||||
upgradeMessageSchema,
|
upgradeMessageSchema,
|
||||||
getMessagesNeedingUpgrade:
|
getMessagesNeedingUpgrade:
|
||||||
|
|
|
@ -1,29 +1,49 @@
|
||||||
// Copyright 2018-2020 Signal Messenger, LLC
|
// Copyright 2018-2022 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// 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 */
|
/**
|
||||||
|
* Ensures that messages in database are at the right schema.
|
||||||
const { isFunction, isNumber } = require('lodash');
|
*/
|
||||||
|
export async function migrateMessageData({
|
||||||
const Message = require('./types/message');
|
|
||||||
|
|
||||||
exports.processNext = async ({
|
|
||||||
BackboneMessageCollection,
|
|
||||||
numMessagesPerBatch,
|
numMessagesPerBatch,
|
||||||
upgradeMessageSchema,
|
upgradeMessageSchema,
|
||||||
getMessagesNeedingUpgrade,
|
getMessagesNeedingUpgrade,
|
||||||
saveMessage,
|
saveMessage,
|
||||||
maxVersion = Message.CURRENT_SCHEMA_VERSION,
|
maxVersion = Message.CURRENT_SCHEMA_VERSION,
|
||||||
} = {}) => {
|
}: Readonly<{
|
||||||
if (!isFunction(BackboneMessageCollection)) {
|
numMessagesPerBatch: number;
|
||||||
throw new TypeError(
|
upgradeMessageSchema: (
|
||||||
"'BackboneMessageCollection' (Whisper.MessageCollection)" +
|
message: MessageAttributesType,
|
||||||
' constructor is required'
|
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)) {
|
if (!isNumber(numMessagesPerBatch)) {
|
||||||
throw new TypeError("'numMessagesPerBatch' is required");
|
throw new TypeError("'numMessagesPerBatch' is required");
|
||||||
}
|
}
|
||||||
|
@ -39,10 +59,7 @@ exports.processNext = async ({
|
||||||
try {
|
try {
|
||||||
messagesRequiringSchemaUpgrade = await getMessagesNeedingUpgrade(
|
messagesRequiringSchemaUpgrade = await getMessagesNeedingUpgrade(
|
||||||
numMessagesPerBatch,
|
numMessagesPerBatch,
|
||||||
{
|
{ maxVersion }
|
||||||
maxVersion,
|
|
||||||
MessageCollection: BackboneMessageCollection,
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
window.SignalContext.log.error(
|
window.SignalContext.log.error(
|
||||||
|
@ -85,4 +102,4 @@ exports.processNext = async ({
|
||||||
saveDuration,
|
saveDuration,
|
||||||
totalDuration,
|
totalDuration,
|
||||||
};
|
};
|
||||||
};
|
}
|
|
@ -401,9 +401,6 @@ declare global {
|
||||||
WhatsNewLink: typeof WhatsNewLink;
|
WhatsNewLink: typeof WhatsNewLink;
|
||||||
};
|
};
|
||||||
OS: typeof OS;
|
OS: typeof OS;
|
||||||
Workflow: {
|
|
||||||
MessageDataMigrator: WhatIsThis;
|
|
||||||
};
|
|
||||||
IndexedDB: {
|
IndexedDB: {
|
||||||
removeDatabase: WhatIsThis;
|
removeDatabase: WhatIsThis;
|
||||||
doesDatabaseExist: WhatIsThis;
|
doesDatabaseExist: WhatIsThis;
|
||||||
|
|
Loading…
Reference in New Issue