Normalize messages table
This commit is contained in:
parent
9bec59b70a
commit
630a1fcc89
18 changed files with 490 additions and 240 deletions
|
@ -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}');
|
||||
`
|
||||
);
|
||||
|
||||
|
|
|
@ -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 = 'It’s 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(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue