fix: add session_id index in tool_results

This commit is contained in:
tommy0103
2026-07-16 02:54:15 +08:00
parent 1e4e5ec2d8
commit cec66d0656
2 changed files with 19 additions and 0 deletions
+1
View File
@@ -57,6 +57,7 @@ CREATE INDEX IF NOT EXISTS idx_sessions_source ON sessions(source);
CREATE INDEX IF NOT EXISTS idx_messages_source ON messages(source); CREATE INDEX IF NOT EXISTS idx_messages_source ON messages(source);
CREATE INDEX IF NOT EXISTS idx_tc_session_name ON tool_calls(session_id, name); CREATE INDEX IF NOT EXISTS idx_tc_session_name ON tool_calls(session_id, name);
CREATE INDEX IF NOT EXISTS idx_tc_file ON tool_calls(file_path); CREATE INDEX IF NOT EXISTS idx_tc_file ON tool_calls(file_path);
CREATE INDEX IF NOT EXISTS idx_tr_session ON tool_results(session_id);
CREATE INDEX IF NOT EXISTS idx_sa_session ON subagents(session_id); CREATE INDEX IF NOT EXISTS idx_sa_session ON subagents(session_id);
CREATE INDEX IF NOT EXISTS idx_wf_session ON workflows(session_id); CREATE INDEX IF NOT EXISTS idx_wf_session ON workflows(session_id);
CREATE INDEX IF NOT EXISTS idx_wa_run ON workflow_agents(run_id); CREATE INDEX IF NOT EXISTS idx_wa_run ON workflow_agents(run_id);
+18
View File
@@ -1,6 +1,7 @@
import { test } from 'node:test'; import { test } from 'node:test';
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises'; import { readFile } from 'node:fs/promises';
import { DatabaseSync } from 'node:sqlite';
import { extractContentType, extractMessageIsMeta } from '../packages/core/src/db.ts'; import { extractContentType, extractMessageIsMeta } from '../packages/core/src/db.ts';
@@ -49,6 +50,23 @@ test('messages schema stores the raw content block type', async () => {
assert.match(source, /CREATE TRIGGER IF NOT EXISTS messages_fts_ad AFTER DELETE ON messages/); assert.match(source, /CREATE TRIGGER IF NOT EXISTS messages_fts_ad AFTER DELETE ON messages/);
}); });
test('tool results schema indexes live session patch lookups', async () => {
const db = new DatabaseSync(':memory:');
try {
db.exec(await readExecutableSchema());
const plan = db.prepare(
'EXPLAIN QUERY PLAN SELECT * FROM tool_results WHERE session_id = ?',
).all('session-1');
assert.ok(
plan.some(row => /USING INDEX idx_tr_session/.test(String(row.detail))),
`expected idx_tr_session lookup, got: ${plan.map(row => row.detail).join('; ')}`,
);
} finally {
db.close();
}
});
test('schema reference stays focused on raw SQL structure', async () => { test('schema reference stays focused on raw SQL structure', async () => {
const ref = await readSchemaReference(); const ref = await readSchemaReference();