perf(onebot): reduce message history migration scans

Speed up unfinished history migration while preserving all messages and
existing processing limits.

Keep migration diagnostics under the configured logging policy and surface
preparation failures.

Fixes #462
This commit is contained in:
DrSmoothl committed 2026-10-04 14:56:02 +08:00
1 parent 99bcfca15c
commit a6dfd04924
6 files changed
+286 -39

No files matched your search

@@ -9,6 +9,7 @@ import {
inspectMessageStoreMigration,
MessageStoreMigrator,
prepareMessageStoreDatabase,
type MessageStoreMigrationPreparation,
} from '../src/message-store-migration';
import {
isMessageStoreMigrationWorkerData,
@@ -25,20 +26,24 @@ function workerPayload(dbPath: string): MessageStoreMigrationWorkerData {
function createControlPort(): {
port: MessagePort;
messages: MessageStoreMigrationWorkerMessage[];
preparations: MessageStoreMigrationPreparation[];
send(message: unknown): void;
listenerCount(): number;
} {
const messages: MessageStoreMigrationWorkerMessage[] = [];
const preparations: MessageStoreMigrationPreparation[] = [];
const events = new EventEmitter();
return {
port: {
on: events.on.bind(events),
off: events.off.bind(events),
postMessage(message: MessageStoreMigrationWorkerMessage) {
messages.push(message);
if (message.kind === 'preparation') preparations.push(message.progress);
else messages.push(message);
},
} as unknown as MessagePort,
messages,
preparations,
send(message: unknown) {
events.emit('message', message);
},
@@ -174,6 +179,37 @@ describe('runMessageStoreMigrationWorker', () => {
]);
});
it('forwards preparation timings only after start without opening a worker log transport', async () => {
const { subscribeLogs } = await import('@snowluma/common/logger');
const { getFileTransport } = await import('@snowluma/common/log-file-transport');
const fileTransport = getFileTransport();
const write = vi.spyOn(fileTransport, 'write').mockImplementation(() => {});
const logs: string[] = [];
const unsubscribe = subscribeLogs(entry => logs.push(entry.scope));
vi.stubEnv('SNOWLUMA_LOG_FILE', '1');
const dbPath = path.join(tmpDir, 'messages.db');
seedUnclassified(dbPath, 1);
const control = createControlPort();
try {
const running = runMessageStoreMigrationWorker(workerPayload(dbPath), control.port);
await Promise.resolve();
expect(control.preparations).toEqual([]);
control.send('start');
await running;
expect(control.preparations).toEqual([
{ stage: 'started' },
{ stage: 'indexed', elapsedMs: expect.any(Number) },
{ stage: 'counted', total: 1, elapsedMs: expect.any(Number) },
]);
expect(control.preparations.filter(progress => progress.stage !== 'started').every(progress => progress.elapsedMs >= 0)).toBe(true);
expect(logs).toEqual([]);
expect(write).not.toHaveBeenCalled();
} finally {
unsubscribe();
vi.unstubAllEnvs();
}
});
it('returns after cancel without posting progress', async () => {
const dbPath = path.join(tmpDir, 'messages.db');
seedUnclassified(dbPath, 1);
@@ -342,7 +378,7 @@ describe('runMessageStoreMigrationWorker', () => {
expect(control.messages).toEqual([
{ kind: 'ready' },
{ kind: 'failed', message: 'no such table: messages' },
{ kind: 'failed', message: 'no such table: main.messages' },
]);
});