mirror of
https://github.com/moeru-ai/airi.git
synced 2026-12-02 18:08:22 +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
|
||
|---|---|---|
| .. | ||
| meta | ||
| 0000_mean_slipstream.sql | ||
| 0001_magenta_skrulls.sql | ||
| 0002_mean_tigra.sql | ||
| 0003_old_titania.sql | ||
| 0004_bouncy_devos.sql | ||
| 0005_tough_living_tribunal.sql | ||
| 0006_overconfident_susan_delgado.sql | ||
| 0007_red_nicolaos.sql | ||
| 0008_gray_xavin.sql | ||
| 0009_perpetual_lilandra.sql | ||
| 0010_sudden_bastion.sql | ||
| 0011_common_doctor_octopus.sql | ||
| 0012_clumsy_the_stranger.sql | ||
| 0013_naive_groot.sql | ||
| 0014_vengeful_blonde_phantom.sql | ||
| 0015_concerned_piledriver.sql | ||
| 0016_tired_dagger.sql | ||
| 0017_nappy_dagger.sql | ||
| 0018_blushing_scarlet_witch.sql | ||
| 0019_low_namora.sql | ||
| 0020_smart_war_machine.sql | ||
| 0021_chilly_starjammers.sql | ||
| 0022_worthless_shriek.sql | ||
| 0023_payment_order.sql | ||
| 0024_drop_stripe_tables.sql | ||
| 0025_premium_frog_thor.sql | ||
| 0026_llm_request_tracking.sql | ||
| 0027_llm_cost_settlement.sql | ||
| 0028_flux_usage.sql | ||
| 0029_llm_request_content.sql | ||
| 0030_drop_flux_consumed_and_settlement.sql | ||
| 0031_character_cards.sql | ||
| 0032_capacitor.sql | ||
| 0033_display_models.sql | ||
| 0034_llm_request_log_created_index.sql | ||