Fix migration for conversations with null json

This commit is contained in:
Fedor Indutny
2023-09-07 00:58:45 +02:00
committed by GitHub
parent bf6dc0b808
commit 1d07203fa9

View File

@@ -5,6 +5,8 @@ import type { Database } from '@signalapp/better-sqlite3';
import { callIdFromEra } from '@signalapp/ringrtc'; import { callIdFromEra } from '@signalapp/ringrtc';
import Long from 'long'; import Long from 'long';
import { v4 as generateUuid } from 'uuid'; import { v4 as generateUuid } from 'uuid';
import { isObject } from 'lodash';
import type { SetOptional } from 'type-fest'; import type { SetOptional } from 'type-fest';
import type { LoggerType } from '../../types/Logging'; import type { LoggerType } from '../../types/Logging';
import { jsonToObject, sql } from '../util'; import { jsonToObject, sql } from '../util';
@@ -248,6 +250,7 @@ export default function updateToSchemaVersion89(
const [selectQuery] = sql` const [selectQuery] = sql`
SELECT SELECT
messages.json AS messageJson, messages.json AS messageJson,
conversations.id AS conversationId,
conversations.json AS conversationJson conversations.json AS conversationJson
FROM messages FROM messages
LEFT JOIN conversations ON conversations.id = messages.conversationId LEFT JOIN conversations ON conversations.id = messages.conversationId
@@ -261,16 +264,25 @@ export default function updateToSchemaVersion89(
// Must match query above // Must match query above
type CallHistoryRow = { type CallHistoryRow = {
messageJson: string; messageJson: string;
conversationId: string;
conversationJson: string; conversationJson: string;
}; };
const rows: Array<CallHistoryRow> = db.prepare(selectQuery).all(); const rows: Array<CallHistoryRow> = db.prepare(selectQuery).all();
for (const row of rows) { for (const row of rows) {
const { messageJson, conversationJson } = row; const { messageJson, conversationId, conversationJson } = row;
const message = jsonToObject<MessageWithCallHistoryDetails>(messageJson); const message = jsonToObject<MessageWithCallHistoryDetails>(messageJson);
const conversation = jsonToObject<ConversationType>(conversationJson); const conversation = jsonToObject<ConversationType>(conversationJson);
if (!isObject(conversation)) {
logger.warn(
`updateToSchemaVersion89: Private conversation (${conversationId}) ` +
'has non-object json column'
);
continue;
}
const details = message.callHistoryDetails; const details = message.callHistoryDetails;
const peerId = getPeerIdFromConversation(conversation, logger); const peerId = getPeerIdFromConversation(conversation, logger);