From c8020168594935e9ee293372c24fbcd32fc0fb1f Mon Sep 17 00:00:00 2001 From: jesus Date: Tue, 22 Sep 2026 18:57:27 +0000 Subject: [PATCH] fix(proxy): stop the legacy resolver marking migrations applied after a failed diff Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../litellm_proxy_extras/utils.py | 2 ++ .../test_litellm_proxy_extras_utils.py | 21 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/litellm-proxy-extras/litellm_proxy_extras/utils.py b/litellm-proxy-extras/litellm_proxy_extras/utils.py index 8a83c786e02..8e8732b8732 100644 --- a/litellm-proxy-extras/litellm_proxy_extras/utils.py +++ b/litellm-proxy-extras/litellm_proxy_extras/utils.py @@ -473,8 +473,10 @@ class ProxyExtrasDBManager: env=_get_prisma_env(), ) except subprocess.CalledProcessError as e: + diff_sql_path.unlink(missing_ok=True) logger.warning(f"Failed to generate migration diff: {e.stderr}") except subprocess.TimeoutExpired: + diff_sql_path.unlink(missing_ok=True) logger.warning("Migration diff generation timed out.") # check if the migration was created 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 bb329264a11..dd03e3cc0dd 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,7 @@ class _FakeCompleted: class TestResolveAllMigrationsLedger: - def _run(self, monkeypatch, tmp_path, partitioned, execute_fails): + def _run(self, monkeypatch, tmp_path, partitioned, execute_fails, diff_fails=False): import subprocess as subprocess_module import litellm_proxy_extras.utils as utils_module @@ -555,9 +555,19 @@ class TestResolveAllMigrationsLedger: ) calls = [] + for migration_name in ( + "20250326162113_baseline", + "20260520120000_add_mcp_env_vars", + ): + migration_dir = tmp_path / migration_name + migration_dir.mkdir(parents=True) + (migration_dir / "migration.sql").write_text("SELECT 1;") + def fake_run(cmd, **kwargs): calls.append(cmd) if "diff" in cmd: + if diff_fails: + raise subprocess_module.CalledProcessError(1, cmd, stderr="P1001") kwargs["stdout"].write(_PARTITIONED_DRIFT_SQL) return _FakeCompleted() if "execute" in cmd: @@ -599,6 +609,15 @@ 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): + 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"), + ] + assert self._resolved(calls) == [] + class TestPartitionedSpendLogsPushGuard: def _forbid_subprocess(self, monkeypatch):