feat: add async SQLite on Node 24 and improve messaging diagnostics (#520)
Move database work off the main thread on Node.js 24 and improve message handling and runtime diagnostics. Preserve migration, restart, and concurrency coverage with the async SQLite driver.
This commit is contained in:
1 parent
20f48d11ec
commit
c7428363cb
170 files changed
+7748
-3913
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { SqliteDatabase } from '@snowluma/sqlite';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { MessageStoreMigrator, prepareMessageStoreDatabase } from '../src/message-store-migration';
|
||||
|
||||
@@ -10,14 +10,14 @@ const pendingIndex = 'idx_messages_pending_classification_v1';
|
||||
describe('message history migration scans', () => {
|
||||
let directory: string;
|
||||
let dbPath: string;
|
||||
let db: DatabaseSync;
|
||||
let db: SqliteDatabase;
|
||||
let migrator: MessageStoreMigrator | undefined;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
directory = fs.mkdtempSync(path.join(os.tmpdir(), 'snowluma-migration-scans-'));
|
||||
dbPath = path.join(directory, 'messages.db');
|
||||
prepareMessageStoreDatabase(dbPath);
|
||||
db = new DatabaseSync(dbPath);
|
||||
await prepareMessageStoreDatabase(dbPath);
|
||||
db = new SqliteDatabase(dbPath);
|
||||
const insert = db.prepare(`INSERT INTO messages
|
||||
(message_hash, is_group, session_id, sequence, client_sequence, event_name, data, media_data, classification_version)
|
||||
VALUES (?, 0, 42, 1, 1, 'message.private', ?, ?, ?)`);
|
||||
@@ -27,10 +27,10 @@ describe('message history migration scans', () => {
|
||||
sub_type: 'friend',
|
||||
message: [{ type: 'text', data: { text: 'x'.repeat(3000) } }],
|
||||
});
|
||||
db.exec('BEGIN');
|
||||
await db.exec('BEGIN');
|
||||
|
||||
for (let hash = -500; hash <= 500; hash++) {
|
||||
insert.run(
|
||||
await insert.run(
|
||||
hash,
|
||||
data,
|
||||
'{"retained":"snapshot"}',
|
||||
@@ -38,14 +38,14 @@ describe('message history migration scans', () => {
|
||||
);
|
||||
}
|
||||
|
||||
db.exec('COMMIT');
|
||||
await db.exec('COMMIT');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(async () => {
|
||||
vi.restoreAllMocks();
|
||||
migrator?.close();
|
||||
await migrator?.close();
|
||||
migrator = undefined;
|
||||
db.close();
|
||||
await db.close();
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
@@ -59,9 +59,9 @@ describe('message history migration scans', () => {
|
||||
];
|
||||
}
|
||||
|
||||
function expectPendingQueryPlans(queries: string[]): void {
|
||||
async function expectPendingQueryPlans(queries: string[]): Promise<void> {
|
||||
expect(queries).toHaveLength(3);
|
||||
const planDb = new DatabaseSync(dbPath);
|
||||
const planDb = new SqliteDatabase(dbPath, { readOnly: true });
|
||||
|
||||
try {
|
||||
for (const sql of queries) {
|
||||
@@ -70,9 +70,7 @@ describe('message history migration scans', () => {
|
||||
? [1, -400, 1]
|
||||
: [1, 1]
|
||||
: [1];
|
||||
const details = planDb
|
||||
.prepare(`EXPLAIN QUERY PLAN ${sql}`)
|
||||
.all(...args)
|
||||
const details = (await planDb.prepare(`EXPLAIN QUERY PLAN ${sql}`).all(...args))
|
||||
.map((row) => String(row.detail))
|
||||
.join('\n');
|
||||
expect(details).toContain(pendingIndex);
|
||||
@@ -83,34 +81,37 @@ describe('message history migration scans', () => {
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
planDb.close();
|
||||
await planDb.close();
|
||||
}
|
||||
}
|
||||
|
||||
it('uses pending rows for the actual count and ordered batch queries without changing history', () => {
|
||||
const before = db
|
||||
it('uses pending rows for the actual count and ordered batch queries without changing history', async () => {
|
||||
const before = await db
|
||||
.prepare('SELECT message_hash, data, media_data FROM messages ORDER BY message_hash')
|
||||
.all();
|
||||
const prepare = vi.spyOn(DatabaseSync.prototype, 'prepare');
|
||||
const prepare = vi.spyOn(SqliteDatabase.prototype, 'prepare');
|
||||
migrator = new MessageStoreMigrator(dbPath);
|
||||
await migrator.initialization;
|
||||
const queries = capturedPendingQueries(prepare.mock.calls.map(([sql]) => sql));
|
||||
expectPendingQueryPlans(queries);
|
||||
expect(migrator.getStatus()).toEqual({ phase: 'migrating', processed: 0, total: 3 });
|
||||
expect(migrator.runBatch(2)).toEqual({ phase: 'migrating', processed: 2, total: 3 });
|
||||
expect(migrator.runBatch(2)).toEqual({ phase: 'complete', processed: 3, total: 3 });
|
||||
expectPendingQueryPlans(queries);
|
||||
await expectPendingQueryPlans(queries);
|
||||
expect(await migrator.getStatus()).toEqual({ phase: 'migrating', processed: 0, total: 3 });
|
||||
expect(await migrator.runBatch(2)).toEqual({ phase: 'migrating', processed: 2, total: 3 });
|
||||
expect(await migrator.runBatch(2)).toEqual({ phase: 'complete', processed: 3, total: 3 });
|
||||
await expectPendingQueryPlans(queries);
|
||||
expect(
|
||||
db.prepare('SELECT COUNT(*) AS n FROM messages WHERE classification_version < 1').get(),
|
||||
await db
|
||||
.prepare('SELECT COUNT(*) AS n FROM messages WHERE classification_version < 1')
|
||||
.get(),
|
||||
).toEqual({ n: 0 });
|
||||
expect(
|
||||
db
|
||||
await db
|
||||
.prepare(
|
||||
'SELECT message_hash, data, media_data FROM messages ORDER BY message_hash',
|
||||
)
|
||||
.all(),
|
||||
).toEqual(before);
|
||||
expect(
|
||||
db
|
||||
await db
|
||||
.prepare(
|
||||
'SELECT message_hash, private_direction FROM messages WHERE message_hash IN (-400, 0, 400) ORDER BY message_hash',
|
||||
)
|
||||
@@ -122,75 +123,84 @@ describe('message history migration scans', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('adds the index when resuming a migration started by an older version', () => {
|
||||
it('adds the index when resuming a migration started by an older version', async () => {
|
||||
migrator = new MessageStoreMigrator(dbPath);
|
||||
expect(migrator.runBatch(1)).toEqual({ phase: 'migrating', processed: 1, total: 3 });
|
||||
migrator.close();
|
||||
await migrator.initialization;
|
||||
expect(await migrator.runBatch(1)).toEqual({ phase: 'migrating', processed: 1, total: 3 });
|
||||
await migrator.close();
|
||||
migrator = undefined;
|
||||
db.exec(`DROP INDEX IF EXISTS ${pendingIndex}`);
|
||||
await db.exec(`DROP INDEX IF EXISTS ${pendingIndex}`);
|
||||
|
||||
migrator = new MessageStoreMigrator(dbPath);
|
||||
await migrator.initialization;
|
||||
expect(
|
||||
db
|
||||
await db
|
||||
.prepare('SELECT name FROM sqlite_schema WHERE type = ? AND name = ?')
|
||||
.get('index', pendingIndex),
|
||||
).toEqual({ name: pendingIndex });
|
||||
expect(migrator.getStatus()).toEqual({ phase: 'migrating', processed: 1, total: 3 });
|
||||
expect(migrator.runBatch(3)).toEqual({ phase: 'complete', processed: 3, total: 3 });
|
||||
expect(await migrator.getStatus()).toEqual({ phase: 'migrating', processed: 1, total: 3 });
|
||||
expect(await migrator.runBatch(3)).toEqual({ phase: 'complete', processed: 3, total: 3 });
|
||||
});
|
||||
|
||||
it('does not rebuild an existing pending index when an interrupted migration resumes', () => {
|
||||
it('does not rebuild an existing pending index when an interrupted migration resumes', async () => {
|
||||
migrator = new MessageStoreMigrator(dbPath);
|
||||
migrator.runBatch(1);
|
||||
migrator.close();
|
||||
await migrator.initialization;
|
||||
await migrator.runBatch(1);
|
||||
await migrator.close();
|
||||
migrator = undefined;
|
||||
const schemaVersion = db.prepare('PRAGMA schema_version').get();
|
||||
const schemaVersion = await db.prepare('PRAGMA schema_version').get();
|
||||
|
||||
migrator = new MessageStoreMigrator(dbPath);
|
||||
expect(db.prepare('PRAGMA schema_version').get()).toEqual(schemaVersion);
|
||||
expect(migrator.runBatch(3)).toEqual({ phase: 'complete', processed: 3, total: 3 });
|
||||
await migrator.initialization;
|
||||
expect(await db.prepare('PRAGMA schema_version').get()).toEqual(schemaVersion);
|
||||
expect(await migrator.runBatch(3)).toEqual({ phase: 'complete', processed: 3, total: 3 });
|
||||
});
|
||||
|
||||
it('revisits rows left behind the cursor after a concurrent update prevented classification', () => {
|
||||
db.exec(`CREATE TRIGGER defer_first BEFORE UPDATE ON messages
|
||||
it('revisits rows left behind the cursor after a concurrent update prevented classification', async () => {
|
||||
await db.exec(`CREATE TRIGGER defer_first BEFORE UPDATE ON messages
|
||||
WHEN OLD.message_hash = -400 BEGIN SELECT RAISE(IGNORE); END`);
|
||||
migrator = new MessageStoreMigrator(dbPath);
|
||||
expect(migrator.runBatch(1)).toEqual({ phase: 'migrating', processed: 0, total: 3 });
|
||||
db.exec('DROP TRIGGER defer_first');
|
||||
expect(migrator.runBatch(2)).toEqual({ phase: 'migrating', processed: 2, total: 3 });
|
||||
expect(migrator.runBatch(2)).toEqual({ phase: 'complete', processed: 3, total: 3 });
|
||||
await migrator.initialization;
|
||||
expect(await migrator.runBatch(1)).toEqual({ phase: 'migrating', processed: 0, total: 3 });
|
||||
await db.exec('DROP TRIGGER defer_first');
|
||||
expect(await migrator.runBatch(2)).toEqual({ phase: 'migrating', processed: 2, total: 3 });
|
||||
expect(await migrator.runBatch(2)).toEqual({ phase: 'complete', processed: 3, total: 3 });
|
||||
expect(
|
||||
db.prepare('SELECT COUNT(*) AS n FROM messages WHERE classification_version < 1').get(),
|
||||
await db
|
||||
.prepare('SELECT COUNT(*) AS n FROM messages WHERE classification_version < 1')
|
||||
.get(),
|
||||
).toEqual({ n: 0 });
|
||||
expect(db.prepare('SELECT COUNT(*) AS n FROM messages').get()).toEqual({ n: 1001 });
|
||||
expect(await db.prepare('SELECT COUNT(*) AS n FROM messages').get()).toEqual({ n: 1001 });
|
||||
});
|
||||
|
||||
it('skips index creation and pending counts when an older version already completed migration', () => {
|
||||
it('skips index creation and pending counts when an older version already completed migration', async () => {
|
||||
migrator = new MessageStoreMigrator(dbPath);
|
||||
migrator.runBatch(10);
|
||||
migrator.close();
|
||||
await migrator.initialization;
|
||||
await migrator.runBatch(10);
|
||||
await migrator.close();
|
||||
migrator = undefined;
|
||||
db.exec(`DROP INDEX IF EXISTS ${pendingIndex}`);
|
||||
const exec = vi.spyOn(DatabaseSync.prototype, 'exec');
|
||||
const prepare = vi.spyOn(DatabaseSync.prototype, 'prepare');
|
||||
await db.exec(`DROP INDEX IF EXISTS ${pendingIndex}`);
|
||||
const exec = vi.spyOn(SqliteDatabase.prototype, 'exec');
|
||||
const prepare = vi.spyOn(SqliteDatabase.prototype, 'prepare');
|
||||
|
||||
migrator = new MessageStoreMigrator(dbPath);
|
||||
expect(migrator.runBatch(10)).toEqual({ phase: 'complete', processed: 3, total: 3 });
|
||||
await migrator.initialization;
|
||||
expect(await migrator.runBatch(10)).toEqual({ phase: 'complete', processed: 3, total: 3 });
|
||||
expect(exec.mock.calls.some(([sql]) => /CREATE\s+INDEX/i.test(sql))).toBe(false);
|
||||
expect(prepare.mock.calls.some(([sql]) => sql.includes('COUNT(*)'))).toBe(false);
|
||||
expect(
|
||||
db
|
||||
await db
|
||||
.prepare('SELECT name FROM sqlite_schema WHERE type = ? AND name = ?')
|
||||
.get('index', pendingIndex),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('propagates preparation failure and closes the failed migration connection', () => {
|
||||
const originalExec = DatabaseSync.prototype.exec;
|
||||
it('propagates preparation failure and closes the failed migration connection', async () => {
|
||||
const originalExec = SqliteDatabase.prototype.exec;
|
||||
const failure = new Error('pending index unavailable');
|
||||
let failedDb: DatabaseSync | undefined;
|
||||
vi.spyOn(DatabaseSync.prototype, 'exec').mockImplementation(function (
|
||||
this: DatabaseSync,
|
||||
let failedDb: SqliteDatabase | undefined;
|
||||
vi.spyOn(SqliteDatabase.prototype, 'exec').mockImplementation(async function (
|
||||
this: SqliteDatabase,
|
||||
sql,
|
||||
) {
|
||||
if (sql.includes(pendingIndex)) {
|
||||
@@ -201,13 +211,18 @@ describe('message history migration scans', () => {
|
||||
return originalExec.call(this, sql);
|
||||
});
|
||||
|
||||
expect(() => {
|
||||
await expect(async () => {
|
||||
migrator = new MessageStoreMigrator(dbPath);
|
||||
}).toThrow(failure);
|
||||
await migrator.initialization;
|
||||
}).rejects.toThrow(failure);
|
||||
expect(failedDb).toBeDefined();
|
||||
expect(() => failedDb!.prepare('SELECT 1')).toThrow(/not open/);
|
||||
await migrator!.close();
|
||||
migrator = undefined;
|
||||
await expect(failedDb!.exec('SELECT 1')).rejects.toThrow(/closing/);
|
||||
expect(
|
||||
db.prepare('SELECT COUNT(*) AS n FROM messages WHERE classification_version < 1').get(),
|
||||
await db
|
||||
.prepare('SELECT COUNT(*) AS n FROM messages WHERE classification_version < 1')
|
||||
.get(),
|
||||
).toEqual({ n: 3 });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user