Fix messages_unread index
This commit is contained in:
parent
fa8ff2ae4c
commit
a17e157e7b
3 changed files with 61 additions and 0 deletions
32
ts/sql/migrations/50-fix-messages-unread-index.ts
Normal file
32
ts/sql/migrations/50-fix-messages-unread-index.ts
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
// Copyright 2021 Signal Messenger, LLC
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
import type { Database } from 'better-sqlite3';
|
||||||
|
|
||||||
|
import type { LoggerType } from '../../types/Logging';
|
||||||
|
|
||||||
|
export default function updateToSchemaVersion50(
|
||||||
|
currentVersion: number,
|
||||||
|
db: Database,
|
||||||
|
logger: LoggerType
|
||||||
|
): void {
|
||||||
|
if (currentVersion >= 50) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
db.transaction(() => {
|
||||||
|
db.exec(
|
||||||
|
`
|
||||||
|
DROP INDEX messages_unread;
|
||||||
|
|
||||||
|
-- Note: here we move to the modern isStory/storyId fields and add received_at/sent_at.
|
||||||
|
CREATE INDEX messages_unread ON messages
|
||||||
|
(conversationId, readStatus, isStory, storyId, received_at, sent_at) WHERE readStatus IS NOT NULL;
|
||||||
|
`
|
||||||
|
);
|
||||||
|
|
||||||
|
db.pragma('user_version = 50');
|
||||||
|
})();
|
||||||
|
|
||||||
|
logger.info('updateToSchemaVersion50: success!');
|
||||||
|
}
|
|
@ -25,6 +25,7 @@ import updateToSchemaVersion46 from './46-optimize-stories';
|
||||||
import updateToSchemaVersion47 from './47-further-optimize';
|
import updateToSchemaVersion47 from './47-further-optimize';
|
||||||
import updateToSchemaVersion48 from './48-fix-user-initiated-index';
|
import updateToSchemaVersion48 from './48-fix-user-initiated-index';
|
||||||
import updateToSchemaVersion49 from './49-fix-preview-index';
|
import updateToSchemaVersion49 from './49-fix-preview-index';
|
||||||
|
import updateToSchemaVersion50 from './50-fix-messages-unread-index';
|
||||||
|
|
||||||
function updateToSchemaVersion1(
|
function updateToSchemaVersion1(
|
||||||
currentVersion: number,
|
currentVersion: number,
|
||||||
|
@ -1913,6 +1914,7 @@ export const SCHEMA_VERSIONS = [
|
||||||
updateToSchemaVersion47,
|
updateToSchemaVersion47,
|
||||||
updateToSchemaVersion48,
|
updateToSchemaVersion48,
|
||||||
updateToSchemaVersion49,
|
updateToSchemaVersion49,
|
||||||
|
updateToSchemaVersion50,
|
||||||
];
|
];
|
||||||
|
|
||||||
export function updateSchema(db: Database, logger: LoggerType): void {
|
export function updateSchema(db: Database, logger: LoggerType): void {
|
||||||
|
|
|
@ -1324,4 +1324,31 @@ describe('SQL migrations test', () => {
|
||||||
assert.notInclude(details, 'SCAN');
|
assert.notInclude(details, 'SCAN');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('updateToSchemaVersion49', () => {
|
||||||
|
it('creates usable index for messages_unread', () => {
|
||||||
|
updateToVersion(50);
|
||||||
|
|
||||||
|
const details = db
|
||||||
|
.prepare(
|
||||||
|
`
|
||||||
|
EXPLAIN QUERY PLAN
|
||||||
|
SELECT * FROM messages WHERE
|
||||||
|
conversationId = 'conversation' AND
|
||||||
|
readStatus = 'something' AND
|
||||||
|
isStory IS 0 AND
|
||||||
|
storyId IS NULL
|
||||||
|
ORDER BY received_at ASC, sent_at ASC
|
||||||
|
LIMIT 1;
|
||||||
|
`
|
||||||
|
)
|
||||||
|
.all()
|
||||||
|
.map(({ detail }) => detail)
|
||||||
|
.join('\n');
|
||||||
|
|
||||||
|
assert.include(details, 'USING INDEX messages_unread');
|
||||||
|
assert.notInclude(details, 'TEMP B-TREE');
|
||||||
|
assert.notInclude(details, 'SCAN');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue