airi/server/apps
RainbowBird fe0b8fa73f
feat(api-server): index llm_request_log by created_at (#3457)
## Summary

Add the index `llm_request_log_created_idx` on
`llm_request_log(created_at)`.

The admin API reads the request log by time window across all users:
request statistics, and since
[proj-airi/backend#68](https://github.com/proj-airi/backend/pull/68) the
provider health query and minute/hour windows. The table has indexes
only for `(user_id, created_at)`, `(user_id, request_id)`, `(state,
started_at)` and `(gateway, generation_id)`. Without a user filter,
those queries scan the whole table. The Go Ent mirror in
proj-airi/backend already declares this index, but no migration created
it.

## Change map

- `server/apps/api/src/schemas/llm-request-log.ts`: the new index.
- `server/apps/api/drizzle/0034_llm_request_log_created_index.sql` and
`meta/`: generated by `drizzle-kit generate --name
llm_request_log_created_index`. The only statement is the new index. I
changed it to `CREATE INDEX IF NOT EXISTS`.

## Boundaries and risks

- The migrator runs pending migrations in one transaction, so the
migration cannot use `CONCURRENTLY`. A plain `CREATE INDEX` blocks
writes to `llm_request_log` while it builds. Request log rows are
written after the response is sent, so a short block delays log writes,
not user requests.
- To avoid the block, run this on production before the deploy. The
migration then skips the existing index:

  ```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS "llm_request_log_created_idx" ON
"llm_request_log" USING btree ("created_at");
  ```

- Rollback: `DROP INDEX IF EXISTS "llm_request_log_created_idx";`. No
data changes.

## Verification

In rc, on a clone at a0d1b2ce0:

- `drizzle-kit generate`: no change other than the new index.
- `pnpm -F @proj-airi/api-server typecheck`: pass.
- `server/apps/api` Vitest: 88 files passed, 2 skipped; 975 tests
passed, 4 skipped.
- `eslint` on the changed schema file: no errors.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 16:28:46 +08:00
..
api feat(api-server): index llm_request_log by created_at (#3457) 2026-11-09 16:28:46 +08:00
auth chore: upgrade injeca to 0.5.1 (#3436) 2026-11-08 06:41:31 +08:00