Skip to content

fix(proxy-extras): keep the post-migration sanity check from building the hand-built SpendLogs indexes - #44397

Merged
yuneng-berri merged 1 commit into
rc/1.104.0from
litellm_fix_d03a47
Oct 3, 2026
Merged

yuneng-berri merged 1 commit into
rc/1.104.0from
litellm_fix_d03a47

Conversation

@yuneng-berri

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • On rc/1.104.0 both SpendLogs index migrations are SELECT 1
  • schema.prisma still declares both indexes on LiteLLM_SpendLogs
  • The legacy resolver's post-migration sanity check builds them anyway, with a table-locking CREATE INDEX

How it solves it:

User Flow

Before: an operator on the legacy migration resolver upgrades and the boot locks LiteLLM_SpendLogs while it builds two indexes

  1. The operator has a database migrated by an older release, with rows in LiteLLM_SpendLogs
  2. They boot 1.104.0 with USE_V2_MIGRATION_RESOLVER=false (or --use_legacy_migration_resolver)
  3. The boot logs "Running post-migration sanity check..." then "Migration diff applied successfully"
  4. Postgres ran CREATE INDEX "LiteLLM_SpendLogs_litellm_call_id_idx" and CREATE INDEX "LiteLLM_SpendLogs_api_key_startTime_idx", blocking writes to the table until both finish

After: the same boot leaves the table alone

  1. The operator has a database migrated by an older release, with rows in LiteLLM_SpendLogs
  2. They boot 1.104.0 with USE_V2_MIGRATION_RESOLVER=false
  3. The boot logs "Drift script is empty after filtering; nothing to apply"
  4. Neither index exists and Postgres ran no CREATE INDEX on LiteLLM_SpendLogs

Relevant issues

Ports #44286 from stable/1.103.x to rc/1.104.0 ahead of the next 1.104.0 release candidate

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review

Screenshots / Proof of Fix

Setup for every run: a fresh postgres:16 -c log_statement=all, seeded with prisma migrate deploy using only the 159 migrations older than 20260823000000, then 5 rows inserted into LiteLLM_SpendLogs. The proxy's DB setup then boots against it:

DATABASE_URL=postgresql://postgres:pw@127.0.0.1:55432/litellm USE_V2_MIGRATION_RESOLVER=false \
  python -c "import sys; from litellm.proxy.proxy_cli import run_server; sys.argv=['litellm','--skip_server_startup']; run_server()"
psql -c "SELECT indexname FROM pg_indexes WHERE tablename='LiteLLM_SpendLogs' AND indexname IN ('LiteLLM_SpendLogs_api_key_startTime_idx','LiteLLM_SpendLogs_litellm_call_id_idx')"
docker logs <pg> | grep -iE 'CREATE (UNIQUE )?INDEX[^;]*LiteLLM_SpendLogs'

The default case drops USE_V2_MIGRATION_RESOLVER=false

Before (5aa0cd0)

Legacy resolver

  1. Boot log:
INFO - Running post-migration sanity check...
INFO - ✅ Migration diff applied successfully
INFO - ✅ Post-migration sanity check completed
  1. pg_indexes:
 LiteLLM_SpendLogs_api_key_startTime_idx
 LiteLLM_SpendLogs_litellm_call_id_idx
(2 rows)
  1. Postgres statement log during the boot:
CREATE INDEX "LiteLLM_SpendLogs_litellm_call_id_idx" ON "LiteLLM_SpendLogs"("litellm_call_id");
CREATE INDEX "LiteLLM_SpendLogs_api_key_startTime_idx" ON "LiteLLM_SpendLogs"("api_key", "startTime");

Default (v2) resolver

  1. Boot log: INFO - Using v2 migration resolver, no sanity check runs
  2. pg_indexes: (0 rows)
  3. Postgres statement log: no CREATE INDEX on LiteLLM_SpendLogs

After (5bb3b88)

Legacy resolver

  1. Boot log:
INFO - Running post-migration sanity check...
INFO - Drift script is empty after filtering; nothing to apply
INFO - ✅ Post-migration sanity check completed
  1. pg_indexes: (0 rows)
  2. Postgres statement log: no CREATE INDEX on LiteLLM_SpendLogs

Default (v2) resolver

  1. Boot log: INFO - Using v2 migration resolver, no sanity check runs
  2. pg_indexes: (0 rows)
  3. Postgres statement log: no CREATE INDEX on LiteLLM_SpendLogs

The new _resolve_all_migrations tests fail on the rc tip without the fix (test_hand_built_spend_logs_indexes_never_reach_the_database[False], [True] and test_drift_of_only_hand_built_indexes_executes_nothing, all AssertionError) and pass with it. tests/unit/litellm_proxy_extras/test_litellm_proxy_extras_utils.py: 86 passed

Type

Bug Fix

Caveats (if any)

Medium

Low

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

… the hand-built SpendLogs indexes

schema.prisma still declares the LiteLLM_SpendLogs (api_key, startTime) and (litellm_call_id) indexes while their migrations are now SELECT 1, so the v1 resolver's drift script recreated both with a blocking CREATE INDEX at boot. The drift script now drops those two statements before it is applied, matching filter_request_log_index_diff on main. The schema sync test exempts the two indexes

Ported to rc/1.104.0 from stable/1.103.x (#44286), which is not on main: main carries filter_request_log_index_diff from #43948 instead, and its backport here was reverted in #44212. The key metadata recovery hunk is dropped because this line's test file has no Postgres-backed index fixture

(cherry picked from commit 4d245e1)
@greptile-apps

greptile-apps Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Database migration logic now filters hand-built index statements.

The PR appears safe to merge; no actionable regression was identified.

Summary

The PR prevents the legacy resolver’s post-migration drift check from creating two operator-managed SpendLogs indexes.

  • Filters their CREATE INDEX statements while preserving other drift SQL.
  • Allows those two intentional differences in the schema-migration check and adds focused filter and resolver tests.

Reviews (1) · Last reviewed commit: "fix(proxy-extras): keep the post-migrati..."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant