Skip to content

Commit b78dd64

Browse files
fix(prisma_migration.py): support decrypting variables in a python script
1 parent b84d335 commit b78dd64

File tree

3 files changed

+79
-43
lines changed

3 files changed

+79
-43
lines changed

Diff for: entrypoint.sh

+9-43
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,13 @@
1-
#!/bin/sh
1+
#!/bin/bash
2+
echo $(pwd)
23

3-
# Check if DATABASE_URL is not set
4-
if [ -z "$DATABASE_URL" ]; then
5-
# Check if all required variables are provided
6-
if [ -n "$DATABASE_HOST" ] && [ -n "$DATABASE_USERNAME" ] && [ -n "$DATABASE_PASSWORD" ] && [ -n "$DATABASE_NAME" ]; then
7-
# Construct DATABASE_URL from the provided variables
8-
DATABASE_URL="postgresql://${DATABASE_USERNAME}:${DATABASE_PASSWORD}@${DATABASE_HOST}/${DATABASE_NAME}"
9-
export DATABASE_URL
10-
else
11-
echo "Error: Required database environment variables are not set. Provide a postgres url for DATABASE_URL."
12-
exit 1
13-
fi
14-
fi
15-
16-
# Set DIRECT_URL to the value of DATABASE_URL if it is not set, required for migrations
17-
if [ -z "$DIRECT_URL" ]; then
18-
export DIRECT_URL=$DATABASE_URL
19-
fi
20-
21-
# Apply migrations
22-
retry_count=0
23-
max_retries=3
24-
exit_code=1
25-
26-
until [ $retry_count -ge $max_retries ] || [ $exit_code -eq 0 ]
27-
do
28-
retry_count=$((retry_count+1))
29-
echo "Attempt $retry_count..."
4+
# Run the Python migration script
5+
python3 litellm/proxy/prisma_migration.py
306

31-
# Run the Prisma db push command
32-
prisma db push --accept-data-loss
33-
34-
exit_code=$?
35-
36-
if [ $exit_code -ne 0 ] && [ $retry_count -lt $max_retries ]; then
37-
echo "Retrying in 10 seconds..."
38-
sleep 10
39-
fi
40-
done
41-
42-
if [ $exit_code -ne 0 ]; then
43-
echo "Unable to push database changes after $max_retries retries."
7+
# Check if the Python script executed successfully
8+
if [ $? -eq 0 ]; then
9+
echo "Migration script ran successfully!"
10+
else
11+
echo "Migration script failed!"
4412
exit 1
4513
fi
46-
47-
echo "Database push successful!"

Diff for: litellm/proxy/prisma_migration.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# What is this?
2+
## Script to apply initial prisma migration on Docker setup
3+
4+
import os
5+
import subprocess
6+
import sys
7+
import time
8+
9+
sys.path.insert(
10+
0, os.path.abspath("./")
11+
) # Adds the parent directory to the system path
12+
from litellm.proxy.secret_managers.aws_secret_manager import decrypt_env_var
13+
14+
if os.getenv("USE_AWS_KMS", None) is not None and os.getenv("USE_AWS_KMS") == "True":
15+
## V2 IMPLEMENTATION OF AWS KMS - USER WANTS TO DECRYPT MULTIPLE KEYS IN THEIR ENV
16+
new_env_var = decrypt_env_var()
17+
18+
for k, v in new_env_var.items():
19+
os.environ[k] = v
20+
21+
# Check if DATABASE_URL is not set
22+
database_url = os.getenv("DATABASE_URL")
23+
if not database_url:
24+
# Check if all required variables are provided
25+
database_host = os.getenv("DATABASE_HOST")
26+
database_username = os.getenv("DATABASE_USERNAME")
27+
database_password = os.getenv("DATABASE_PASSWORD")
28+
database_name = os.getenv("DATABASE_NAME")
29+
30+
if database_host and database_username and database_password and database_name:
31+
# Construct DATABASE_URL from the provided variables
32+
database_url = f"postgresql://{database_username}:{database_password}@{database_host}/{database_name}"
33+
os.environ["DATABASE_URL"] = database_url
34+
else:
35+
print( # noqa
36+
"Error: Required database environment variables are not set. Provide a postgres url for DATABASE_URL." # noqa
37+
)
38+
exit(1)
39+
40+
# Set DIRECT_URL to the value of DATABASE_URL if it is not set, required for migrations
41+
direct_url = os.getenv("DIRECT_URL")
42+
if not direct_url:
43+
os.environ["DIRECT_URL"] = database_url
44+
45+
# Apply migrations
46+
retry_count = 0
47+
max_retries = 3
48+
exit_code = 1
49+
50+
while retry_count < max_retries and exit_code != 0:
51+
retry_count += 1
52+
print(f"Attempt {retry_count}...") # noqa
53+
54+
# Run the Prisma db push command
55+
result = subprocess.run(
56+
["prisma", "db", "push", "--accept-data-loss"], capture_output=True
57+
)
58+
exit_code = result.returncode
59+
60+
if exit_code != 0 and retry_count < max_retries:
61+
print("Retrying in 10 seconds...") # noqa
62+
time.sleep(10)
63+
64+
if exit_code != 0:
65+
print(f"Unable to push database changes after {max_retries} retries.") # noqa
66+
exit(1)
67+
68+
print("Database push successful!") # noqa

Diff for: tests/test_entrypoint.py

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import subprocess
1313

1414

15+
@pytest.mark.skip(reason="local test")
1516
def test_decrypt_and_reset_env():
1617
os.environ["DATABASE_URL"] = (
1718
"aws_kms/AQICAHgwddjZ9xjVaZ9CNCG8smFU6FiQvfdrjL12DIqi9vUAQwHwF6U7caMgHQa6tK+TzaoMAAAAzjCBywYJKoZIhvcNAQcGoIG9MIG6AgEAMIG0BgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDCmu+DVeKTm5tFZu6AIBEICBhnOFQYviL8JsciGk0bZsn9pfzeYWtNkVXEsl01AdgHBqT9UOZOI4ZC+T3wO/fXA7wdNF4o8ASPDbVZ34ZFdBs8xt4LKp9niufL30WYBkuuzz89ztly0jvE9pZ8L6BMw0ATTaMgIweVtVSDCeCzEb5PUPyxt4QayrlYHBGrNH5Aq/axFTe0La"
@@ -29,6 +30,7 @@ def test_decrypt_and_reset_env():
2930
print("DATABASE_URL={}".format(os.environ["DATABASE_URL"]))
3031

3132

33+
@pytest.mark.skip(reason="local test")
3234
def test_entrypoint_decrypt_and_reset():
3335
os.environ["DATABASE_URL"] = (
3436
"aws_kms/AQICAHgwddjZ9xjVaZ9CNCG8smFU6FiQvfdrjL12DIqi9vUAQwHwF6U7caMgHQa6tK+TzaoMAAAAzjCBywYJKoZIhvcNAQcGoIG9MIG6AgEAMIG0BgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDCmu+DVeKTm5tFZu6AIBEICBhnOFQYviL8JsciGk0bZsn9pfzeYWtNkVXEsl01AdgHBqT9UOZOI4ZC+T3wO/fXA7wdNF4o8ASPDbVZ34ZFdBs8xt4LKp9niufL30WYBkuuzz89ztly0jvE9pZ8L6BMw0ATTaMgIweVtVSDCeCzEb5PUPyxt4QayrlYHBGrNH5Aq/axFTe0La"

0 commit comments

Comments
 (0)