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

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