From ed44f4ae129cb14fcde22636fca1d5c34f151138 Mon Sep 17 00:00:00 2001 From: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> Date: Thu, 10 Sep 2026 19:11:08 -0300 Subject: [PATCH] fix(db): create the call_logs provider-stats index after legacy healing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #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. --- src/lib/db/core.ts | 7 ++++++- src/lib/db/schemaColumns.ts | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/lib/db/core.ts b/src/lib/db/core.ts index a69db56e64..9d8e928864 100644 --- a/src/lib/db/core.ts +++ b/src/lib/db/core.ts @@ -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, diff --git a/src/lib/db/schemaColumns.ts b/src/lib/db/schemaColumns.ts index b288072dac..d0d7b42493 100644 --- a/src/lib/db/schemaColumns.ts +++ b/src/lib/db/schemaColumns.ts @@ -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)" );