fix(db): create the call_logs provider-stats index after legacy healing

#12832 declared `idx_cl_request_provider ON call_logs(request_type, provider)`
inside SCHEMA_SQL. That block runs before ensureCallLogsColumns() heals a legacy
call_logs table, so on any lineage predating the request_type column the CREATE
INDEX aborted the whole schema exec with "no such column: request_type" and the
server never finished opening the database.

Move the index next to the other request_type/combo indexes in
ensureCallLogsColumns(), which runs after the ALTER TABLE healing (and is also
called on the in-memory path), so both fresh and upgraded databases get it.

Proven by tests/unit/db-core-init.test.ts, "legacy call_logs schemas are upgraded
before combo target indexes are created" — failing on the release tip, green now.
This commit is contained in:
diegosouzapw
2026-09-10 19:11:08 -03:00
parent fd27ff08c7
commit ed44f4ae12
2 changed files with 12 additions and 1 deletions

View File

@@ -408,7 +408,12 @@ const SCHEMA_SQL = `
CREATE INDEX IF NOT EXISTS idx_cl_timestamp ON call_logs(timestamp);
CREATE INDEX IF NOT EXISTS idx_cl_status ON call_logs(status);
CREATE INDEX IF NOT EXISTS idx_cl_provider_timestamp ON call_logs(provider, timestamp);
CREATE INDEX IF NOT EXISTS idx_cl_request_provider ON call_logs(request_type, provider);
-- idx_cl_request_provider is NOT declared here: SCHEMA_SQL runs before
-- ensureCallLogsColumns() heals a legacy call_logs table, and a lineage that
-- predates the request_type column has none yet — the CREATE INDEX would abort
-- the whole schema exec with "no such column: request_type" and the server would
-- never boot. It is created next to the other request_type/combo indexes in
-- ensureCallLogsColumns() (db/schemaColumns.ts), after the columns exist.
CREATE TABLE IF NOT EXISTS proxy_logs (
id TEXT PRIMARY KEY,

View File

@@ -265,6 +265,12 @@ export function ensureCallLogsColumns(db: SqliteDatabase) {
"CREATE INDEX IF NOT EXISTS idx_call_logs_requested_model ON call_logs(requested_model)"
);
db.exec("CREATE INDEX IF NOT EXISTS idx_call_logs_request_type ON call_logs(request_type)");
// #12832's provider-stats index. It lives here rather than in SCHEMA_SQL because
// SCHEMA_SQL runs before this healing pass: on a legacy call_logs table that
// predates `request_type` the CREATE INDEX aborts the whole schema exec.
db.exec(
"CREATE INDEX IF NOT EXISTS idx_cl_request_provider ON call_logs(request_type, provider)"
);
db.exec(
"CREATE INDEX IF NOT EXISTS idx_cl_combo_target ON call_logs(combo_name, combo_execution_key, timestamp)"
);