airi/server/apps/api/drizzle
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
..
meta feat(api-server): index llm_request_log by created_at (#3457) 2026-11-09 16:28:46 +08:00
0000_mean_slipstream.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0001_magenta_skrulls.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0002_mean_tigra.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0003_old_titania.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0004_bouncy_devos.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0005_tough_living_tribunal.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0006_overconfident_susan_delgado.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0007_red_nicolaos.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0008_gray_xavin.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0009_perpetual_lilandra.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0010_sudden_bastion.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0011_common_doctor_octopus.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0012_clumsy_the_stranger.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0013_naive_groot.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0014_vengeful_blonde_phantom.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0015_concerned_piledriver.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0016_tired_dagger.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0017_nappy_dagger.sql chore: move the server to an independent folder 2026-08-02 18:43:57 +08:00
0018_blushing_scarlet_witch.sql perf(server): add query indexes and cache admin metrics (#2213) 2026-08-04 17:31:16 +08:00
0019_low_namora.sql fix(server): index hot Postgres query paths (#2218) 2026-08-04 21:24:26 +08:00
0020_smart_war_machine.sql feat(api): hot-reload ConfigKV from Postgres (#2289) 2026-08-15 22:24:32 +08:00
0021_chilly_starjammers.sql refactor(auth): replace admin plugin with ban guard (#2303) 2026-08-16 16:16:36 +08:00
0022_worthless_shriek.sql fix(analytics): remove TS product events table (#2306) 2026-08-17 10:16:44 +00:00
0023_payment_order.sql refactor(api): extract payment CORE to support other payment providers [1/2] (#2335) 2026-09-12 17:43:10 +08:00
0024_drop_stripe_tables.sql refactor(api): extract payment CORE to support other payment providers [2/2] (#2368) 2026-09-20 14:52:48 +08:00
0025_premium_frog_thor.sql feat(stage-ui): sync user providers to a cloud replica (#2471) 2026-09-27 00:37:51 +08:00
0026_llm_request_tracking.sql feat(api): add extensible LLM request tracking (#2673) 2026-09-27 19:40:36 +08:00
0027_llm_cost_settlement.sql feat(api): add provider-cost Flux settlement (#2644) 2026-10-03 21:13:04 +08:00
0028_flux_usage.sql refactor(api): store micro-Flux fees in flux_usage (#2783) 2026-10-03 21:03:55 +00:00
0029_llm_request_content.sql feat(api): store LLM request and error content (#2675) 2026-10-04 05:23:38 +08:00
0030_drop_flux_consumed_and_settlement.sql refactor(api): drop the request-log Flux column and the settlement archive (#2785) 2026-10-03 21:33:39 +00:00
0031_character_cards.sql feat(api,stage-ui): synchronize character cards by field (#2817) 2026-10-06 18:49:25 +00:00
0032_capacitor.sql feat(api,stage-ui): add the Capacitor, a subscription store of Flux (#2813) 2026-10-11 01:18:20 +08:00
0033_display_models.sql feat(api,stage-ui): sync Live2D and VRM models to private storage (#2869) 2026-10-14 16:06:15 +08:00
0034_llm_request_log_created_index.sql feat(api-server): index llm_request_log by created_at (#3457) 2026-11-09 16:28:46 +08:00