Add indexes to support messages foreign key constraints
This commit is contained in:
parent
28664a606f
commit
de0f49ed42
3 changed files with 90 additions and 3 deletions
33
ts/sql/migrations/1120-messages-foreign-keys-indexes.ts
Normal file
33
ts/sql/migrations/1120-messages-foreign-keys-indexes.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { Database } from '@signalapp/better-sqlite3';
|
||||
|
||||
import type { LoggerType } from '../../types/Logging';
|
||||
|
||||
export const version = 1120;
|
||||
|
||||
export function updateToSchemaVersion1120(
|
||||
currentVersion: number,
|
||||
db: Database,
|
||||
logger: LoggerType
|
||||
): void {
|
||||
if (currentVersion >= 1120) {
|
||||
return;
|
||||
}
|
||||
|
||||
db.transaction(() => {
|
||||
/** Adds indexes for all tables with foreign key relationships to messages(id) */
|
||||
db.exec(`
|
||||
CREATE INDEX edited_messages_messageId
|
||||
ON edited_messages(messageId);
|
||||
|
||||
CREATE INDEX mentions_messageId
|
||||
ON mentions(messageId);
|
||||
`);
|
||||
})();
|
||||
|
||||
db.pragma('user_version = 1120');
|
||||
|
||||
logger.info('updateToSchemaVersion1120: success!');
|
||||
}
|
|
@ -86,10 +86,11 @@ import { updateToSchemaVersion1070 } from './1070-attachment-backup';
|
|||
import { updateToSchemaVersion1080 } from './1080-nondisappearing-addressable';
|
||||
import { updateToSchemaVersion1090 } from './1090-message-delete-indexes';
|
||||
import { updateToSchemaVersion1100 } from './1100-optimize-mark-call-history-read-in-conversation';
|
||||
import { updateToSchemaVersion1110 } from './1110-sticker-local-key';
|
||||
import {
|
||||
updateToSchemaVersion1110,
|
||||
updateToSchemaVersion1120,
|
||||
version as MAX_VERSION,
|
||||
} from './1110-sticker-local-key';
|
||||
} from './1120-messages-foreign-keys-indexes';
|
||||
|
||||
function updateToSchemaVersion1(
|
||||
currentVersion: number,
|
||||
|
@ -2042,9 +2043,9 @@ export const SCHEMA_VERSIONS = [
|
|||
updateToSchemaVersion1070,
|
||||
updateToSchemaVersion1080,
|
||||
updateToSchemaVersion1090,
|
||||
|
||||
updateToSchemaVersion1100,
|
||||
updateToSchemaVersion1110,
|
||||
updateToSchemaVersion1120,
|
||||
];
|
||||
|
||||
export class DBVersionFromFutureError extends Error {
|
||||
|
|
53
ts/test-node/sql/migration_1120_test.ts
Normal file
53
ts/test-node/sql/migration_1120_test.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { assert } from 'chai';
|
||||
import type { Database } from '@signalapp/better-sqlite3';
|
||||
import SQL from '@signalapp/better-sqlite3';
|
||||
import { updateToVersion } from './helpers';
|
||||
|
||||
describe('SQL/updateToSchemaVersion1120', () => {
|
||||
let db: Database;
|
||||
beforeEach(() => {
|
||||
db = new SQL(':memory:');
|
||||
updateToVersion(db, 1120);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
db.close();
|
||||
});
|
||||
|
||||
it('uses index for deleting edited messages', () => {
|
||||
const details = db
|
||||
.prepare(
|
||||
`EXPLAIN QUERY PLAN
|
||||
DELETE FROM edited_messages WHERE messageId = 'messageId';
|
||||
`
|
||||
)
|
||||
.all()
|
||||
.map(step => step.detail)
|
||||
.join(', ');
|
||||
|
||||
assert.strictEqual(
|
||||
details,
|
||||
'SEARCH edited_messages USING COVERING INDEX edited_messages_messageId (messageId=?)'
|
||||
);
|
||||
});
|
||||
|
||||
it('uses index for deleting mentions', () => {
|
||||
const details = db
|
||||
.prepare(
|
||||
`EXPLAIN QUERY PLAN
|
||||
DELETE FROM mentions WHERE messageId = 'messageId';
|
||||
`
|
||||
)
|
||||
.all()
|
||||
.map(step => step.detail)
|
||||
.join(', ');
|
||||
|
||||
assert.strictEqual(
|
||||
details,
|
||||
'SEARCH mentions USING COVERING INDEX mentions_messageId (messageId=?)'
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue