Use @signalapp/sqlcipher

This commit is contained in:
Fedor Indutny
2025-03-12 14:45:54 -07:00
committed by GitHub
parent df7997b313
commit 5a9253bd44
118 changed files with 2497 additions and 1485 deletions

View File

@@ -1,7 +1,7 @@
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { ReadonlyDeep } from 'type-fest';
import type {
ConversationAttributesType,
@@ -236,6 +236,13 @@ export type SentProtoType = {
urgent: boolean;
hasPniSignatureMessage: boolean;
};
export type SentProtoDBType = {
contentHint: number;
proto: Uint8Array;
timestamp: number;
urgent: number;
hasPniSignatureMessage: number;
};
export type SentProtoWithMessageIdsType = SentProtoType & {
messageIds: Array<string>;
};
@@ -676,14 +683,14 @@ type ReadableInterface = {
) => Array<MessageType>;
getUnprocessedCount: () => number;
getUnprocessedById: (id: string) => UnprocessedType | undefined;
getAttachmentDownloadJob(
// Test-only
_getAttachmentDownloadJob(
job: Pick<
AttachmentDownloadJobType,
'messageId' | 'attachmentType' | 'digest'
>
): AttachmentDownloadJobType;
): AttachmentDownloadJobType | undefined;
getBackupCdnObjectMetadata: (
mediaId: string

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { ReadStatus } from '../../messages/MessageReadStatus';

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { sql } from '../util';

View File

@@ -30,7 +30,11 @@ export function updateToSchemaVersion1020(
SELECT id FROM conversations
WHERE serviceId IS ${ourAci}
`;
const ourConversationId = db.prepare(selectQuery).pluck().get(selectParams);
const ourConversationId = db
.prepare(selectQuery, {
pluck: true,
})
.get(selectParams);
if (ourConversationId == null) {
logger.error('updateToSchemaVersion1020: no conversation');
db.pragma('user_version = 1020');

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { sql, sqlFragment } from '../util';

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import {

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { sql } from '../util';

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { sql } from '../util';

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,6 +1,6 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
export const version = 1140;

View File

@@ -1,6 +1,6 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
export const version = 1150;

View File

@@ -1,6 +1,6 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { sql, sqlConstant } from '../util';
import { CallDirection, CallStatusValue } from '../../types/CallDisposition';

View File

@@ -1,6 +1,6 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { sql } from '../util';

View File

@@ -1,6 +1,6 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { AttachmentDownloadSource } from '../Interface';

View File

@@ -1,6 +1,6 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
export const version = 1190;

View File

@@ -1,6 +1,6 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
export const version = 1200;

View File

@@ -1,6 +1,6 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
export const version = 1210;

View File

@@ -2,7 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
import assert from 'assert';
import z from 'zod';
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import * as Errors from '../../types/errors';
import {
@@ -144,16 +144,17 @@ export function updateToSchemaVersion1220(
) STRICT;
`);
const getItem = db
.prepare(
`
SELECT json -> '$.value' FROM items WHERE id IS ?
`
)
.pluck();
const getItem = db.prepare(
`
SELECT json -> '$.value' FROM items WHERE id IS ?
`,
{
pluck: true,
}
);
const identityKeyMapJson = getItem.get('identityKeyMap');
const registrationIdMapJson = getItem.get('registrationIdMap');
const identityKeyMapJson = getItem.get<string>(['identityKeyMap']);
const registrationIdMapJson = getItem.get<string>(['registrationIdMap']);
// If we don't have private keys - the sessions cannot be used anyway
if (!identityKeyMapJson || !registrationIdMapJson) {

View File

@@ -1,6 +1,6 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
export const version = 1230;

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { sql } from '../util';

View File

@@ -1,6 +1,6 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
export const version = 1250;

View File

@@ -1,6 +1,6 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { sql } from '../util';

View File

@@ -1,6 +1,6 @@
// Copyright 2025 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { sql } from '../util';

View File

@@ -116,20 +116,22 @@ export function updateToSchemaVersion1280(
}
try {
const decoded = Proto.Envelope.decode(Buffer.from(envelope, 'base64'));
const decoded = Proto.Envelope.decode(
Buffer.from(String(envelope), 'base64')
);
if (!decoded.content) {
throw new Error('Missing envelope content');
}
const content = decrypted
? Buffer.from(decrypted, 'base64')
? Buffer.from(String(decrypted), 'base64')
: decoded.content;
insertStmt.run({
...rest,
id,
type: decoded.type ?? Proto.Envelope.Type.UNKNOWN,
content,
content: content ?? null,
isEncrypted: decrypted ? 0 : 1,
timestamp: timestamp || Date.now(),
attempts: attempts || 0,
@@ -138,16 +140,17 @@ export function updateToSchemaVersion1280(
story: story ? 1 : 0,
serverGuid: serverGuid || getGuid(),
serverTimestamp: serverTimestamp || 0,
destinationServiceId: normalizeServiceId(
decoded.destinationServiceId || ourAci,
'Envelope.destinationServiceId'
),
destinationServiceId:
normalizeServiceId(
decoded.destinationServiceId || ourAci,
'Envelope.destinationServiceId'
) ?? null,
updatedPni: isUntaggedPniString(decoded.updatedPni)
? normalizePni(
toTaggedPni(decoded.updatedPni),
'Envelope.updatedPni'
)
: undefined,
: null,
// Sadly not captured previously
messageAgeSec: 0,
reportingToken: decoded.reportSpamToken?.length

View File

@@ -1,6 +1,6 @@
// Copyright 2025 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { sql } from '../util';

View File

@@ -5,14 +5,13 @@ import type { LoggerType } from '../../types/Logging';
import { isValidUuid } from '../../util/isValidUuid';
import Helpers from '../../textsecure/Helpers';
import { createOrUpdate, getById, removeById } from '../util';
import type { EmptyQuery, Query } from '../util';
import type { ItemKeyType, ReadableDB, WritableDB } from '../Interface';
export function getOurUuid(db: ReadableDB): string | undefined {
const UUID_ID: ItemKeyType = 'uuid_id';
const row: { json: string } | undefined = db
.prepare<Query>('SELECT json FROM items WHERE id = $id;')
.prepare('SELECT json FROM items WHERE id = $id;')
.get({ id: UUID_ID });
if (!row) {
@@ -34,19 +33,20 @@ export default function updateToSchemaVersion41(
return;
}
const getConversationUuid = db
.prepare<Query>(
`
SELECT uuid
FROM
conversations
WHERE
id = $conversationId
`
)
.pluck();
const getConversationUuid = db.prepare(
`
SELECT uuid
FROM
conversations
WHERE
id = $conversationId
`,
{
pluck: true,
}
);
const getConversationStats = db.prepare<Query>(
const getConversationStats = db.prepare(
`
SELECT uuid, e164, active_at
FROM
@@ -56,9 +56,15 @@ export default function updateToSchemaVersion41(
`
);
type StatsType = {
uuid: string;
e164: string;
active_at: number;
};
const compareConvoRecency = (a: string, b: string): number => {
const aStats = getConversationStats.get({ conversationId: a });
const bStats = getConversationStats.get({ conversationId: b });
const aStats = getConversationStats.get<StatsType>({ conversationId: a });
const bStats = getConversationStats.get<StatsType>({ conversationId: b });
const isAComplete = Boolean(aStats?.uuid && aStats?.e164);
const isBComplete = Boolean(bStats?.uuid && bStats?.e164);
@@ -73,7 +79,7 @@ export default function updateToSchemaVersion41(
return 1;
}
return aStats.active_at - bStats.active_at;
return (aStats?.active_at ?? 0) - (bStats?.active_at ?? 0);
};
const clearSessionsAndKeys = (): number => {
@@ -141,7 +147,7 @@ export default function updateToSchemaVersion41(
const prefixKeys = (ourUuid: string) => {
for (const table of ['signedPreKeys', 'preKeys']) {
// Update id to include suffix, add `ourUuid` and `keyId` fields.
db.prepare<Query>(
db.prepare(
`
UPDATE ${table}
SET
@@ -166,14 +172,12 @@ export default function updateToSchemaVersion41(
senderId: string;
lastUpdatedDate: number;
}> = db
.prepare<EmptyQuery>(
'SELECT id, senderId, lastUpdatedDate FROM senderKeys'
)
.prepare('SELECT id, senderId, lastUpdatedDate FROM senderKeys')
.all();
logger.info(`Updating ${senderKeys.length} sender keys`);
const updateSenderKey = db.prepare<Query>(
const updateSenderKey = db.prepare(
`
UPDATE senderKeys
SET
@@ -184,9 +188,7 @@ export default function updateToSchemaVersion41(
`
);
const deleteSenderKey = db.prepare<Query>(
'DELETE FROM senderKeys WHERE id = $id'
);
const deleteSenderKey = db.prepare('DELETE FROM senderKeys WHERE id = $id');
const pastKeys = new Map<
string,
@@ -201,7 +203,7 @@ export default function updateToSchemaVersion41(
let skipped = 0;
for (const { id, senderId, lastUpdatedDate } of senderKeys) {
const [conversationId] = Helpers.unencodeNumber(senderId);
const uuid = getConversationUuid.get({ conversationId });
const uuid = getConversationUuid.get<string>({ conversationId });
if (!uuid) {
deleted += 1;
@@ -252,12 +254,12 @@ export default function updateToSchemaVersion41(
//
// Set ourUuid column and field in json
const allSessions = db
.prepare<EmptyQuery>('SELECT id, conversationId FROM SESSIONS')
.all();
.prepare('SELECT id, conversationId FROM SESSIONS')
.all<{ id: string; conversationId: string }>();
logger.info(`Updating ${allSessions.length} sessions`);
const updateSession = db.prepare<Query>(
const updateSession = db.prepare(
`
UPDATE sessions
SET
@@ -278,9 +280,7 @@ export default function updateToSchemaVersion41(
`
);
const deleteSession = db.prepare<Query>(
'DELETE FROM sessions WHERE id = $id'
);
const deleteSession = db.prepare('DELETE FROM sessions WHERE id = $id');
const pastSessions = new Map<
string,
@@ -293,7 +293,7 @@ export default function updateToSchemaVersion41(
let deleted = 0;
let skipped = 0;
for (const { id, conversationId } of allSessions) {
const uuid = getConversationUuid.get({ conversationId });
const uuid = getConversationUuid.get<string>({ conversationId });
if (!uuid) {
deleted += 1;
deleteSession.run({ id });
@@ -338,13 +338,13 @@ export default function updateToSchemaVersion41(
};
const updateIdentityKeys = () => {
const identityKeys: ReadonlyArray<{
id: string;
}> = db.prepare<EmptyQuery>('SELECT id FROM identityKeys').all();
const identityKeys = db
.prepare('SELECT id FROM identityKeys')
.all<{ id: string }>();
logger.info(`Updating ${identityKeys.length} identity keys`);
const updateIdentityKey = db.prepare<Query>(
const updateIdentityKey = db.prepare(
`
UPDATE OR REPLACE identityKeys
SET
@@ -361,7 +361,7 @@ export default function updateToSchemaVersion41(
let migrated = 0;
for (const { id } of identityKeys) {
const uuid = getConversationUuid.get({ conversationId: id });
const uuid = getConversationUuid.get<string>({ conversationId: id });
let newId: string;
if (uuid) {

View File

@@ -2,7 +2,6 @@
// SPDX-License-Identifier: AGPL-3.0-only
import { batchMultiVarQuery } from '../util';
import type { ArrayQuery } from '../util';
import type { WritableDB } from '../Interface';
import type { LoggerType } from '../../types/Logging';
@@ -39,8 +38,9 @@ export default function updateToSchemaVersion42(
// Note: we use `pluck` here to fetch only the first column of
// returned row.
const messageIdList: Array<string> = db
.prepare('SELECT id FROM messages ORDER BY id ASC;')
.pluck()
.prepare('SELECT id FROM messages ORDER BY id ASC;', {
pluck: true,
})
.all();
const allReactions: Array<{
rowid: number;
@@ -57,7 +57,7 @@ export default function updateToSchemaVersion42(
});
function deleteReactions(rowids: ReadonlyArray<number>) {
db.prepare<ArrayQuery>(
db.prepare(
`
DELETE FROM reactions
WHERE rowid IN ( ${rowids.map(() => '?').join(', ')} );

View File

@@ -14,7 +14,6 @@ import {
jsonToObject,
objectToJSON,
} from '../util';
import type { EmptyQuery, Query } from '../util';
import type { WritableDB } from '../Interface';
type MessageType = Readonly<{
@@ -61,17 +60,18 @@ export default function updateToSchemaVersion43(
pendingAdminApprovalV2?: Array<LegacyAdminApprovalType>;
};
const getConversationUuid = db
.prepare<Query>(
`
SELECT uuid
FROM
conversations
WHERE
id = $conversationId
`
)
.pluck();
const getConversationUuid = db.prepare(
`
SELECT uuid
FROM
conversations
WHERE
id = $conversationId
`,
{
pluck: true,
}
);
const updateConversationStmt = db.prepare(
`
@@ -112,7 +112,7 @@ export default function updateToSchemaVersion43(
const newValue = oldValue
.map(member => {
const uuid: ServiceIdString = getConversationUuid.get({
const uuid = getConversationUuid.get<ServiceIdString>({
conversationId: member.conversationId,
});
if (!uuid) {
@@ -278,7 +278,7 @@ export default function updateToSchemaVersion43(
}
changedDetails = true;
const newValue: ServiceIdString | null = getConversationUuid.get({
const newValue = getConversationUuid.get<ServiceIdString>({
conversationId: oldValue,
});
if (key === 'inviter' && !newValue) {
@@ -318,7 +318,7 @@ export default function updateToSchemaVersion43(
}
if (sourceUuid) {
const newValue: ServiceIdString | null = getConversationUuid.get({
const newValue = getConversationUuid.get<ServiceIdString>({
conversationId: sourceUuid,
});
@@ -333,7 +333,7 @@ export default function updateToSchemaVersion43(
if (invitedGV2Members) {
const newMembers = invitedGV2Members
.map(({ addedByUserId, conversationId }, i) => {
const uuid: ServiceIdString | null = getConversationUuid.get({
const uuid = getConversationUuid.get<ServiceIdString>({
conversationId,
});
const oldMember =
@@ -357,7 +357,7 @@ export default function updateToSchemaVersion43(
return newMember;
}
const newAddedBy: ServiceIdString | null = getConversationUuid.get({
const newAddedBy = getConversationUuid.get<ServiceIdString>({
conversationId: addedByUserId,
});
if (!newAddedBy) {
@@ -392,15 +392,16 @@ export default function updateToSchemaVersion43(
db.transaction(() => {
const allConversations = db
.prepare<EmptyQuery>(
.prepare(
`
SELECT json, profileLastFetchedAt
SELECT json
FROM conversations
ORDER BY id ASC;
`
`,
{ pluck: true }
)
.all()
.map(({ json }) => jsonToObject<ConversationType>(json));
.all<string>()
.map(json => jsonToObject<ConversationType>(json));
logger.info(
'updateToSchemaVersion43: About to iterate through ' +

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -4,7 +4,6 @@
import type { LoggerType } from '../../types/Logging';
import { getOurUuid } from './41-uuid-keys';
import type { WritableDB } from '../Interface';
import type { Query } from '../util';
export default function updateToSchemaVersion47(
currentVersion: number,
@@ -123,7 +122,7 @@ export default function updateToSchemaVersion47(
if (!ourUuid) {
logger.info('updateToSchemaVersion47: our UUID not found');
} else {
db.prepare<Query>(
db.prepare(
`
UPDATE messages SET
isChangeCreatedByUs = json_extract(json, '$.groupV2Change.from') IS $ourUuid;

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -46,7 +46,7 @@ export default function updateToSchemaVersion51(
return;
}
const message = getMessageById.get(messageId);
const message = getMessageById.get([messageId]);
if (!message) {
logger.warn(
`updateToSchemaVersion51: Unable to find message for reaction job ${id}`

View File

@@ -1,7 +1,7 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,11 +1,10 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { jsonToObject } from '../util';
import type { EmptyQuery } from '../util';
export default function updateToSchemaVersion53(
currentVersion: number,
@@ -67,16 +66,17 @@ export default function updateToSchemaVersion53(
db.transaction(() => {
const allConversations = db
.prepare<EmptyQuery>(
.prepare(
`
SELECT json, profileLastFetchedAt
SELECT json
FROM conversations
WHERE type = 'group'
ORDER BY id ASC;
`
`,
{ pluck: true }
)
.all()
.map(({ json }) => jsonToObject<ConversationType>(json));
.all<string>()
.map(json => jsonToObject<ConversationType>(json));
logger.info(
'updateToSchemaVersion53: About to iterate through ' +

View File

@@ -1,7 +1,7 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import { ReadStatus } from '../../messages/MessageReadStatus';
import { SeenStatus } from '../../MessageSeenStatus';

View File

@@ -1,7 +1,7 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import { ReadStatus } from '../../messages/MessageReadStatus';
import { SeenStatus } from '../../MessageSeenStatus';

View File

@@ -1,7 +1,7 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
export default function updateToSchemaVersion72(

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
@@ -18,7 +18,7 @@ export default function updateToSchemaVersion77(
db.exec(
`
-- Create FTS table with custom tokenizer from
-- @signalapp/better-sqlite3.
-- @signalapp/sqlcipher.
DROP TABLE messages_fts;

View File

@@ -66,7 +66,7 @@ export default function updateToSchemaVersion78(
return;
}
const message = getMessageById.get(messageId);
const message = getMessageById.get([messageId]);
if (!message) {
logger.warn(
`updateToSchemaVersion78: Unable to find message for ${queue.queueType} job ${id}`

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
export default function updateToSchemaVersion83(

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
export default function updateToSchemaVersion84(

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { cleanKeys } from './920-clean-more-keys';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import { omit } from 'lodash';
import type { LoggerType } from '../../types/Logging';
@@ -484,27 +484,31 @@ function migrateItems(db: Database, logger: LoggerType): OurServiceIds {
const uuidIdJson = db
.prepare(
`
SELECT json
FROM items
WHERE id IS 'uuid_id'
`
SELECT json
FROM items
WHERE id IS 'uuid_id'
`,
{
pluck: true,
}
)
.pluck()
.get();
.get<string>();
const pniJson = db
.prepare(
`
SELECT json
FROM items
WHERE id IS 'pni'
`
SELECT json
FROM items
WHERE id IS 'pni'
`,
{
pluck: true,
}
)
.pluck()
.get();
.get<string>();
let legacyAci: string | undefined;
try {
[legacyAci] = JSON.parse(uuidIdJson).value.split('.', 2);
[legacyAci] = JSON.parse(uuidIdJson ?? '').value.split('.', 2);
} catch (error) {
if (uuidIdJson) {
logger.warn(
@@ -518,7 +522,7 @@ function migrateItems(db: Database, logger: LoggerType): OurServiceIds {
let legacyPni: string | undefined;
try {
legacyPni = JSON.parse(pniJson).value;
legacyPni = JSON.parse(pniJson ?? '').value;
} catch (error) {
if (pniJson) {
logger.warn('updateToSchemaVersion88: failed to parse pni item', error);
@@ -852,7 +856,10 @@ function migratePreKeys(
ourServiceIds: OurServiceIds,
logger: LoggerType
): void {
const preKeys = db.prepare(`SELECT id, json FROM ${table}`).all();
const preKeys = db.prepare(`SELECT id, json FROM ${table}`).all<{
id: string;
json: string;
}>();
const updateStmt = db.prepare(`
UPDATE ${table}
@@ -1001,7 +1008,11 @@ function migrateJobs(
identifierToServiceId: Map<string, ServiceIdString>,
logger: LoggerType
): void {
const jobs = db.prepare('SELECT id, queueType, data FROM jobs').all();
const jobs = db.prepare('SELECT id, queueType, data FROM jobs').all<{
id: string;
queueType: string;
data: string;
}>();
const updateStmt = db.prepare('UPDATE jobs SET data = $data WHERE id IS $id');
let updatedCount = 0;

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { sql } from '../util';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database, RunResult } from '@signalapp/better-sqlite3';
import type { Database, RunResult } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import { sql } from '../util';
@@ -60,10 +60,12 @@ export default function updateToSchemaVersion91(
// Do overall count - if it's less than 1000, move on
const totalKeys = db
.prepare('SELECT count(*) FROM preKeys;')
.pluck(true)
.get();
const totalKeys =
db
.prepare('SELECT count(*) FROM preKeys;', {
pluck: true,
})
.get<number>() ?? 0;
logger.info(`updateToSchemaVersion91: Found ${totalKeys} keys`);
if (totalKeys < 1000) {
db.pragma('user_version = 91');
@@ -74,11 +76,12 @@ export default function updateToSchemaVersion91(
let pni: PniString;
const pniJson = db
.prepare("SELECT json FROM items WHERE id IS 'pni'")
.pluck()
.get();
.prepare("SELECT json FROM items WHERE id IS 'pni'", {
pluck: true,
})
.get<string>();
try {
const pniData = JSON.parse(pniJson);
const pniData = JSON.parse(pniJson ?? '');
pni = normalizePni(pniData.value, 'updateToSchemaVersion91');
} catch (error) {
db.pragma('user_version = 91');
@@ -97,7 +100,11 @@ export default function updateToSchemaVersion91(
const [beforeQuery, beforeParams] =
sql`SELECT count(*) from preKeys WHERE ourServiceId = ${pni}`;
const beforeKeys = db.prepare(beforeQuery).pluck(true).get(beforeParams);
const beforeKeys = db
.prepare(beforeQuery, {
pluck: true,
})
.get(beforeParams);
logger.info(`updateToSchemaVersion91: Found ${beforeKeys} preKeys for PNI`);
// Create index to help us with all these queries
@@ -124,7 +131,11 @@ export default function updateToSchemaVersion91(
LIMIT 1
OFFSET 499
`;
const oldBoundary = db.prepare(oldQuery).pluck(true).get(oldParams);
const oldBoundary = db
.prepare(oldQuery, {
pluck: true,
})
.get(oldParams);
logger.info(
`updateToSchemaVersion91: Found 500th-oldest timestamp: ${oldBoundary}`
);
@@ -141,7 +152,11 @@ export default function updateToSchemaVersion91(
LIMIT 1
OFFSET 499
`;
const newBoundary = db.prepare(newQuery).pluck(true).get(newParams);
const newBoundary = db
.prepare(newQuery, {
pluck: true,
})
.get(newParams);
logger.info(
`updateToSchemaVersion91: Found 500th-newest timestamp: ${newBoundary}`
);
@@ -155,8 +170,8 @@ export default function updateToSchemaVersion91(
SELECT rowid FROM preKeys
WHERE
createdAt IS NOT NULL AND
createdAt > ${oldBoundary} AND
createdAt < ${newBoundary} AND
createdAt > ${oldBoundary ?? null} AND
createdAt < ${newBoundary ?? null} AND
ourServiceId = ${pni}
LIMIT 10000
);
@@ -175,7 +190,11 @@ export default function updateToSchemaVersion91(
FROM preKeys
WHERE ourServiceId = ${pni};
`;
const afterCount = db.prepare(afterQuery).pluck(true).get(afterParams);
const afterCount = db
.prepare(afterQuery, {
pluck: true,
})
.get(afterParams);
logger.info(
`updateToSchemaVersion91: Found ${afterCount} preKeys for PNI after delete`
);

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database, RunResult } from '@signalapp/better-sqlite3';
import type { Database, RunResult } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import type { QueryFragment } from '../util';
@@ -65,11 +65,12 @@ export function cleanKeys(
// Grab our PNI
let pni: PniString;
const pniJson = db
.prepare("SELECT json FROM items WHERE id IS 'pni'")
.pluck()
.get();
.prepare("SELECT json FROM items WHERE id IS 'pni'", {
pluck: true,
})
.get<string>();
try {
const pniData = JSON.parse(pniJson);
const pniData = JSON.parse(pniJson ?? '');
pni = normalizePni(pniData.value, logId);
} catch (error) {
if (pniJson) {
@@ -84,10 +85,12 @@ export function cleanKeys(
}
// Do overall count - if it's less than 1000, move on
const totalKeys = db
.prepare(sql`SELECT count(*) FROM ${tableName};`[0])
.pluck(true)
.get();
const totalKeys =
db
.prepare(sql`SELECT count(*) FROM ${tableName};`[0], {
pluck: true,
})
.get<number>() ?? 0;
logger.info(`${logId}: Found ${totalKeys} total keys`);
if (totalKeys < 1000) {
return;
@@ -96,7 +99,11 @@ export function cleanKeys(
// Grab PNI-specific count
const [beforeQuery, beforeParams] =
sql`SELECT count(*) from ${tableName} WHERE ${idField} = ${pni}`;
const beforeKeys = db.prepare(beforeQuery).pluck(true).get(beforeParams);
const beforeKeys = db
.prepare(beforeQuery, {
pluck: true,
})
.get(beforeParams);
logger.info(`${logId}: Found ${beforeKeys} keys for PNI`);
// Create index to help us with all these queries
@@ -123,7 +130,11 @@ export function cleanKeys(
LIMIT 1
OFFSET 499
`;
const oldBoundary = db.prepare(oldQuery).pluck(true).get(oldParams);
const oldBoundary = db
.prepare(oldQuery, {
pluck: true,
})
.get(oldParams);
logger.info(`${logId}: Found 500th-oldest timestamp: ${oldBoundary}`);
// Fetch 500th-newest timestamp for PNI
@@ -137,7 +148,11 @@ export function cleanKeys(
LIMIT 1
OFFSET 499
`;
const newBoundary = db.prepare(newQuery).pluck(true).get(newParams);
const newBoundary = db
.prepare(newQuery, {
pluck: true,
})
.get(newParams);
logger.info(`${logId}: Found 500th-newest timestamp: ${newBoundary}`);
// Delete everything in between for PNI
@@ -146,8 +161,8 @@ export function cleanKeys(
DELETE FROM ${tableName}
WHERE
createdAt IS NOT NULL AND
createdAt > ${oldBoundary} AND
createdAt < ${newBoundary} AND
createdAt > ${oldBoundary ?? null} AND
createdAt < ${newBoundary ?? null} AND
${idField} = ${pni}
LIMIT 10000;
`;
@@ -164,7 +179,11 @@ export function cleanKeys(
FROM ${tableName}
WHERE ${idField} = ${pni};
`;
const afterCount = db.prepare(afterQuery).pluck(true).get(afterParams);
const afterCount = db
.prepare(afterQuery, {
pluck: true,
})
.get(afterParams);
logger.info(`${logId}: Found ${afterCount} keys for PNI after delete`);
db.exec(

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
import type {
@@ -74,27 +74,31 @@ function migratePni(
const uuidIdJson = db
.prepare(
`
SELECT json
FROM items
WHERE id IS 'uuid_id'
`
SELECT json
FROM items
WHERE id IS 'uuid_id'
`,
{
pluck: true,
}
)
.pluck()
.get();
.get<string>();
const pniJson = db
.prepare(
`
SELECT json
FROM items
WHERE id IS 'pni'
`
SELECT json
FROM items
WHERE id IS 'pni'
`,
{
pluck: true,
}
)
.pluck()
.get();
.get<string>();
let aci: string | undefined;
try {
[aci] = JSON.parse(uuidIdJson).value.split('.', 2);
[aci] = JSON.parse(uuidIdJson ?? '').value.split('.', 2);
} catch (error) {
if (uuidIdJson) {
logger.warn(
@@ -111,7 +115,7 @@ function migratePni(
let legacyPni: string | undefined;
try {
legacyPni = JSON.parse(pniJson).value;
legacyPni = JSON.parse(pniJson ?? '').value;
} catch (error) {
if (pniJson) {
logger.warn('updateToSchemaVersion960: failed to parse pni item', error);
@@ -188,7 +192,7 @@ function migratePreKeys(
): void {
const preKeys = db
.prepare(`SELECT id, json FROM ${table} WHERE ourServiceId IS $legacyPni`)
.all({ legacyPni });
.all<{ id: string; json: string }>({ legacyPni });
const updateStmt = db.prepare(`
UPDATE ${table}

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';
export const version = 970;

View File

@@ -1,7 +1,7 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import type { LoggerType } from '../../types/Logging';

View File

@@ -1,7 +1,7 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { Database } from '@signalapp/sqlcipher';
import { keyBy } from 'lodash';
import { v4 as generateUuid } from 'uuid';
@@ -14,7 +14,6 @@ import {
objectToJSON,
jsonToObject,
} from '../util';
import type { Query, EmptyQuery } from '../util';
import type { WritableDB } from '../Interface';
import updateToSchemaVersion41 from './41-uuid-keys';
@@ -957,10 +956,10 @@ function updateToSchemaVersion20(
// significantly, so we drop them and recreate them later.
// Drop triggers
const triggers = db
.prepare<EmptyQuery>(
.prepare(
"SELECT * FROM sqlite_master WHERE type = 'trigger' AND tbl_name = 'messages'"
)
.all();
.all<{ name: string; sql: string }>();
for (const trigger of triggers) {
db.exec(`DROP TRIGGER ${trigger.name}`);
@@ -985,28 +984,29 @@ function updateToSchemaVersion20(
// Drop invalid groups and any associated messages
const maybeInvalidGroups = db
.prepare<EmptyQuery>(
.prepare(
"SELECT * FROM conversations WHERE type = 'group' AND members IS NULL;"
)
.all();
.all<{ id: string; json: string }>();
for (const group of maybeInvalidGroups) {
const json: { id: string; members: Array<unknown> } = JSON.parse(
group.json
);
if (!json.members || !json.members.length) {
db.prepare<Query>('DELETE FROM conversations WHERE id = $id;').run({
db.prepare('DELETE FROM conversations WHERE id = $id;').run({
id: json.id,
});
db.prepare('DELETE FROM messages WHERE conversationId = $id;').run({
id: json.id,
});
db.prepare<Query>(
'DELETE FROM messages WHERE conversationId = $id;'
).run({ id: json.id });
}
}
// Generate new IDs and alter data
const allConversations = db
.prepare<EmptyQuery>('SELECT * FROM conversations;')
.all();
const allConversations = db.prepare('SELECT * FROM conversations;').all<{
id: string;
type: string;
}>();
const allConversationsByOldId = keyBy(allConversations, 'id');
for (const row of allConversations) {
@@ -1023,7 +1023,7 @@ function updateToSchemaVersion20(
}
const patch = JSON.stringify(patchObj);
db.prepare<Query>(
db.prepare(
`
UPDATE conversations
SET id = $newId, json = JSON_PATCH(json, $patch)
@@ -1035,7 +1035,7 @@ function updateToSchemaVersion20(
patch,
});
const messagePatch = JSON.stringify({ conversationId: newId });
db.prepare<Query>(
db.prepare(
`
UPDATE messages
SET conversationId = $newId, json = JSON_PATCH(json, $patch)
@@ -1049,7 +1049,7 @@ function updateToSchemaVersion20(
members: string;
json: string;
}> = db
.prepare<EmptyQuery>(
.prepare(
`
SELECT id, members, json FROM conversations WHERE type = 'group';
`
@@ -1088,7 +1088,7 @@ function updateToSchemaVersion20(
profileSharing: false,
};
db.prepare<Query>(
db.prepare(
`
UPDATE conversations
SET
@@ -1113,7 +1113,7 @@ function updateToSchemaVersion20(
members: newMembers,
};
const newMembersValue = newMembers.join(' ');
db.prepare<Query>(
db.prepare(
`
UPDATE conversations
SET members = $newMembersValue, json = $newJsonValue
@@ -1127,7 +1127,10 @@ function updateToSchemaVersion20(
});
// Update sessions to stable IDs
const allSessions = db.prepare<EmptyQuery>('SELECT * FROM sessions;').all();
const allSessions = db.prepare('SELECT * FROM sessions;').all<{
id: string;
json: string;
}>();
for (const session of allSessions) {
// Not using patch here so we can explicitly delete a property rather than
// implicitly delete via null
@@ -1138,7 +1141,7 @@ function updateToSchemaVersion20(
newJson.id = `${newJson.conversationId}.${newJson.deviceId}`;
}
delete newJson.number;
db.prepare<Query>(
db.prepare(
`
UPDATE sessions
SET id = $newId, json = $newJson, conversationId = $newConversationId
@@ -1153,13 +1156,14 @@ function updateToSchemaVersion20(
}
// Update identity keys to stable IDs
const allIdentityKeys = db
.prepare<EmptyQuery>('SELECT * FROM identityKeys;')
.all();
const allIdentityKeys = db.prepare('SELECT * FROM identityKeys;').all<{
json: string;
id: number;
}>();
for (const identityKey of allIdentityKeys) {
const newJson = JSON.parse(identityKey.json);
newJson.id = allConversationsByOldId[newJson.id];
db.prepare<Query>(
db.prepare(
`
UPDATE identityKeys
SET id = $newId, json = $newJson
@@ -2105,10 +2109,12 @@ export function enableFTS5SecureDelete(db: Database, logger: LoggerType): void {
db
.prepare(
`
SELECT v FROM messages_fts_config WHERE k is 'secure-delete';
`
SELECT v FROM messages_fts_config WHERE k is 'secure-delete';
`,
{
pluck: true,
}
)
.pluck()
.get() === 1;
if (!isEnabled) {

View File

@@ -21,7 +21,6 @@ import {
defunctCallLinkFromRecord,
} from '../../util/callLinksRingrtc';
import type { ReadableDB, WritableDB } from '../Interface';
import { prepare } from '../Server';
import { sql } from '../util';
import { strictAssert } from '../../util/assert';
import { CallStatusValue, DirectCallStatus } from '../../types/CallDisposition';
@@ -33,7 +32,13 @@ export function callLinkExists(db: ReadableDB, roomId: string): boolean {
FROM callLinks
WHERE roomId = ${roomId};
`;
return db.prepare(query).pluck(true).get(params) === 1;
return (
db
.prepare(query, {
pluck: true,
})
.get(params) === 1
);
}
export function getCallLinkByRoomId(
@@ -53,11 +58,9 @@ export function getCallLinkRecordByRoomId(
db: ReadableDB,
roomId: string
): CallLinkRecord | undefined {
const row = prepare(db, 'SELECT * FROM callLinks WHERE roomId = $roomId').get(
{
roomId,
}
);
const row = db.prepare('SELECT * FROM callLinks WHERE roomId = $roomId').get({
roomId,
});
if (!row) {
return undefined;
@@ -83,8 +86,7 @@ function _insertCallLink(db: WritableDB, callLink: CallLinkType): void {
assertRoomIdMatchesRootKey(roomId, rootKey);
const data = callLinkToRecord(callLink);
prepare(
db,
db.prepare(
`
INSERT INTO callLinks (
roomId,
@@ -174,8 +176,7 @@ export function updateCallLinkAdminKeyByRoomId(
adminKey: string
): void {
const adminKeyBytes = toAdminKeyBytes(adminKey);
prepare(
db,
db.prepare(
`
UPDATE callLinks
SET adminKey = $adminKeyBytes
@@ -360,7 +361,11 @@ export function getAllMarkedDeletedCallLinkRoomIds(
const [query] = sql`
SELECT roomId FROM callLinks WHERE deleted = 1;
`;
return db.prepare(query).pluck().all();
return db
.prepare(query, {
pluck: true,
})
.all();
}
// TODO: Run this after uploading storage records, maybe periodically on startup
@@ -387,7 +392,13 @@ export function defunctCallLinkExists(db: ReadableDB, roomId: string): boolean {
FROM defunctCallLinks
WHERE roomId = ${roomId};
`;
return db.prepare(query).pluck(true).get(params) === 1;
return (
db
.prepare(query, {
pluck: true,
})
.get(params) === 1
);
}
export function getAllDefunctCallLinksWithAdminKey(
@@ -414,8 +425,7 @@ export function insertDefunctCallLink(
assertRoomIdMatchesRootKey(roomId, rootKey);
const data = defunctCallLinkToRecord(defunctCallLink);
prepare(
db,
db.prepare(
`
INSERT INTO defunctCallLinks (
roomId,

View File

@@ -11,7 +11,6 @@ import {
groupSendMemberEndorsementSchema,
groupSendEndorsementsDataSchema,
} from '../../types/GroupSendEndorsements';
import { prepare } from '../Server';
import type { ReadableDB, WritableDB } from '../Interface';
import { sql } from '../util';
import type { AciString } from '../../types/ServiceId';
@@ -43,8 +42,8 @@ function _deleteAllEndorsementsForGroup(db: WritableDB, groupId: string): void {
DELETE FROM groupSendMemberEndorsement
WHERE groupId IS ${groupId};
`;
prepare<Array<unknown>>(db, deleteCombined).run(deleteCombinedParams);
prepare<Array<unknown>>(db, deleteMembers).run(deleteMembersParams);
db.prepare(deleteCombined).run(deleteCombinedParams);
db.prepare(deleteMembers).run(deleteMembersParams);
}
function _replaceCombinedEndorsement(
@@ -57,9 +56,7 @@ function _replaceCombinedEndorsement(
(groupId, expiration, endorsement)
VALUES (${groupId}, ${expiration}, ${endorsement});
`;
const result = prepare<Array<unknown>>(db, insertCombined).run(
insertCombinedParams
);
const result = db.prepare(insertCombined).run(insertCombinedParams);
strictAssert(
result.changes === 1,
'Must update groupSendCombinedEndorsement'
@@ -77,9 +74,7 @@ function _replaceMemberEndorsements(
(groupId, memberAci, expiration, endorsement)
VALUES (${groupId}, ${memberAci}, ${expiration}, ${endorsement});
`;
const result = prepare<Array<unknown>>(db, replaceMember).run(
replaceMemberParams
);
const result = db.prepare(replaceMember).run(replaceMemberParams);
strictAssert(
result.changes === 1,
'Must update groupSendMemberEndorsement'
@@ -104,8 +99,10 @@ export function getGroupSendCombinedEndorsementExpiration(
SELECT expiration FROM groupSendCombinedEndorsement
WHERE groupId IS ${groupId};
`;
const value = prepare<Array<unknown>>(db, selectGroup)
.pluck()
const value = db
.prepare(selectGroup, {
pluck: true,
})
.get(selectGroupParams);
if (value == null) {
return null;
@@ -128,19 +125,17 @@ export function getGroupSendEndorsementsData(
WHERE groupId IS ${groupId}
`;
const combinedEndorsement: unknown = prepare<Array<unknown>>(
db,
selectCombinedEndorsement
).get(selectCombinedEndorsementParams);
const combinedEndorsement: unknown = db
.prepare(selectCombinedEndorsement)
.get(selectCombinedEndorsementParams);
if (combinedEndorsement == null) {
return null;
}
const memberEndorsements: Array<unknown> = prepare<Array<unknown>>(
db,
selectMemberEndorsements
).all(selectMemberEndorsementsParams);
const memberEndorsements: Array<unknown> = db
.prepare(selectMemberEndorsements)
.all(selectMemberEndorsementsParams);
return parseLoose(groupSendEndorsementsDataSchema, {
combinedEndorsement,
@@ -159,9 +154,9 @@ export function getGroupSendMemberEndorsement(
WHERE groupId IS ${groupId}
AND memberAci IS ${memberAci}
`;
const row = prepare<Array<unknown>>(db, selectMemberEndorsements).get(
selectMemberEndorsementsParams
);
const row = db
.prepare(selectMemberEndorsements)
.get(selectMemberEndorsementsParams);
if (row == null) {
return null;
}

View File

@@ -6,11 +6,6 @@ import { isNumber, last } from 'lodash';
import type { ReadableDB, WritableDB } from './Interface';
import type { LoggerType } from '../types/Logging';
export type EmptyQuery = [];
export type ArrayQuery = Array<ReadonlyArray<null | number | bigint | string>>;
export type Query = {
[key: string]: null | number | bigint | string | Uint8Array;
};
export type JSONRow = Readonly<{ json: string }>;
export type JSONRows = Array<JSONRow>;
@@ -39,12 +34,7 @@ export function jsonToObject<T>(json: string): T {
return JSON.parse(json);
}
export type QueryTemplateParam =
| Uint8Array
| string
| number
| null
| undefined;
export type QueryTemplateParam = Uint8Array | string | number | null;
export type QueryFragmentValue = QueryFragment | QueryTemplateParam;
export class QueryFragment {
@@ -184,7 +174,11 @@ export function explainQueryPlan(
template: QueryTemplate
): QueryTemplate {
const [query, params] = template;
const plan = db.prepare(`EXPLAIN QUERY PLAN ${query}`).all(params);
const plan = db.prepare(`EXPLAIN QUERY PLAN ${query}`).all<{
id: string | number;
parent: string | number;
detail: string;
}>(params);
logger.info('EXPLAIN QUERY PLAN');
for (const line of query.split('\n')) {
logger.info(line);
@@ -200,15 +194,15 @@ export function explainQueryPlan(
//
export function getSQLiteVersion(db: ReadableDB): string {
const { sqlite_version: version } = db
.prepare<EmptyQuery>('select sqlite_version() AS sqlite_version')
.get();
return version;
return (
db
.prepare('select sqlite_version() AS sqlite_version', { pluck: true })
.get<string>() ?? ''
);
}
export function getSchemaVersion(db: ReadableDB): number {
return db.pragma('schema_version', { simple: true });
return db.pragma('schema_version', { simple: true }) as number;
}
export function setUserVersion(db: WritableDB, version: number): void {
@@ -219,11 +213,11 @@ export function setUserVersion(db: WritableDB, version: number): void {
}
export function getUserVersion(db: ReadableDB): number {
return db.pragma('user_version', { simple: true });
return db.pragma('user_version', { simple: true }) as number;
}
export function getSQLCipherVersion(db: ReadableDB): string | undefined {
return db.pragma('cipher_version', { simple: true });
return db.pragma('cipher_version', { simple: true }) as string | undefined;
}
//
@@ -276,7 +270,7 @@ export function createOrUpdate<Key extends string | number>(
throw new Error('createOrUpdate: Provided data did not have a truthy id');
}
db.prepare<Query>(
db.prepare(
`
INSERT OR REPLACE INTO ${table} (
id,
@@ -310,14 +304,14 @@ export function getById<Key extends string | number, Result = unknown>(
id: Key
): Result | undefined {
const row = db
.prepare<Query>(
.prepare(
`
SELECT *
SELECT json
FROM ${table}
WHERE id = $id;
`
)
.get({
.get<{ json: string }>({
id,
});
@@ -362,22 +356,21 @@ export function removeById<Key extends string | number>(
}
export function removeAllFromTable(db: WritableDB, table: TableType): number {
return db.prepare<EmptyQuery>(`DELETE FROM ${table};`).run().changes;
return db.prepare(`DELETE FROM ${table};`).run().changes;
}
export function getAllFromTable<T>(db: ReadableDB, table: TableType): Array<T> {
const rows: JSONRows = db
.prepare<EmptyQuery>(`SELECT json FROM ${table};`)
.all();
const rows: JSONRows = db.prepare(`SELECT json FROM ${table};`).all();
return rows.map(row => jsonToObject(row.json));
}
export function getCountFromTable(db: ReadableDB, table: TableType): number {
const result: null | number = db
.prepare<EmptyQuery>(`SELECT count(*) from ${table};`)
.pluck(true)
.get();
const result = db
.prepare(`SELECT count(*) from ${table};`, {
pluck: true,
})
.get<number>();
if (isNumber(result)) {
return result;
}
@@ -392,7 +385,7 @@ export class TableIterator<ObjectType extends { id: string }> {
) {}
*[Symbol.iterator](): Iterator<ObjectType> {
const fetchObject = this.db.prepare<Query>(
const fetchObject = this.db.prepare(
`
SELECT json FROM ${this.table}
WHERE id > $id