From c8d54facb9baf283bf47db3d9fb0958644a415db Mon Sep 17 00:00:00 2001 From: Muhammad Ibrahim Date: Wed, 17 Sep 2025 23:39:00 +0100 Subject: [PATCH] fix: Use .env file for database credentials in update commands - Update update_instance function to read database credentials from .env file - Fix database backup to use credentials from .env instead of hardcoded values - Update migration commands to load environment variables from .env - Fix small management script update command to use .env credentials This resolves the 'password authentication failed' error when running update commands on existing instances where database credentials are stored in the .env file rather than hardcoded. --- manage-patchmon.sh | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/manage-patchmon.sh b/manage-patchmon.sh index 79748f3..f505dcd 100755 --- a/manage-patchmon.sh +++ b/manage-patchmon.sh @@ -1147,7 +1147,13 @@ case \$1 in git pull npm install cd backend && npm install - npx prisma migrate deploy + if [ -f ".env" ]; then + export \$(grep -v '^#' .env | xargs) + npx prisma migrate deploy + else + echo "Error: .env file not found" + exit 1 + fi cd ../frontend && npm install && npm run build systemctl restart $SERVICE_NAME ;; @@ -1333,11 +1339,28 @@ update_instance() { # Backup database first print_info "Creating database backup..." - local db_safe_name=$(echo $fqdn | tr '[:upper:]' '[:lower:]' | tr '.-' '__') - local db_name="patchmon_${db_safe_name}" - local db_user="patchmon_${db_safe_name}_user" - pg_dump -h localhost -U "$db_user" "$db_name" > backup_$(date +%Y%m%d_%H%M%S).sql + # Read database credentials from .env file + if [ -f "$app_dir/backend/.env" ]; then + local db_name=$(grep "^DATABASE_URL=" "$app_dir/backend/.env" | cut -d'=' -f2 | sed 's/.*\/\([^?]*\).*/\1/') + local db_user=$(grep "^DATABASE_URL=" "$app_dir/backend/.env" | cut -d'@' -f1 | sed 's/.*:\/\/\([^:]*\).*/\1/') + local db_pass=$(grep "^DATABASE_URL=" "$app_dir/backend/.env" | cut -d'@' -f1 | sed 's/.*:\/\/[^:]*:\([^@]*\).*/\1/') + local db_host=$(grep "^DATABASE_URL=" "$app_dir/backend/.env" | cut -d'@' -f2 | cut -d'/' -f1 | cut -d':' -f1) + local db_port=$(grep "^DATABASE_URL=" "$app_dir/backend/.env" | cut -d'@' -f2 | cut -d'/' -f1 | cut -d':' -f2) + + # Set defaults if not found + db_host=${db_host:-localhost} + db_port=${db_port:-5432} + + if [ -n "$db_name" ] && [ -n "$db_user" ] && [ -n "$db_pass" ]; then + PGPASSWORD="$db_pass" pg_dump -h "$db_host" -p "$db_port" -U "$db_user" "$db_name" > backup_$(date +%Y%m%d_%H%M%S).sql + print_status "Database backup created" + else + print_warning "Could not read database credentials from .env file, skipping backup" + fi + else + print_warning ".env file not found, skipping database backup" + fi # Update code print_info "Pulling latest code..." @@ -1352,7 +1375,16 @@ update_instance() { # Run migrations print_info "Running database migrations..." cd ../backend - npx prisma migrate deploy + + # Set environment variables for Prisma + if [ -f ".env" ]; then + export $(grep -v '^#' .env | xargs) + npx prisma migrate deploy + print_status "Database migrations completed" + else + print_error ".env file not found, cannot run migrations" + exit 1 + fi # Rebuild frontend print_info "Rebuilding frontend..."