signal-desktop/ts/sql/migrations/51-centralize-conversation-jobs.ts

105 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-02-16 18:36:21 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { LoggerType } from '../../types/Logging';
import { isRecord } from '../../util/isRecord';
2024-07-22 18:16:33 +00:00
import type { WritableDB } from '../Interface';
import { getJobsInQueue, getMessageById, insertJob } from '../Server';
2022-02-16 18:36:21 +00:00
export default function updateToSchemaVersion51(
currentVersion: number,
2024-07-22 18:16:33 +00:00
db: WritableDB,
2022-02-16 18:36:21 +00:00
logger: LoggerType
): void {
if (currentVersion >= 51) {
return;
}
db.transaction(() => {
const deleteJobsInQueue = db.prepare(
'DELETE FROM jobs WHERE queueType = $queueType'
);
// First, make sure that reactions job data has a type and conversationId
2024-07-22 18:16:33 +00:00
const reactionsJobs = getJobsInQueue(db, 'reactions');
2022-02-16 18:36:21 +00:00
deleteJobsInQueue.run({ queueType: 'reactions' });
reactionsJobs.forEach(job => {
const { data, id } = job;
if (!isRecord(data)) {
logger.warn(
`updateToSchemaVersion51: reactions queue job ${id} was missing valid data`
);
return;
}
const { messageId } = data;
if (typeof messageId !== 'string') {
logger.warn(
`updateToSchemaVersion51: reactions queue job ${id} had a non-string messageId`
);
return;
}
2024-07-22 18:16:33 +00:00
const message = getMessageById(db, messageId);
2022-02-16 18:36:21 +00:00
if (!message) {
logger.warn(
`updateToSchemaVersion51: Unable to find message for reaction job ${id}`
);
return;
}
const { conversationId } = message;
if (typeof conversationId !== 'string') {
logger.warn(
`updateToSchemaVersion51: reactions queue job ${id} had a non-string conversationId`
);
return;
}
const newJob = {
...job,
queueType: 'conversation',
data: {
...data,
type: 'Reaction',
conversationId,
},
};
2024-07-22 18:16:33 +00:00
insertJob(db, newJob);
2022-02-16 18:36:21 +00:00
});
// Then make sure all normal send job data has a type
2024-07-22 18:16:33 +00:00
const normalSendJobs = getJobsInQueue(db, 'normal send');
2022-02-16 18:36:21 +00:00
deleteJobsInQueue.run({ queueType: 'normal send' });
normalSendJobs.forEach(job => {
const { data, id } = job;
if (!isRecord(data)) {
logger.warn(
`updateToSchemaVersion51: normal send queue job ${id} was missing valid data`
);
return;
}
const newJob = {
...job,
queueType: 'conversation',
data: {
...data,
type: 'NormalMessage',
},
};
2024-07-22 18:16:33 +00:00
insertJob(db, newJob);
2022-02-16 18:36:21 +00:00
});
db.pragma('user_version = 51');
})();
logger.info('updateToSchemaVersion51: success!');
}