Files
litellm/tests/proxy_migration_tests/test_db_schema_migration.py
T
ryan-crabbe-berri 770fff7058 test(proxy): stop running real-DB tests in GitHub Actions unit jobs (#29700)
* test(proxy): stop running real-DB tests in GitHub Actions unit jobs

GitHub Actions unit jobs were spinning up a Postgres service container, but
the only active tests that touched it either used the DB incidentally (a
cargo-culted prisma_client.connect()) or were genuine integration tests
mislabeled as unit. Mock the incidental ones so the proxy-db job needs no
container, and move the tests that genuinely need a database (proxy
management behavior, master-key-not-persisted, schema-migration sync) to
CircleCI, which is already the real-infrastructure lane.

* test(proxy): restore no-unexpected-startup-writes canary in master-key test

Greptile noted the hash-match assertion no longer catches other unexpected
startup writes (a default key, a rotation artifact). The CircleCI job gives
each run a fresh DB, so a clean startup must leave the table empty; add that
canary back alongside the precise master-key assertion.
2026-06-04 14:56:02 -07:00

71 lines
2.2 KiB
Python

import os
import shutil
import subprocess
import tempfile
from pathlib import Path
import pytest
@pytest.mark.skipif(
"DATABASE_URL" not in os.environ,
reason="requires a postgres database (DATABASE_URL)",
)
def test_schema_migration_in_sync():
"""Fail if schema.prisma has changes not captured by the committed migrations.
Applies every committed migration to an empty database, then diffs the result
against schema.prisma. A non-empty diff means the schema was changed without a
matching migration being generated.
"""
db_url = os.environ["DATABASE_URL"]
source_migrations_dir = Path(
"./litellm-proxy-extras/litellm_proxy_extras/migrations"
)
source_schema_path = Path("./schema.prisma")
temp_base = Path(tempfile.mkdtemp(prefix="litellm_schema_migration_"))
schema_path = temp_base / "schema.prisma"
migrations_dir = temp_base / "migrations"
try:
shutil.copy(source_schema_path, schema_path)
shutil.copytree(source_migrations_dir, migrations_dir)
if not any(migrations_dir.iterdir()):
pytest.fail(
"No existing migrations found. Run `python litellm/ci_cd/baseline_db_migration.py`."
)
subprocess.run(
["prisma", "migrate", "deploy", "--schema", str(schema_path)],
check=True,
env={**os.environ, "DATABASE_URL": db_url},
)
diff = subprocess.run(
[
"prisma",
"migrate",
"diff",
"--from-url",
db_url,
"--to-schema-datamodel",
str(schema_path),
"--script",
"--exit-code",
],
capture_output=True,
text=True,
)
if diff.returncode == 2:
pytest.fail(
"Schema changes detected that no migration captures. Run "
"`python litellm/ci_cd/run_migration.py <migration_name>`.\n\n"
+ diff.stdout
)
assert diff.returncode == 0, f"prisma migrate diff errored: {diff.stderr}"
finally:
shutil.rmtree(temp_base, ignore_errors=True)