mirror of
https://github.com/moeru-ai/airi.git
synced 2026-12-03 03:31:01 +00:00
## 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
|
||
|---|---|---|
| .. | ||
| api | ||
| auth | ||