fix(onebot): prepare message storage without blocking startup

Prevent existing message histories from being fully updated on the account startup path. Preserve current message behavior while allowing unfinished maintenance to continue safely after restarts.

Add coverage for older databases, interrupted upgrades, new messages written during maintenance, and newly created account storage.
This commit is contained in:
DrSmoothl
2026-07-30 09:57:00 +08:00
parent b1fd5dd0fe
commit caed31ca20
4 changed files with 570 additions and 172 deletions
@@ -0,0 +1,425 @@
import { DatabaseSync, type StatementSync } from 'node:sqlite';
import { openSqliteDb } from './sqlite-open';
const CLASSIFICATION_VERSION = 1;
const MIGRATION_NAME = 'message-classification-v1';
const LEGACY_PRIVATE_SEQUENCE_MIGRATION = 'private-nt-sequence-v1';
export type MessageStoreMigrationPhase = 'migrating' | 'complete';
export interface MessageStoreMigrationStatus {
phase: MessageStoreMigrationPhase;
processed: number;
total: number;
}
interface MigrationStateRow {
phase: MessageStoreMigrationPhase;
processed: number;
total: number;
cursor: number | null;
}
interface PendingMessageRow {
message_hash: number;
is_group: number;
sequence: number;
sequence_authoritative: number;
client_sequence: number;
private_direction: number;
random: number;
data: string | null;
}
export class MessageStoreMigrator {
private readonly db: DatabaseSync;
private readonly selectInitialBatch: StatementSync;
private readonly selectNextBatch: StatementSync;
private readonly updateRow: StatementSync;
private readonly updateState: StatementSync;
private state: MigrationStateRow;
constructor(dbPath: string) {
this.db = openSqliteDb(dbPath.replace(/\.json$/, '.db'));
createMigrationStateTable(this.db);
this.state = this.loadOrCreateState();
this.selectInitialBatch = this.db.prepare(`
SELECT message_hash, is_group, sequence, sequence_authoritative, client_sequence, private_direction, random, data
FROM messages
WHERE classification_version < ?
ORDER BY message_hash ASC
LIMIT ?
`);
this.selectNextBatch = this.db.prepare(`
SELECT message_hash, is_group, sequence, sequence_authoritative, client_sequence, private_direction, random, data
FROM messages
WHERE classification_version < ? AND message_hash > ?
ORDER BY message_hash ASC
LIMIT ?
`);
this.updateRow = this.db.prepare(`
UPDATE messages
SET sequence_authoritative = ?, private_direction = ?, classification_version = ?
WHERE message_hash = ?
AND classification_version < ?
AND is_group = ?
AND sequence = ?
AND sequence_authoritative = ?
AND client_sequence = ?
AND private_direction = ?
AND random = ?
AND data IS ?
`);
this.updateState = this.db.prepare(`
UPDATE message_store_migration_state
SET phase = ?, processed = ?, total = ?, cursor = ?, updated_at = ?
WHERE name = ?
`);
}
close(): void {
this.db.close();
}
getStatus(): MessageStoreMigrationStatus {
return publicStatus(this.state);
}
runBatch(batchSize: number): MessageStoreMigrationStatus {
const limit = normalizeBatchSize(batchSize);
if (this.state.phase === 'complete') return this.getStatus();
let rows = this.readBatch(limit, this.state.cursor);
if (rows.length === 0 && this.state.cursor !== null) {
rows = this.readBatch(limit, null);
}
if (rows.length === 0) {
const remaining = this.countPending();
this.persistState({
...this.state,
phase: remaining === 0 ? 'complete' : 'migrating',
processed: remaining === 0 ? this.state.total : this.state.total - remaining,
cursor: null,
});
return this.getStatus();
}
const lastMessageHash = rows[rows.length - 1].message_hash;
const classified = rows.map((row) => ({ row, classification: classifyLegacyMessage(row) }));
let updatedRows = 0;
this.db.exec('BEGIN IMMEDIATE');
try {
for (const { row, classification } of classified) {
const result = this.updateRow.run(
classification.sequenceAuthoritative ? 1 : 0,
classification.privateDirection,
CLASSIFICATION_VERSION,
row.message_hash,
CLASSIFICATION_VERSION,
row.is_group,
row.sequence,
row.sequence_authoritative,
row.client_sequence,
row.private_direction,
row.random,
row.data,
);
updatedRows += Number(result.changes);
}
const nextState: MigrationStateRow = {
...this.state,
processed: Math.min(this.state.total, this.state.processed + updatedRows),
cursor: lastMessageHash,
};
this.writeState(nextState);
this.db.exec('COMMIT');
this.state = nextState;
} catch (error) {
this.db.exec('ROLLBACK');
throw error;
}
if (rows.length < limit) {
const remaining = this.countPending();
if (remaining === 0) {
this.persistState({
...this.state,
phase: 'complete',
processed: this.state.total,
cursor: null,
});
}
}
return this.getStatus();
}
private loadOrCreateState(): MigrationStateRow {
const existing = this.db.prepare(`
SELECT phase, processed, total, cursor
FROM message_store_migration_state
WHERE name = ?
`).get(MIGRATION_NAME) as MigrationStateRow | undefined;
if (existing) return existing;
const row = this.db.prepare(`
SELECT COUNT(*) AS total
FROM messages
WHERE classification_version < ?
`).get(CLASSIFICATION_VERSION) as { total: number };
const total = Number(row.total);
const now = Math.floor(Date.now() / 1000);
const state: MigrationStateRow = {
phase: total === 0 ? 'complete' : 'migrating',
processed: 0,
total,
cursor: null,
};
this.db.prepare(`
INSERT INTO message_store_migration_state
(name, phase, processed, total, cursor, started_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
`).run(MIGRATION_NAME, state.phase, 0, total, null, now, now);
return state;
}
private countPending(): number {
const row = this.db.prepare(`
SELECT COUNT(*) AS total
FROM messages
WHERE classification_version < ?
`).get(CLASSIFICATION_VERSION) as { total: number };
return Number(row.total);
}
private readBatch(limit: number, cursor: number | null): PendingMessageRow[] {
return (cursor === null
? this.selectInitialBatch.all(CLASSIFICATION_VERSION, limit)
: this.selectNextBatch.all(CLASSIFICATION_VERSION, cursor, limit)) as unknown as PendingMessageRow[];
}
private persistState(state: MigrationStateRow): void {
this.db.exec('BEGIN IMMEDIATE');
try {
this.writeState(state);
this.db.exec('COMMIT');
this.state = state;
} catch (error) {
this.db.exec('ROLLBACK');
throw error;
}
}
private writeState(state: MigrationStateRow): void {
this.updateState.run(
state.phase,
state.processed,
state.total,
state.cursor,
Math.floor(Date.now() / 1000),
MIGRATION_NAME,
);
}
}
export function prepareMessageStoreDatabase(dbPath: string): void {
const db = openSqliteDb(dbPath.replace(/\.json$/, '.db'));
try {
const messagesTableExists = db.prepare(`
SELECT 1 AS present
FROM sqlite_schema
WHERE type = 'table' AND name = 'messages'
`).get() !== undefined;
db.exec(`
CREATE TABLE IF NOT EXISTS messages (
message_hash INTEGER PRIMARY KEY,
is_group INTEGER NOT NULL,
session_id INTEGER NOT NULL,
sequence INTEGER NOT NULL,
sequence_authoritative INTEGER NOT NULL DEFAULT 1,
event_name TEXT NOT NULL,
client_sequence INTEGER NOT NULL DEFAULT 0,
private_direction INTEGER NOT NULL DEFAULT -1,
random INTEGER NOT NULL DEFAULT 0,
timestamp INTEGER NOT NULL DEFAULT 0,
data TEXT,
classification_version INTEGER NOT NULL DEFAULT 1
)
`);
prepareMessageStoreSchema(db);
if (!messagesTableExists) {
createMessageStoreIndexes(db);
}
} finally {
db.close();
}
}
export function inspectMessageStoreMigration(dbPath: string): MessageStoreMigrationStatus {
const db = openSqliteDb(dbPath.replace(/\.json$/, '.db'));
try {
createMigrationStateTable(db);
const row = db.prepare(`
SELECT phase, processed, total
FROM message_store_migration_state
WHERE name = ?
`).get(MIGRATION_NAME) as MessageStoreMigrationStatus | undefined;
if (row) return row;
const pending = db.prepare(`
SELECT COUNT(*) AS total
FROM messages
WHERE classification_version < ?
`).get(CLASSIFICATION_VERSION) as { total: number };
const total = Number(pending.total);
return { phase: total === 0 ? 'complete' : 'migrating', processed: 0, total };
} finally {
db.close();
}
}
export function prepareMessageStoreSchema(db: DatabaseSync): void {
const before = db.prepare('PRAGMA table_info(messages)').all() as Array<{ name: string }>;
const existingColumns = new Set(before.map((column) => column.name));
const existingRowsAreCurrent = existingColumns.has('sequence_authoritative')
&& existingColumns.has('private_direction')
&& hasLegacyPrivateSequenceMigration(db);
if (!existingColumns.has('sequence_authoritative')) {
db.exec('ALTER TABLE messages ADD COLUMN sequence_authoritative INTEGER NOT NULL DEFAULT 1');
}
if (!existingColumns.has('private_direction')) {
db.exec('ALTER TABLE messages ADD COLUMN private_direction INTEGER NOT NULL DEFAULT -1');
}
if (!existingColumns.has('classification_version')) {
db.exec(
`ALTER TABLE messages ADD COLUMN classification_version INTEGER NOT NULL DEFAULT ${existingRowsAreCurrent ? CLASSIFICATION_VERSION : 0}`,
);
}
}
export function createMessageStoreIndexes(db: DatabaseSync): void {
db.exec('CREATE INDEX IF NOT EXISTS idx_messages_session_seq ON messages(is_group, session_id, sequence)');
db.exec('CREATE INDEX IF NOT EXISTS idx_messages_session_authoritative_seq ON messages(is_group, session_id, sequence_authoritative, sequence)');
db.exec('CREATE INDEX IF NOT EXISTS idx_messages_private_client_seq ON messages(is_group, session_id, client_sequence)');
db.exec('CREATE INDEX IF NOT EXISTS idx_messages_private_client_direction ON messages(is_group, session_id, client_sequence, private_direction)');
}
function hasLegacyPrivateSequenceMigration(db: DatabaseSync): boolean {
const tableExists = db.prepare(`
SELECT 1 AS present
FROM sqlite_schema
WHERE type = 'table' AND name = 'message_store_migrations'
`).get() !== undefined;
if (!tableExists) return false;
return db.prepare(`
SELECT 1 AS applied
FROM message_store_migrations
WHERE name = ?
`).get(LEGACY_PRIVATE_SEQUENCE_MIGRATION) !== undefined;
}
function createMigrationStateTable(db: DatabaseSync): void {
db.exec(`
CREATE TABLE IF NOT EXISTS message_store_migration_state (
name TEXT PRIMARY KEY,
phase TEXT NOT NULL,
processed INTEGER NOT NULL,
total INTEGER NOT NULL,
cursor INTEGER,
started_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
)
`);
}
function publicStatus(state: MigrationStateRow): MessageStoreMigrationStatus {
return {
phase: state.phase,
processed: state.processed,
total: state.total,
};
}
function normalizeBatchSize(value: number): number {
if (!Number.isSafeInteger(value) || value <= 0 || value > 1000) {
throw new RangeError('migration batch size must be an integer between 1 and 1000');
}
return value;
}
function classifyLegacyMessage(row: PendingMessageRow): {
sequenceAuthoritative: boolean;
privateDirection: number;
} {
const event = parseEvent(row.data);
if (row.is_group !== 1) {
return {
sequenceAuthoritative: row.sequence_authoritative === 1 && row.client_sequence > 0,
privateDirection: privateDirection(event),
};
}
return {
sequenceAuthoritative: row.sequence_authoritative === 1
&& (row.sequence <= 0 || !isSyntheticGroupMessage(row, event)),
privateDirection: -1,
};
}
function parseEvent(data: string | null): Record<string, unknown> | null {
if (data === null) return null;
try {
const parsed = JSON.parse(data) as unknown;
return parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)
? parsed as Record<string, unknown>
: null;
} catch {
return null;
}
}
function privateDirection(event: Record<string, unknown> | null): number {
if (event?.message_type !== 'private' || event.sub_type !== 'friend') return -1;
if (event.post_type === 'message') return 0;
if (event.post_type === 'message_sent') return 1;
return -1;
}
function isSyntheticGroupMessage(
row: PendingMessageRow,
event: Record<string, unknown> | null,
): boolean {
if (!event || event.message_type !== 'group') return false;
const message = Array.isArray(event.message) ? event.message : [];
const first = message[0];
const firstSegment = first !== null && typeof first === 'object'
? first as Record<string, unknown>
: null;
const firstData = firstSegment?.data !== null && typeof firstSegment?.data === 'object'
? firstSegment.data as Record<string, unknown>
: null;
if (event.post_type === 'message_sent'
&& row.random === row.sequence
&& (firstSegment?.type === 'file' || firstSegment?.type === 'video')) {
return true;
}
if (event.post_type !== 'message') return false;
if (event.user_id === 0 && event.time === 0) return true;
if (event.user_id === 0
&& message.length === 1
&& firstSegment?.type === 'text'
&& firstData?.text === '[引用消息]') {
return true;
}
const sender = event.sender !== null && typeof event.sender === 'object'
? event.sender as Record<string, unknown>
: null;
return !Object.prototype.hasOwnProperty.call(event, 'group_name')
&& sender !== null
&& sender.user_id === event.user_id
&& sender.nickname === ''
&& sender.card === ''
&& sender.role === 'member'
&& sender.sex === 'unknown'
&& sender.age === 0;
}
+25 -171
View File
@@ -2,6 +2,10 @@ import { DatabaseSync, type StatementSync } from 'node:sqlite';
import { createLogger } from '@snowluma/common/logger';
import type { JsonObject, MessageMeta } from './types';
import { openSqliteDb } from './sqlite-open';
import {
createMessageStoreIndexes,
prepareMessageStoreSchema,
} from './message-store-migration';
const log = createLogger('OneBot.MessageStore');
@@ -52,8 +56,8 @@ export class MessageStore {
// Database instance — `close()` finalizes them automatically.
this.stmtStoreEvent = this.db.prepare(
`INSERT INTO messages
(message_hash, is_group, session_id, sequence, sequence_authoritative, event_name, client_sequence, private_direction, random, timestamp, data)
VALUES (?, ?, ?, ?, ?, ?, 0, ?, 0, ?, ?)
(message_hash, is_group, session_id, sequence, sequence_authoritative, event_name, client_sequence, private_direction, random, timestamp, data, classification_version)
VALUES (?, ?, ?, ?, ?, ?, 0, ?, 0, ?, ?, 1)
ON CONFLICT(message_hash) DO UPDATE SET
is_group = excluded.is_group,
session_id = excluded.session_id,
@@ -68,13 +72,17 @@ export class MessageStore {
ELSE messages.private_direction
END,
timestamp = excluded.timestamp,
data = excluded.data`,
data = excluded.data,
classification_version = CASE
WHEN excluded.is_group = 1 THEN excluded.classification_version
ELSE messages.classification_version
END`,
);
this.stmtStoreMeta = this.db.prepare(
`INSERT INTO messages
(message_hash, is_group, session_id, sequence, sequence_authoritative, event_name, client_sequence, private_direction, random, timestamp)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
(message_hash, is_group, session_id, sequence, sequence_authoritative, event_name, client_sequence, private_direction, random, timestamp, classification_version)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)
ON CONFLICT(message_hash) DO UPDATE SET
is_group = excluded.is_group,
session_id = excluded.session_id,
@@ -84,7 +92,8 @@ export class MessageStore {
client_sequence = excluded.client_sequence,
private_direction = excluded.private_direction,
random = excluded.random,
timestamp = excluded.timestamp`,
timestamp = excluded.timestamp,
classification_version = excluded.classification_version`,
);
this.stmtFindEvent = this.db.prepare(
@@ -603,6 +612,11 @@ export class MessageStore {
}
private initSchema(): void {
const messagesTableExists = this.db.prepare(`
SELECT 1 AS present
FROM sqlite_schema
WHERE type = 'table' AND name = 'messages'
`).get() !== undefined;
this.db.exec(`
CREATE TABLE IF NOT EXISTS messages (
message_hash INTEGER PRIMARY KEY,
@@ -615,12 +629,11 @@ export class MessageStore {
private_direction INTEGER NOT NULL DEFAULT -1,
random INTEGER NOT NULL DEFAULT 0,
timestamp INTEGER NOT NULL DEFAULT 0,
data TEXT
data TEXT,
classification_version INTEGER NOT NULL DEFAULT 1
)
`);
this.migrateSequenceAuthority();
this.migratePrivateDirection();
this.migratePrivateNtSequences();
prepareMessageStoreSchema(this.db);
this.db.exec(`
CREATE TABLE IF NOT EXISTS private_recall_tombstones (
session_id INTEGER NOT NULL,
@@ -630,167 +643,8 @@ export class MessageStore {
PRIMARY KEY (session_id, client_sequence, private_direction)
)
`);
this.db.exec('CREATE INDEX IF NOT EXISTS idx_messages_session_seq ON messages(is_group, session_id, sequence)');
this.db.exec('CREATE INDEX IF NOT EXISTS idx_messages_session_authoritative_seq ON messages(is_group, session_id, sequence_authoritative, sequence)');
this.db.exec('CREATE INDEX IF NOT EXISTS idx_messages_private_client_seq ON messages(is_group, session_id, client_sequence)');
this.db.exec('CREATE INDEX IF NOT EXISTS idx_messages_private_client_direction ON messages(is_group, session_id, client_sequence, private_direction)');
}
private migratePrivateDirection(): void {
const columns = this.db.prepare('PRAGMA table_info(messages)').all() as Array<{ name: string }>;
if (columns.some((column) => column.name === 'private_direction')) return;
this.db.exec('BEGIN IMMEDIATE');
try {
this.db.exec(
'ALTER TABLE messages ADD COLUMN private_direction INTEGER NOT NULL DEFAULT -1',
);
this.db.exec(`
UPDATE messages
SET private_direction = CASE json_extract(data, '$.post_type')
WHEN 'message' THEN 0
WHEN 'message_sent' THEN 1
ELSE -1
END
WHERE is_group = 0
AND data IS NOT NULL
AND json_valid(data)
AND json_extract(data, '$.message_type') = 'private'
AND json_extract(data, '$.sub_type') = 'friend'
`);
this.db.exec('COMMIT');
} catch (error) {
this.db.exec('ROLLBACK');
throw error;
}
}
private migratePrivateNtSequences(): void {
this.db.exec(`
CREATE TABLE IF NOT EXISTS message_store_migrations (
name TEXT PRIMARY KEY,
applied_at INTEGER NOT NULL
)
`);
const migration = 'private-nt-sequence-v1';
this.db.exec('BEGIN IMMEDIATE');
try {
const applied = this.db.prepare(
'SELECT 1 AS applied FROM message_store_migrations WHERE name = ?',
).get(migration) as { applied: number } | undefined;
if (applied) {
this.db.exec('COMMIT');
return;
}
// Pre-fix observed/fetched private rows did not retain client_sequence;
// their `sequence` came from ContentHead field 5 and is sender-local.
// Action-sent rows do retain client_sequence and their send receipt
// carries QQ's private server sequence, so those remain usable.
const result = this.db.prepare(`
UPDATE messages
SET sequence_authoritative = 0
WHERE is_group = 0
AND sequence_authoritative = 1
AND client_sequence = 0
`).run();
this.db.prepare(
'INSERT INTO message_store_migrations (name, applied_at) VALUES (?, ?)',
).run(migration, Math.floor(Date.now() / 1000));
this.db.exec('COMMIT');
if (Number(result.changes) > 0) {
log.info(
'message-store schema migrated: legacy private sequences invalidated=%d',
Number(result.changes),
);
}
} catch (error) {
this.db.exec('ROLLBACK');
throw error;
}
}
private migrateSequenceAuthority(): void {
const columns = this.db.prepare('PRAGMA table_info(messages)').all() as Array<{ name: string }>;
if (columns.some((column) => column.name === 'sequence_authoritative')) return;
this.db.exec('BEGIN IMMEDIATE');
try {
this.db.exec(
'ALTER TABLE messages ADD COLUMN sequence_authoritative INTEGER NOT NULL DEFAULT 1',
);
const result = this.db.prepare(`
UPDATE messages
SET sequence_authoritative = 0
WHERE sequence > 0
AND data IS NOT NULL
AND CASE WHEN json_valid(data) THEN (
(
is_group = 1
AND (
(
json_extract(data, '$.post_type') = 'message_sent'
AND json_extract(data, '$.message_type') = 'group'
AND random = sequence
AND json_extract(data, '$.message[0].type') IN ('file', 'video')
)
OR
(
json_extract(data, '$.post_type') = 'message'
AND json_extract(data, '$.message_type') = 'group'
AND json_type(data, '$.group_name') IS NULL
AND json_type(data, '$.sender') = 'object'
AND json_extract(data, '$.sender.user_id') = json_extract(data, '$.user_id')
AND json_extract(data, '$.sender.nickname') = ''
AND json_extract(data, '$.sender.card') = ''
AND json_extract(data, '$.sender.role') = 'member'
AND json_extract(data, '$.sender.sex') = 'unknown'
AND json_extract(data, '$.sender.age') = 0
)
OR
(
json_extract(data, '$.post_type') = 'message'
AND json_extract(data, '$.message_type') = 'group'
AND json_extract(data, '$.user_id') = 0
AND json_extract(data, '$.time') = 0
)
OR
(
json_extract(data, '$.post_type') = 'message'
AND json_extract(data, '$.message_type') = 'group'
AND json_extract(data, '$.user_id') = 0
AND json_array_length(json_extract(data, '$.message')) = 1
AND json_extract(data, '$.message[0].type') = 'text'
AND json_extract(data, '$.message[0].data.text') = '[引用消息]'
)
)
)
OR
(
is_group = 0
AND random = 0
AND client_sequence = 0
AND json_extract(data, '$.post_type') = 'message'
AND json_extract(data, '$.message_type') = 'private'
AND json_extract(data, '$.sub_type') = 'friend'
AND json_type(data, '$.target_id') IS NULL
AND json_type(data, '$.sender') = 'object'
AND json_extract(data, '$.sender.user_id') = json_extract(data, '$.user_id')
AND json_extract(data, '$.sender.nickname') = ''
AND json_extract(data, '$.sender.sex') = 'unknown'
AND json_extract(data, '$.sender.age') = 0
)
) ELSE 0 END
`).run();
this.db.exec('COMMIT');
log.info(
'message-store schema migrated: sequence authority added, synthetic rows marked=%d',
Number(result.changes),
);
} catch (error) {
this.db.exec('ROLLBACK');
throw error;
if (!messagesTableExists) {
createMessageStoreIndexes(this.db);
}
}
}
+1
View File
@@ -17,5 +17,6 @@ export function openSqliteDb(dbPath: string): DatabaseSync {
const db = new DatabaseSync(dbPath);
db.exec('PRAGMA journal_mode = WAL');
db.exec('PRAGMA synchronous = NORMAL');
db.exec('PRAGMA busy_timeout = 1000');
return db;
}
+119 -1
View File
@@ -1,5 +1,9 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { MessageStore } from '../src/message-store';
import {
MessageStoreMigrator,
prepareMessageStoreDatabase,
} from '../src/message-store-migration';
import { hashMessageIdInt32, GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT } from '../src/message-id';
import fs from 'fs';
import path from 'path';
@@ -353,7 +357,6 @@ describe('MessageStore', () => {
const legacy = new DatabaseSync(testDbPath);
legacy.exec(`
DROP TABLE messages;
DROP TABLE message_store_migrations;
CREATE TABLE messages (
message_hash INTEGER PRIMARY KEY,
is_group INTEGER NOT NULL,
@@ -465,6 +468,49 @@ describe('MessageStore', () => {
legacy.close();
store = new MessageStore(testDbPath);
const migrator = new MessageStoreMigrator(testDbPath);
expect(migrator.getStatus()).toMatchObject({
phase: 'migrating',
processed: 0,
total: 8,
});
expect(store.findMeta(2)).toMatchObject({ sequenceAuthoritative: true });
expect(migrator.runBatch(3)).toMatchObject({
phase: 'migrating',
processed: 3,
total: 8,
});
store.storeMeta(-100, {
isGroup: true,
targetId: groupId,
sequence: 9700,
sequenceAuthoritative: true,
eventName: GROUP_MESSAGE_EVENT,
clientSequence: 0,
random: 0,
timestamp: 1700000600,
});
migrator.close();
const resumedMigrator = new MessageStoreMigrator(testDbPath);
expect(resumedMigrator.getStatus()).toMatchObject({
phase: 'migrating',
processed: 3,
total: 8,
});
expect(resumedMigrator.runBatch(3)).toMatchObject({
phase: 'migrating',
processed: 6,
total: 8,
});
expect(resumedMigrator.runBatch(3)).toMatchObject({
phase: 'complete',
processed: 8,
total: 8,
});
resumedMigrator.close();
expect(store.findLatestAuthoritativeSequence(true, groupId)).toBe(9748);
expect(store.listSessionEvents(true, groupId).map((message) => message.message_seq)).toEqual([9748]);
@@ -479,6 +525,10 @@ describe('MessageStore', () => {
expect(store.findMeta(6)).toMatchObject({ sequenceAuthoritative: false });
expect(store.findMeta(7)).toMatchObject({ sequenceAuthoritative: false });
expect(store.findMeta(9)).toMatchObject({ sequenceAuthoritative: true });
expect(store.findMeta(-100)).toMatchObject({
sequence: 9700,
sequenceAuthoritative: true,
});
store.storeMeta(8, {
isGroup: false,
@@ -492,6 +542,74 @@ describe('MessageStore', () => {
});
expect(store.findLatestAuthoritativeSequence(false, 555)).toBe(900);
});
it('resumes classification when an older schema upgrade stopped between steps', () => {
store.close();
const legacy = new DatabaseSync(testDbPath);
legacy.exec(`
DROP TABLE messages;
CREATE TABLE messages (
message_hash INTEGER PRIMARY KEY,
is_group INTEGER NOT NULL,
session_id INTEGER NOT NULL,
sequence INTEGER NOT NULL,
sequence_authoritative INTEGER NOT NULL DEFAULT 1,
event_name TEXT NOT NULL,
client_sequence INTEGER NOT NULL DEFAULT 0,
private_direction INTEGER NOT NULL DEFAULT -1,
random INTEGER NOT NULL DEFAULT 0,
timestamp INTEGER NOT NULL DEFAULT 0,
data TEXT
);
CREATE TABLE message_store_migrations (
name TEXT PRIMARY KEY,
applied_at INTEGER NOT NULL
);
INSERT INTO messages
(message_hash, is_group, session_id, sequence, sequence_authoritative,
event_name, client_sequence, private_direction, random, timestamp, data)
VALUES
(1, 0, 555, 800, 1, '${PRIVATE_MESSAGE_EVENT}', 0, 0, 0, 1700000000,
'{"post_type":"message","message_type":"private","sub_type":"friend"}')
`);
legacy.close();
store = new MessageStore(testDbPath);
const migrator = new MessageStoreMigrator(testDbPath);
expect(migrator.getStatus()).toEqual({
phase: 'migrating',
processed: 0,
total: 1,
});
expect(migrator.runBatch(10)).toEqual({
phase: 'complete',
processed: 1,
total: 1,
});
migrator.close();
expect(store.findMeta(1)).toMatchObject({ sequenceAuthoritative: false });
});
it('creates the normal indexes when background preparation creates the database', () => {
store.close();
fs.unlinkSync(testDbPath);
prepareMessageStoreDatabase(testDbPath);
const database = new DatabaseSync(testDbPath);
const indexes = database.prepare(`
SELECT name FROM sqlite_schema
WHERE type = 'index' AND tbl_name = 'messages'
`).all() as Array<{ name: string }>;
database.close();
expect(indexes.map(({ name }) => name).sort()).toEqual([
'idx_messages_private_client_direction',
'idx_messages_private_client_seq',
'idx_messages_session_authoritative_seq',
'idx_messages_session_seq',
]);
store = new MessageStore(testDbPath);
});
});
describe('private recall invalidation', () => {