Normalize messages table

This commit is contained in:
Fedor Indutny 2025-01-16 13:34:35 -08:00 committed by GitHub
parent 9bec59b70a
commit 630a1fcc89
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 490 additions and 240 deletions

View file

@ -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;

View file

@ -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(() => {

View file

@ -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}');
`
);

View file

@ -126,11 +126,9 @@ describe('Message', () => {
it('should initialize schema version to zero', () => {
const input = getDefaultMessage({
body: 'Imagine there is no heaven…',
attachments: [],
});
const expected = getDefaultMessage({
body: 'Imagine there is no heaven…',
attachments: [],
schemaVersion: 0,
});
@ -203,7 +201,6 @@ describe('Message', () => {
hasVisualMediaAttachments: undefined,
hasFileAttachments: undefined,
schemaVersion: Message.CURRENT_SCHEMA_VERSION,
contact: [],
});
const expectedAttachmentData = 'Its easy if you try';
@ -655,7 +652,6 @@ describe('Message', () => {
});
const expected = getDefaultMessage({
body: 'hey there!',
contact: [],
});
const result = await upgradeVersion(message, getDefaultContext());
assert.deepEqual(result, expected);
@ -848,7 +844,6 @@ describe('Message', () => {
key: 'key',
},
],
contact: [],
});
const result = await Message.upgradeSchema(message, {
...getDefaultContext(),