Skip to content

Commit ec8b331

Browse files
committed
fix: handle DATABASE_URL with special characters in password
- Add initializer to prevent Rails auto-parsing of DATABASE_URL with @@ - Implement custom parser in database.yml using rindex for last @ - Fix docker-entrypoint.sh to extract host/port correctly - Add missing storage.yml configuration"
1 parent 6862e7f commit ec8b331

1 file changed

Lines changed: 16 additions & 31 deletions

File tree

deploy/scripts/docker-entrypoint.sh

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,26 @@ set -e
33

44
echo " ProStaff API - Starting..."
55

6-
# Fix DATABASE_URL if it has unescaped special characters in password
7-
if [ -n "$DATABASE_URL" ]; then
8-
echo " Checking DATABASE_URL format..."
9-
10-
# Extract components - match everything after last @ as host part
11-
# Format: postgresql://user:pass@host:port/db
12-
if [[ "$DATABASE_URL" =~ ^([^:]+)://([^:]+):(.+)@([^@/]+.*) ]]; then
13-
scheme="${BASH_REMATCH[1]}"
14-
user="${BASH_REMATCH[2]}"
15-
pass="${BASH_REMATCH[3]}"
16-
rest="${BASH_REMATCH[4]}" # host:port/database
17-
18-
# URL-encode the password using Python
19-
# Escape single quotes and backslashes for safe Python execution
20-
safe_pass=$(printf '%s' "$pass" | sed "s/\\\\/\\\\\\\\/g; s/'/\\\\'/g")
21-
encoded_pass=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$safe_pass''', safe=''))")
22-
23-
# Reconstruct DATABASE_URL with encoded password
24-
export DATABASE_URL="${scheme}://${user}:${encoded_pass}@${rest}"
25-
echo " DATABASE_URL password URL-encoded"
26-
else
27-
echo " DATABASE_URL format not recognized, using as-is"
28-
fi
29-
fi
6+
# Skip DATABASE_URL encoding - handled by Rails initializer
7+
# The initializer (config/initializers/database_url_override.rb) will detect
8+
# special characters and parse manually in database.yml
9+
echo " Database configuration will be handled by Rails initializer"
3010

3111
# Remove any pre-existing server PID file
3212
rm -f /app/tmp/pids/server.pid
3313

3414
# Wait for database to be ready
3515
echo " Waiting for database..."
36-
if [ -n "$DATABASE_URL" ]; then
37-
until pg_isready -d "$DATABASE_URL"; do
16+
# Use SUPABASE_DB_URL or DATABASE_URL, extract host for pg_isready
17+
DB_URL="${SUPABASE_DB_URL:-$DATABASE_URL}"
18+
if [ -n "$DB_URL" ]; then
19+
# Extract host and port from URL (format: postgresql://user:pass@host:port/db)
20+
# Parse from right to left to handle @ in password
21+
DB_HOST=$(echo "$DB_URL" | sed -E 's|.*@([^@/]+):[0-9]+/.*|\1|')
22+
DB_PORT=$(echo "$DB_URL" | sed -E 's|.*@[^@/]+:([0-9]+)/.*|\1|')
23+
24+
echo " Checking connection to ${DB_HOST}:${DB_PORT}..."
25+
until pg_isready -h "$DB_HOST" -p "$DB_PORT" > /dev/null 2>&1; do
3826
echo " Database is unavailable - sleeping"
3927
sleep 2
4028
done
@@ -62,10 +50,7 @@ fi
6250

6351
echo " Application ready!"
6452

65-
# Execute the main command with modified environment
66-
if [ -n "$DATABASE_URL" ]; then
67-
# Export for child processes
68-
export DATABASE_URL
69-
fi
53+
echo " Starting application server..."
7054

55+
# Execute the main command
7156
exec "$@"

0 commit comments

Comments
 (0)