From 2aad137bd7c9f57cc8b6f65ceb18b51d0bde0d55 Mon Sep 17 00:00:00 2001 From: olsch01 Date: Thu, 9 Apr 2026 09:15:54 -0400 Subject: [PATCH] fix: resolve unbound variable error in deploy script migration check The APPLIED_MIGRATIONS associative array triggered "unbound variable" under set -u when empty (first run / seed-existing). Fix by initializing with =() and using a safe helper function with ${:-} default syntax. Co-Authored-By: Claude Opus 4.6 --- scripts/deploy-prod.sh | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/scripts/deploy-prod.sh b/scripts/deploy-prod.sh index 7e9619c..a329196 100755 --- a/scripts/deploy-prod.sh +++ b/scripts/deploy-prod.sh @@ -248,12 +248,19 @@ CREATE TABLE IF NOT EXISTS shared.schema_migrations ( SQL ok "Migration tracking table ready" +# Helper: check if a migration has been applied (safe with set -u) +is_applied() { + local key="$1" + # Use a subshell test to avoid unbound variable with set -u on empty associative arrays + [[ -n "${APPLIED_MIGRATIONS[$key]:-}" ]] +} + # Step 5b: Get list of already-applied migrations -declare -A APPLIED_MIGRATIONS +declare -A APPLIED_MIGRATIONS=() while IFS= read -r fname; do fname=$(echo "$fname" | xargs) # trim whitespace [ -n "$fname" ] && APPLIED_MIGRATIONS["$fname"]=1 -done < <(run_sql -t -c "SELECT filename FROM shared.schema_migrations ORDER BY filename;") +done < <(run_sql -t -c "SELECT filename FROM shared.schema_migrations ORDER BY filename;" 2>/dev/null || true) APPLIED_COUNT=${#APPLIED_MIGRATIONS[@]} log "Previously applied migrations: $APPLIED_COUNT" @@ -304,7 +311,7 @@ PENDING_COUNT=0 APPLIED_THIS_RUN=0 for filename in "${MIGRATION_FILES[@]}"; do - if [ -n "${APPLIED_MIGRATIONS[$filename]+x}" ]; then + if is_applied "$filename"; then continue fi ((PENDING_COUNT++)) @@ -317,7 +324,7 @@ else echo "" for filename in "${MIGRATION_FILES[@]}"; do - if [ -n "${APPLIED_MIGRATIONS[$filename]+x}" ]; then + if is_applied "$filename"; then continue fi -- 2.49.1