The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(litellm_proxy_extras): add baselining db script (#9942)
Krish Dholakiad004fb542ff9
diff --git a/.circleci/config.yml b/.circleci/config.ymlindex 32414e8c4e0..8b5b3ef55a1 100644--- a/.circleci/config.yml+++ b/.circleci/config.yml@@ -2390,6 +2390,108 @@ jobs:echo "triggering load testing server for version ${VERSION} and commit ${CIRCLE_SHA1}"curl -X POST "https://proxyloadtester-production.up.railway.app/start/load/test?version=${VERSION}&commit_hash=${CIRCLE_SHA1}&release_type=nightly"+ publish_proxy_extras:+ docker:+ - image: cimg/python:3.8+ working_directory: ~/project/litellm-proxy-extras+ environment:+ TWINE_USERNAME: __token__++ steps:+ - checkout:+ path: ~/project++ - run:+ name: Check if litellm-proxy-extras dir or pyproject.toml was modified+ command: |+ echo "Install TOML package."+ python -m pip install toml+ # Get current version from pyproject.toml+ CURRENT_VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['tool']['poetry']['version'])")++ # Get last published version from PyPI+ LAST_VERSION=$(curl -s https://pypi.org/pypi/litellm-proxy-extras/json | python -c "import json, sys; print(json.load(sys.stdin)['info']['version'])")++ echo "Current version: $CURRENT_VERSION"+ echo "Last published version: $LAST_VERSION"++ # Compare versions using Python's packaging.version+ VERSION_COMPARE=$(python -c "from packaging import version; print(1 if version.parse('$CURRENT_VERSION') < version.parse('$LAST_VERSION') else 0)")++ echo "Version compare: $VERSION_COMPARE"+ if [ "$VERSION_COMPARE" = "1" ]; then+ echo "Error: Current version ($CURRENT_VERSION) is less than last published version ($LAST_VERSION)"+ exit 1+ fi++ # If versions are equal or current is greater, check contents+ pip download --no-deps litellm-proxy-extras==$LAST_VERSION -d /tmp+ # Find the downloaded file+ DOWNLOADED_FILE=$(ls /tmp/litellm_proxy_extras-*.tar.gz)+ tar -xzf "$DOWNLOADED_FILE" -C /tmp++ echo "Downloaded file: $DOWNLOADED_FILE"+ echo "Contents of extracted package:"+ ls -R /tmp/litellm_proxy_extras-$LAST_VERSION++ # Compare contents+ if ! diff -r /tmp/litellm_proxy_extras-$LAST_VERSION/litellm_proxy_extras ./litellm_proxy_extras; then+ if [ "$CURRENT_VERSION" = "$LAST_VERSION" ]; then+ echo "Error: Changes detected in litellm-proxy-extras but version was not bumped"+ echo "Current version: $CURRENT_VERSION"+ echo "Last published version: $LAST_VERSION"+ echo "Changes:"+ diff -r /tmp/litellm_proxy_extras-$LAST_VERSION/litellm_proxy_extras ./litellm_proxy_extras+ exit 1+ fi+ else+ echo "No changes detected in litellm-proxy-extras. Skipping PyPI publish."+ circleci step halt+ fi++ # Check if there are changes+ if [ -n "$(git diff --name-only $CIRCLE_SHA1^..$CIRCLE_SHA1 | grep -E 'litellm-proxy-extras/|litellm-proxy-extras/pyproject\.toml')" ]; then+ echo "litellm-proxy-extras or its pyproject.toml updated"+ else+ echo "No changes to litellm-proxy-extras. Skipping PyPI publish."+ circleci step halt+ fi++ - run:+ name: Get new version+ command: |+ cd litellm-proxy-extras+ NEW_VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['tool']['poetry']['version'])")+ echo "export NEW_VERSION=$NEW_VERSION" >> $BASH_ENV++ - run:+ name: Check if versions match+ command: |+ cd ~/project+ # Check pyproject.toml+ CURRENT_VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['tool']['poetry']['dependencies']['litellm-proxy-extras'].split('\"')[1])")+ if [ "$CURRENT_VERSION" != "$NEW_VERSION" ]; then+ echo "Error: Version in pyproject.toml ($CURRENT_VERSION) doesn't match new version ($NEW_VERSION)"+ exit 1+ fi++ # Check requirements.txt+ REQ_VERSION=$(grep -oP 'litellm-proxy-extras==\K[0-9.]+' requirements.txt)+ if [ "$REQ_VERSION" != "$NEW_VERSION" ]; then+ echo "Error: Version in requirements.txt ($REQ_VERSION) doesn't match new version ($NEW_VERSION)"+ exit 1+ fi++ - run:+ name: Publish to PyPI+ command: |+ cd litellm-proxy-extras+ echo -e "[pypi]\nusername = $PYPI_PUBLISH_USERNAME\npassword = $PYPI_PUBLISH_PASSWORD" > ~/.pypirc+ python -m pip install --upgrade pip build twine setuptools wheel+ rm -rf build dist+ python -m build+ twine upload --verbose dist/*+e2e_ui_testing:machine:image: ubuntu-2204:2023.10.1@@ -2785,6 +2887,16 @@ workflows:only:- main- /litellm_.*/+ - publish_proxy_extras:+ filters:+ branches:+ only:+ - main+ - /litellm_.*/+ # filters:+ # branches:+ # only:+ # - main- publish_to_pypi:requires:- local_testing@@ -2819,7 +2931,5 @@ workflows:- proxy_build_from_pip_tests- proxy_pass_through_endpoint_tests- check_code_and_doc_quality- filters:- branches:- only:- - main+ - publish_proxy_extras+diff --git a/litellm-proxy-extras/litellm_proxy_extras/utils.py b/litellm-proxy-extras/litellm_proxy_extras/utils.pyindex cd9beeb7536..2dab9421cb1 100644--- a/litellm-proxy-extras/litellm_proxy_extras/utils.py+++ b/litellm-proxy-extras/litellm_proxy_extras/utils.py@@ -1,7 +1,9 @@+import globimport osimport randomimport subprocessimport time+from pathlib import Pathfrom typing import Optionalfrom litellm_proxy_extras._logging import logger@@ -14,6 +16,94 @@ def str_to_bool(value: Optional[str]) -> bool:class ProxyExtrasDBManager:+ @staticmethod+ def _get_prisma_dir() -> str:+ """Get the path to the migrations directory"""+ migrations_dir = os.path.dirname(__file__)+ return migrations_dir++ @staticmethod+ def _create_baseline_migration(schema_path: str) -> bool:+ """Create a baseline migration for an existing database"""+ prisma_dir = ProxyExtrasDBManager._get_prisma_dir()+ prisma_dir_path = Path(prisma_dir)+ init_dir = prisma_dir_path / "migrations" / "0_init"++ # Create migrations/0_init directory+ init_dir.mkdir(parents=True, exist_ok=True)++ # Generate migration SQL file+ migration_file = init_dir / "migration.sql"++ try:+ # Generate migration diff with increased timeout+ subprocess.run(+ [+ "prisma",+ "migrate",+ "diff",+ "--from-empty",+ "--to-schema-datamodel",+ str(schema_path),+ "--script",+ ],+ stdout=open(migration_file, "w"),+ check=True,+ timeout=30,+ ) # 30 second timeout++ # Mark migration as applied with increased timeout+ subprocess.run(+ [+ "prisma",+ "migrate",+ "resolve",+ "--applied",+ "0_init",+ ],+ check=True,+ timeout=30,+ )++ return True+ except subprocess.TimeoutExpired:+ logger.warning(+ "Migration timed out - the database might be under heavy load."+ )+ return False+ except subprocess.CalledProcessError as e:+ logger.warning(f"Error creating baseline migration: {e}")+ return False++ @staticmethod+ def _get_migration_names(migrations_dir: str) -> list:+ """Get all migration directory names from the migrations folder"""+ migration_paths = glob.glob(f"{migrations_dir}/migrations/*/migration.sql")+ logger.info(f"Found {len(migration_paths)} migrations at {migrations_dir}")+ return [Path(p).parent.name for p in migration_paths]++ @staticmethod+ def _resolve_all_migrations(migrations_dir: str):+ """Mark all existing migrations as applied"""+ migration_names = ProxyExtrasDBManager._get_migration_names(migrations_dir)+ logger.info(f"Resolving {len(migration_names)} migrations")+ for migration_name in migration_names:+ try:+ logger.info(f"Resolving migration: {migration_name}")+ subprocess.run(+ ["prisma", "migrate", "resolve", "--applied", migration_name],+ timeout=60,+ check=True,+ capture_output=True,+ text=True,+ )+ logger.debug(f"Resolved migration: {migration_name}")+ except subprocess.CalledProcessError as e:+ if "is already recorded as applied in the database." not in e.stderr:+ logger.warning(+ f"Failed to resolve migration {migration_name}: {e.stderr}"+ )+@staticmethoddef setup_database(schema_path: str, use_migrate: bool = False) -> bool:"""@@ -30,7 +120,7 @@ class ProxyExtrasDBManager:use_migrate = str_to_bool(os.getenv("USE_PRISMA_MIGRATE")) or use_migratefor attempt in range(4):original_dir = os.getcwd()- migrations_dir = os.path.dirname(__file__)+ migrations_dir = ProxyExtrasDBManager._get_prisma_dir()os.chdir(migrations_dir)try:@@ -55,8 +145,16 @@ class ProxyExtrasDBManager:"P3005" in e.stderrand "database schema is not empty" in e.stderr):- logger.info("Error: Database schema is not empty")- return False+ logger.info(+ "Database schema is not empty, creating baseline migration"+ )+ ProxyExtrasDBManager._create_baseline_migration(schema_path)+ logger.info(+ "Baseline migration created, resolving all migrations"+ )+ ProxyExtrasDBManager._resolve_all_migrations(migrations_dir)+ logger.info("✅ All migrations resolved.")+ return Trueelse:# Use prisma db push with increased timeoutsubprocess.run(diff --git a/litellm/proxy/db/prisma_client.py b/litellm/proxy/db/prisma_client.pyindex c81dc35e0c6..339117cb272 100644--- a/litellm/proxy/db/prisma_client.py+++ b/litellm/proxy/db/prisma_client.py@@ -3,7 +3,6 @@ This file contains the PrismaWrapper class, which is used to wrap the Prisma cli"""import asyncio-import globimport osimport randomimport subprocess@@ -11,7 +10,6 @@ import timeimport urllibimport urllib.parsefrom datetime import datetime, timedelta-from pathlib import Pathfrom typing import Any, Optional, Unionfrom litellm._logging import verbose_proxy_logger@@ -126,86 +124,6 @@ class PrismaManager:dname = os.path.dirname(os.path.dirname(abspath))return dname- @staticmethod- def _create_baseline_migration(schema_path: str) -> bool:- """Create a baseline migration for an existing database"""- prisma_dir = PrismaManager._get_prisma_dir()- prisma_dir_path = Path(prisma_dir)- init_dir = prisma_dir_path / "migrations" / "0_init"-- # Create migrations/0_init directory- init_dir.mkdir(parents=True, exist_ok=True)-- # Generate migration SQL file- migration_file = init_dir / "migration.sql"-- try:- # Generate migration diff with increased timeout- subprocess.run(- [- "prisma",- "migrate",- "diff",- "--from-empty",- "--to-schema-datamodel",- str(schema_path),- "--script",- ],- stdout=open(migration_file, "w"),- check=True,- timeout=30,- ) # 30 second timeout-- # Mark migration as applied with increased timeout- subprocess.run(- [- "prisma",- "migrate",- "resolve",- "--applied",- "0_init",- ],- check=True,- timeout=30,- )-- return True- except subprocess.TimeoutExpired:- verbose_proxy_logger.warning(- "Migration timed out - the database might be under heavy load."- )- return False- except subprocess.CalledProcessError as e:- verbose_proxy_logger.warning(f"Error creating baseline migration: {e}")- return False-- @staticmethod- def _get_migration_names(migrations_dir: str) -> list:- """Get all migration directory names from the migrations folder"""- migration_paths = glob.glob(f"{migrations_dir}/*/migration.sql")- return [Path(p).parent.name for p in migration_paths]-- @staticmethod- def _resolve_all_migrations(migrations_dir: str):- """Mark all existing migrations as applied"""- migration_names = PrismaManager._get_migration_names(migrations_dir)- for migration_name in migration_names:- try:- verbose_proxy_logger.info(f"Resolving migration: {migration_name}")- subprocess.run(- ["prisma", "migrate", "resolve", "--applied", migration_name],- timeout=60,- check=True,- capture_output=True,- text=True,- )- verbose_proxy_logger.debug(f"Resolved migration: {migration_name}")- except subprocess.CalledProcessError as e:- if "is already recorded as applied in the database." not in e.stderr:- verbose_proxy_logger.warning(- f"Failed to resolve migration {migration_name}: {e.stderr}"- )-@staticmethoddef setup_database(use_migrate: bool = False) -> bool:"""