Skip to content

Commit 2947375

Browse files
committed
fix: correctly fail restore script
1 parent b783539 commit 2947375

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

internal/workers/pg_dump_restore.sh

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,44 @@ start_async_restore() {
130130
# Pipe pg_dump directly to psql
131131
# We use ON_ERROR_STOP=0 to continue past non-fatal errors like missing extensions
132132
# This allows restores from managed services that have proprietary extensions
133+
#
134+
# We capture PIPESTATUS inside bash -c to get both exit codes
135+
# Write exit codes to temp file to avoid output parsing issues
136+
EXIT_CODES_FILE=\"${RESTORE_LOG_DIR}/restore-${DATABASE_NAME}.exitcodes\"
133137
set +e
134-
sudo -u postgres bash -c \"\${PG_BIN}/pg_dump \\\"${CONNECTION_STRING}\\\" \${DUMP_FLAGS} | \${PG_BIN}/psql -p ${PG_PORT} -d \\\"${DATABASE_NAME}\\\" -v ON_ERROR_STOP=0\" 2>&1
135-
PIPE_EXIT_CODE=\$?
138+
sudo -u postgres bash -c \"
139+
set +e
140+
\${PG_BIN}/pg_dump \\\"${CONNECTION_STRING}\\\" \${DUMP_FLAGS} | \${PG_BIN}/psql -p ${PG_PORT} -d \\\"${DATABASE_NAME}\\\" -v ON_ERROR_STOP=0
141+
echo \\\${PIPESTATUS[0]}:\\\${PIPESTATUS[1]} > ${EXIT_CODES_FILE}
142+
\" 2>&1
136143
set -e
137144
138-
# Log the exit code for debugging
139-
log \"Restore completed with exit code \$PIPE_EXIT_CODE\"
145+
# Read and parse exit codes
146+
if [ -f \"\$EXIT_CODES_FILE\" ]; then
147+
EXIT_CODES=\$(cat \"\$EXIT_CODES_FILE\")
148+
PGDUMP_EXIT=\$(echo \"\$EXIT_CODES\" | cut -d: -f1)
149+
PSQL_EXIT=\$(echo \"\$EXIT_CODES\" | cut -d: -f2)
150+
rm -f \"\$EXIT_CODES_FILE\"
151+
else
152+
die \"Failed to capture exit codes from pg_dump/psql pipeline\"
153+
fi
154+
155+
# Log the exit codes for debugging
156+
log \"Restore completed - pg_dump exit code: \$PGDUMP_EXIT, psql exit code: \$PSQL_EXIT\"
157+
158+
# Check pg_dump exit code
159+
if [ \$PGDUMP_EXIT -ne 0 ]; then
160+
die \"pg_dump failed with exit code \$PGDUMP_EXIT - restore incomplete\"
161+
fi
140162
141-
# Only fail on truly fatal errors
142-
if [ \$PIPE_EXIT_CODE -gt 3 ]; then
143-
die \"pg_dump | psql piped restore failed with fatal exit code \$PIPE_EXIT_CODE\"
163+
# Check psql exit code - allow non-fatal errors (exit codes 1-3) from psql
164+
# These can occur with missing extensions or other non-fatal issues
165+
if [ \$PSQL_EXIT -gt 3 ]; then
166+
die \"psql restore failed with fatal exit code \$PSQL_EXIT\"
144167
fi
145168
146-
if [ \$PIPE_EXIT_CODE -ne 0 ]; then
147-
log \"Restore completed with warnings\"
169+
if [ \$PSQL_EXIT -ne 0 ]; then
170+
log \"Restore completed with psql warnings (exit code \$PSQL_EXIT)\"
148171
fi
149172
150173
log \"Verifying PostgreSQL is accepting connections after restore...\"

0 commit comments

Comments
 (0)