From 2ab2718c21dfe4db9584d31d2fcc2ecaca6dc868 Mon Sep 17 00:00:00 2001 From: jesus Date: Tue, 22 Sep 2026 19:06:05 +0000 Subject: [PATCH] fix(proxy): make the legacy resolver fallback find and record migration files Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../litellm_proxy_extras/utils.py | 8 ++++- .../test_litellm_proxy_extras_utils.py | 34 +++++++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/litellm-proxy-extras/litellm_proxy_extras/utils.py b/litellm-proxy-extras/litellm_proxy_extras/utils.py index 8e8732b8732..2c3c906990b 100644 --- a/litellm-proxy-extras/litellm_proxy_extras/utils.py +++ b/litellm-proxy-extras/litellm_proxy_extras/utils.py @@ -489,7 +489,8 @@ class ProxyExtrasDBManager: # Fall back: run each migration SQL file directly via prisma db execute. # This works with pooler URLs (no schema introspection needed) and is # safe to re-run because migrations use IF NOT EXISTS / IF EXISTS guards. - migration_files = sorted(Path(migrations_dir).glob("*/migration.sql")) + migration_files = sorted(Path(migrations_dir).glob("migrations/*/migration.sql")) + fallback_ok = True for mig_file in migration_files: try: prisma_toolchain.run_prisma( @@ -510,8 +511,13 @@ class ProxyExtrasDBManager: logger.warning( f"Failed to apply migration {mig_file.parent.name}: {e.stderr}" ) + if not ProxyExtrasDBManager._is_idempotent_error(str(e.stderr or "")): # pyright: ignore[reportAny] # CalledProcessError.stderr is Any + fallback_ok = False except subprocess.TimeoutExpired: logger.warning(f"Migration {mig_file.parent.name} timed out.") + fallback_ok = False + if mark_all_applied and fallback_ok: + ProxyExtrasDBManager._mark_migrations_applied(migrations_dir) return logger.info(f"Migration diff created at {diff_sql_path}") diff --git a/tests/litellm-proxy-extras/test_litellm_proxy_extras_utils.py b/tests/litellm-proxy-extras/test_litellm_proxy_extras_utils.py index dd03e3cc0dd..b115047b4b2 100644 --- a/tests/litellm-proxy-extras/test_litellm_proxy_extras_utils.py +++ b/tests/litellm-proxy-extras/test_litellm_proxy_extras_utils.py @@ -538,7 +538,15 @@ class _FakeCompleted: class TestResolveAllMigrationsLedger: - def _run(self, monkeypatch, tmp_path, partitioned, execute_fails, diff_fails=False): + def _run( + self, + monkeypatch, + tmp_path, + partitioned, + execute_fails, + diff_fails=False, + execute_stderr="boom", + ): import subprocess as subprocess_module import litellm_proxy_extras.utils as utils_module @@ -559,7 +567,7 @@ class TestResolveAllMigrationsLedger: "20250326162113_baseline", "20260520120000_add_mcp_env_vars", ): - migration_dir = tmp_path / migration_name + migration_dir = tmp_path / "migrations" / migration_name migration_dir.mkdir(parents=True) (migration_dir / "migration.sql").write_text("SELECT 1;") @@ -574,7 +582,7 @@ class TestResolveAllMigrationsLedger: executed_sql = open(cmd[cmd.index("--file") + 1]).read() calls.append(("executed_sql", executed_sql)) if execute_fails: - raise subprocess_module.CalledProcessError(1, cmd, stderr="boom") + raise subprocess_module.CalledProcessError(1, cmd, stderr=execute_stderr) return _FakeCompleted() return _FakeCompleted() @@ -609,13 +617,27 @@ class TestResolveAllMigrationsLedger: calls = self._run(monkeypatch, tmp_path, partitioned=False, execute_fails=False) assert self._executed_sql(calls) == _PARTITIONED_DRIFT_SQL - def test_failed_diff_runs_the_migration_files_directly_and_marks_nothing_applied(self, monkeypatch, tmp_path): + def test_failed_diff_runs_the_migration_files_directly_and_marks_them_applied(self, monkeypatch, tmp_path): calls = self._run(monkeypatch, tmp_path, partitioned=False, execute_fails=False, diff_fails=True) executed_files = [c[c.index("--file") + 1] for c in calls if isinstance(c, list) and "execute" in c] assert executed_files == [ - str(tmp_path / "20250326162113_baseline" / "migration.sql"), - str(tmp_path / "20260520120000_add_mcp_env_vars" / "migration.sql"), + str(tmp_path / "migrations" / "20250326162113_baseline" / "migration.sql"), + str(tmp_path / "migrations" / "20260520120000_add_mcp_env_vars" / "migration.sql"), ] + assert len(self._resolved(calls)) == 1 + + def test_failed_diff_with_already_exists_errors_still_marks_migrations_applied(self, monkeypatch, tmp_path): + calls = self._run( + monkeypatch, tmp_path, partitioned=False, execute_fails=True, diff_fails=True, + execute_stderr='ERROR: relation "LiteLLM_MCPUserEnvVars" already exists', + ) + assert len(self._resolved(calls)) == 1 + + def test_failed_diff_with_a_real_execute_failure_marks_nothing_applied(self, monkeypatch, tmp_path): + calls = self._run( + monkeypatch, tmp_path, partitioned=False, execute_fails=True, diff_fails=True, + execute_stderr="ERROR: permission denied for schema public", + ) assert self._resolved(calls) == []