mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2025-12-24 20:26:24 +00:00
Normalize messages table
This commit is contained in:
@@ -6,11 +6,11 @@ import { v4 as generateGuid } from 'uuid';
|
||||
|
||||
import {
|
||||
dequeueOldestSyncTasks,
|
||||
getMostRecentAddressableMessages,
|
||||
removeSyncTaskById,
|
||||
saveSyncTasks,
|
||||
} from '../../sql/Server';
|
||||
import type { WritableDB } from '../../sql/Interface';
|
||||
import type { WritableDB, ReadableDB, MessageType } from '../../sql/Interface';
|
||||
import { sql, jsonToObject } from '../../sql/util';
|
||||
import { insertData, updateToVersion, createDB } from './helpers';
|
||||
import { MAX_SYNC_TASK_ATTEMPTS } from '../../util/syncTasks.types';
|
||||
import { WEEK } from '../../util/durations';
|
||||
@@ -20,6 +20,27 @@ import type { SyncTaskType } from '../../util/syncTasks';
|
||||
|
||||
/* eslint-disable camelcase */
|
||||
|
||||
// Snapshot before: 1270
|
||||
export function getMostRecentAddressableMessages(
|
||||
db: ReadableDB,
|
||||
conversationId: string,
|
||||
limit = 5
|
||||
): Array<MessageType> {
|
||||
const [query, parameters] = sql`
|
||||
SELECT json FROM messages
|
||||
INDEXED BY messages_by_date_addressable
|
||||
WHERE
|
||||
conversationId IS ${conversationId} AND
|
||||
isAddressableMessage = 1
|
||||
ORDER BY received_at DESC, sent_at DESC
|
||||
LIMIT ${limit};
|
||||
`;
|
||||
|
||||
const rows = db.prepare(query).all(parameters);
|
||||
|
||||
return rows.map(row => jsonToObject(row.json));
|
||||
}
|
||||
|
||||
function generateMessage(json: MessageAttributesType) {
|
||||
const { conversationId, received_at, sent_at, type } = json;
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
import { assert } from 'chai';
|
||||
import { v4 as generateGuid } from 'uuid';
|
||||
|
||||
import type { WritableDB } from '../../sql/Interface';
|
||||
import { getMostRecentAddressableNondisappearingMessages } from '../../sql/Server';
|
||||
import type { WritableDB, ReadableDB, MessageType } from '../../sql/Interface';
|
||||
import { sql, jsonToObject } from '../../sql/util';
|
||||
import { createDB, insertData, updateToVersion } from './helpers';
|
||||
|
||||
import type { MessageAttributesType } from '../../model-types';
|
||||
@@ -26,6 +26,28 @@ function generateMessage(json: MessageAttributesType) {
|
||||
};
|
||||
}
|
||||
|
||||
// Snapshot before: 1270
|
||||
export function getMostRecentAddressableNondisappearingMessages(
|
||||
db: ReadableDB,
|
||||
conversationId: string,
|
||||
limit = 5
|
||||
): Array<MessageType> {
|
||||
const [query, parameters] = sql`
|
||||
SELECT json FROM messages
|
||||
INDEXED BY messages_by_date_addressable_nondisappearing
|
||||
WHERE
|
||||
expireTimer IS NULL AND
|
||||
conversationId IS ${conversationId} AND
|
||||
isAddressableMessage = 1
|
||||
ORDER BY received_at DESC, sent_at DESC
|
||||
LIMIT ${limit};
|
||||
`;
|
||||
|
||||
const rows = db.prepare(query).all(parameters);
|
||||
|
||||
return rows.map(row => jsonToObject(row.json));
|
||||
}
|
||||
|
||||
describe('SQL/updateToSchemaVersion1080', () => {
|
||||
let db: WritableDB;
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -1351,10 +1351,8 @@ describe('SQL migrations test', () => {
|
||||
db.exec(
|
||||
`
|
||||
INSERT INTO messages
|
||||
(id, json)
|
||||
VALUES ('${MESSAGE_ID_1}', '${JSON.stringify({
|
||||
conversationId: CONVERSATION_ID_1,
|
||||
})}')
|
||||
(id, conversationId)
|
||||
VALUES ('${MESSAGE_ID_1}', '${CONVERSATION_ID_1}');
|
||||
`
|
||||
);
|
||||
|
||||
@@ -2482,10 +2480,8 @@ describe('SQL migrations test', () => {
|
||||
db.exec(
|
||||
`
|
||||
INSERT INTO messages
|
||||
(id, json)
|
||||
VALUES ('${MESSAGE_ID_1}', '${JSON.stringify({
|
||||
conversationId: CONVERSATION_ID_1,
|
||||
})}')
|
||||
(id, conversationId)
|
||||
VALUES ('${MESSAGE_ID_1}', '${CONVERSATION_ID_1}');
|
||||
`
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user